Since the length of my data being sent will not always be the same, I am trying to make this as "modular" as possible. Here is the code I am trying based on sample codes from the given threads and the replies to this thread.
Public Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
SerialPort1.ReadTimeout = 500
Dim bytes2read = SerialPort1.BytesToRead
Dim Character As UInt32
Dim buff(4) As Byte
If bytes2read >= 4 Then
Dim len = SerialPort1.Read(buff, 0, 4)
For i = 0 To 3
Character = BitConverter.ToUInt32(buff, 0)
If Character.ToString <> Strings.Chr(10) Then ' otherwise double space
Label20.Text = Label20.Text & " " & Character ' add to the rich text box
End If
Next i
status_txt.Text = buff(0)
End If
End Sub
As of right now, I can't seem to figure out how to convert the sent 4 bytes from the prop to the VB program into the proper value "123456" and display it on the Label20. I am not even sure the 4 bytes were sent properly.
No, it was not, but I decided since the Prop to VB program was working properly without a glitch, and the only thing that was not working was sending data from the PC to the Prop, I decided to leave the original code and only work on the PC to Prop data sending functions.
Here is what I have right now :
' on button press do this :
send2prop(123456)
Public Sub send2prop(ByVal mydata As Integer) ' also tried UInt64 and get same results
Dim bytes() As Byte = BitConverter.GetBytes(mydata)
If port_closed <> 1 Then
SerialPort1.Write(bytes, 0, 4)
Else
pop_msg = MsgBox("Failed to send data to Shift Box! Please connect first", vbOK)
End If
End Sub
User_Setting7 := getValue
serial.dec(User_Setting7)
serial.tx(10)
pause(50)
PUB getValue | a, b, c, d, e
a := serial.rx
b := serial.rx
c := serial.rx
d := serial.rx
e := a << 24 | b << 16 | c << 8 | d
return e
The returned data from the Prop is 172024321 which is not quite right. Any suggestions?
The returned data from the Prop is 172024321 which is not quite right. Any suggestions?
That value in HEX is 0A40E201 ( Notice the 0A byte (10) )
123456 in HEX is 0001E240
Your bytes are out of position and in reverse order.
Edit: If the 4 bytes of data from the Prop happens to contain a 0A byte (10 dec) , your code will see this
as your "sync" character and not as part of your data!
I'll give this another shot due to your PM. The attached VB 2010 and SPIN projects are designed as a learning tool. This is not a full solution but darn close. This solution does NOT send ASCII data!
Packet Format
[Start Byte][Start Byte][Number of bytes][Command][Payload bytes[1..n][Checksum]
The process is very simple.
The VB program sends a well formatted packet.
The Prop code waits for the two start bytes then buffers the entire packet in memory using the length byte.
Prop code processes the buffered data.
The Spin project has two modes, Unit test and not unit test. Set unitTest to true and PST to see the results. Unit test mocks the packet reception and lets you play with the packet processing logic.
With unitTest := true use the VB console to send a packet. The packet minus the two start bytes are echoed to the VB console. There are a few thing left for you to do; 1) Properly format the packet on the VB side. 2) Do whatever project specific processing on the Prop side
I looked at your code in the last two posts and it's starting to look really good.
That was a good catch by ozpropdev and something that can be fixed very easily by storing and retrieving your longs a little differently.
I don't have the time right now but later I would like to run this and see how it works. In the meantime there are a couple of things you can do in Visual Basic or Prop to rearrange the byte order.
If you think the long is being stored and read correctly by the Prop but being displayed wrongly in VB you can use "Array.Reverse" in the datareceived handler
Dim buff(4) As Byte
If bytes2read >= 4 Then
Dim len = SerialPort1.Read(buff, 0, 4)
Array.Reverse(buff)
If you think the long is being sent wrongly by VB you can use "Array.Reverse" in the send2prop routine.
Dim bytes() As Byte = BitConverter.GetBytes(mydata)
If port_closed <> 1 Then
Array.Reverse(bytes)
If you think the value is stored in the Prop wrongly then you can reverse the way the Prop stores the bytes in the long.
I am out of town right now for a week so downloading files if a pita since i only have internet on my phone. I did get the 2 files downloaded posted above, but i dont see where i can send a specific number via a 4 byte array to the prop and it convert it to a long and send it back to the pc with serial.dec(number)
... i dont see where i can send a specific number via a 4 byte array to the prop and it convert it to a long and send it back to the pc with serial.dec(number)
Post #31 shows the easiest text based protocol to implement.
Use the same protocol at the PC(VB) end as well.
I can't think of a simpler technique.
Comments
As of right now, I can't seem to figure out how to convert the sent 4 bytes from the prop to the VB program into the proper value "123456" and display it on the Label20. I am not even sure the 4 bytes were sent properly.
Here is what I have right now :
The returned data from the Prop is 172024321 which is not quite right. Any suggestions?
That value in HEX is 0A40E201 ( Notice the 0A byte (10) )
123456 in HEX is 0001E240
Your bytes are out of position and in reverse order.
Edit: If the 4 bytes of data from the Prop happens to contain a 0A byte (10 dec) , your code will see this
as your "sync" character and not as part of your data!
Packet Format
The process is very simple.
The Spin project has two modes, Unit test and not unit test. Set unitTest to true and PST to see the results. Unit test mocks the packet reception and lets you play with the packet processing logic.
With unitTest := true use the VB console to send a packet. The packet minus the two start bytes are echoed to the VB console. There are a few thing left for you to do; 1) Properly format the packet on the VB side. 2) Do whatever project specific processing on the Prop side
I looked at your code in the last two posts and it's starting to look really good.
That was a good catch by ozpropdev and something that can be fixed very easily by storing and retrieving your longs a little differently.
I don't have the time right now but later I would like to run this and see how it works. In the meantime there are a couple of things you can do in Visual Basic or Prop to rearrange the byte order.
If you think the long is being stored and read correctly by the Prop but being displayed wrongly in VB you can use "Array.Reverse" in the datareceived handler
If you think the long is being sent wrongly by VB you can use "Array.Reverse" in the send2prop routine.
If you think the value is stored in the Prop wrongly then you can reverse the way the Prop stores the bytes in the long.
Jeff T.
Post #31 shows the easiest text based protocol to implement.
Use the same protocol at the PC(VB) end as well.
I can't think of a simpler technique.