I am trying to send a single byte to a PIC from a computer (Note: the pic only accepts a single byte else it will only accept the first byte of the data send)
This shouldn’t be much of a problem because I only want to manage a total of 8 LEDS so I only need it to go from 0 to 255 but I am having issues achieving this.
If I try to send the value 1 to the pic my program sends 31 if I try to send 5 it sends 35 If I try to send 255 it sends 3*2*3*5*3*5* so for every single digit I try to send it adds a 3 in front of it. I am using the following code to determine the value and to send it:
Dim t As Integer = 0
Dim result As Integer = 0
For Each chk As CheckBox In GroupBox1.Controls
If chk.Checked = True Then
result = result + 2 ^ t
End If
t = t + 1
Next
publisher.Connect(IPTo, PortTo)
Dim sendbytes() As Byte = ASCII.GetBytes(result)
publisher.Send(sendbytes, sendbytes.Length)
I think the issue is with the convert to ASCII.
I also try to receive the inputs from the PIC to my pc for this I have the following script inside a timer:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim rcvbytes() As Byte = subscriber.Receive(ep)
Dim translate As String
translate = System.BitConverter.ToInt32(rcvbytes, 0)
TBRcv.Text = translate
Catch ex As Exception
End Try
End Sub
ASCII.GetBytes expects a string (encoded in ASCII). Giving it an Int32 doesn’t make sense.
Check out System.BitConverter.GetBytes() and System.BitConverter.ToInt32() (for the receiving end)
So the only change you’d need to make is:
Further discussion:
What has happened is that your result (let’s say 5), is first converted to string (now it’s “5”), then ASCII.GetBytes will now return a byte array with a single value 0x35, because Asc(“5”) is 0x35.