Shop OBEX P1 Docs P2 Docs Learn Events
Prop Communicating with VB2010 - Page 2 — Parallax Forums

Prop Communicating with VB2010

2»

Comments

  • eagletalontimeagletalontim Posts: 1,399
    edited 2013-11-22 19:52
    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.
    ' Prop Code
            User_Setting7 := 123456
            send(User_Setting7)
            pause(50)
    
    PUB send(myvar)
        serial.tx(myvar >> 24)
        serial.tx(myvar >> 16)
        serial.tx(myvar >> 8)
        serial.tx(myvar)
    
        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.
  • ozpropdevozpropdev Posts: 2,792
    edited 2013-11-22 20:18
    Is the encoding property set as in post #7?
  • eagletalontimeagletalontim Posts: 1,399
    edited 2013-11-22 20:40
    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?
  • ozpropdevozpropdev Posts: 2,792
    edited 2013-11-22 20:56
    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!
  • Mike GMike G Posts: 2,702
    edited 2013-11-23 08:08
    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.
    PUB Main | unitTest
      serial.Start(31, 30, %0000, 115_200)
      waitcnt(cnt + (1 * clkfreq))
    
      [COLOR="#FF0000"]unitTest := false[/COLOR]
    

    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
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-11-23 08:27
    Hi, Uint64 is the data type you need.

    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.
    e := d << 24 | c << 16 | b << 8 | a
    

    Jeff T.
  • Mike GMike G Posts: 2,702
    edited 2013-11-23 08:47
    That was a good catch by ozpropdev and something that can be fixed very easily by storing and retrieving your longs a little differently.
    Endianness must be defined and understood by both nodes. It's not something you want to throw at the wall and see if it sticks.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2013-11-24 17:43
    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)
  • ozpropdevozpropdev Posts: 2,792
    edited 2013-11-24 18:13
    ... 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.
Sign In or Register to comment.