Reputation: 146
I'm developing a project in VB.NET that reads from a SERIAL PORT some info. This info comes in in a pack of 4 bytes. Im able to read the data from the serial port, but what I get is just a pack of 4 numbers.
For example, my readings are:
134 0 0 4
140 0 0 6
141 0 0 5
133 0 0 8
...
The manual explains how to convert this numbers into usable data. Im able to do it over a paper but i dont know how to code this in VB.NET. I dont know how to work at a byte-level.
I attach a picture about the byte's meaning.
Upvotes: 0
Views: 2188
Reputation: 146
I got the answer, thanks to user x4rf41
Maybe need some fixes but its what I was looking for.
This is the code:
Private Sub thread_lectura_tarjeta1()
Dim RXByte As Byte 'byte recived
Dim RXPacket As List(Of Byte) = New List(Of Byte) 'each reading has 4 bytes
Dim lectura As Long = 0 'is the FINAL data
Dim COMPort As SerialPort = ensayo.get_digitalizadores(0).get_puerto_com
Dim chk_signo As Byte = 0
While (True)
lectura = 0
Do 'each package starts with a byte > 127, because is the only byte that its first bit is 0
RXByte = COMPort.ReadByte
Loop Until (RXByte > 127)
RXByte = RXByte And 127
RXPacket.Insert(0, RXByte)
RXByte = COMPort.ReadByte
RXPacket.Insert(1, RXByte)
RXByte = COMPort.ReadByte
chk_signo = RXByte And 8
RXPacket.Insert(2, RXByte And 7)
RXByte = COMPort.ReadByte
RXPacket.Insert(3, RXByte)
lectura = RXPacket.Item(0) + RXPacket.Item(1) * 128 + RXPacket.Item(2) * 16384
'checking sign
If chk_signo = 8 Then ' negative number
lectura = (lectura Xor 131071) * -1
End If
Sleep(1) 'wait 1 milisecond and read again
End While
End Sub
Upvotes: 2