Shop OBEX P1 Docs P2 Docs Learn Events
Serial communication (two-way) between VB '05 Express and BS - Page 2 — Parallax Forums

Serial communication (two-way) between VB '05 Express and BS

2»

Comments

  • SeanTSeanT Posts: 17
    edited 2006-11-02 05:10
    OK, I did a little testing with the method you posted Jeff and it works, so I'm guessing my problem is that I'm using only one button and there is no significant delay between writing and reading, but shouldn't the VB program be able to wait in the loop while bytestoread < 2 and then continue from there? I've fixed the problem with the VB program freezing due to no data to read, so I can now keep trying to send different values...but I've got my stamp set up to beep once if X distance > Y distance, beep twice if X distance = Y distance, and beep three times if X distance < Y distance. Then, the stamp should loop and request the next point from VB and give me another beep. However, I only seem to get one result (usually the X = Y result, even if I send different values for X and Y) and then nothing. The VB program stays running, and I can click send again to get the same result. Seemingly, the stamp is getting the first point and processing it and then strange things happen. Wait, I just thought some of the points I'm sending may result in negative distances...and the stamp can't process negative values, right? I'll probably have to do the number wrestling in VB and send maybe a "P" for positive directions and "N" for negative directions along with the distances. More testing in a couple days when I have some free time. Thanks again for the help.
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2006-11-02 05:53
    SeanT,

    I have had good results with this approach... Basically VB works like a dispatcher.
    ·
    You have a main initialization section where COM ports are set, forms are created etc.
    Within the "Sub Form_Initialize()" I set up the Timer as an interrupt, setting the interval very low... 5 or less.
    This method seems to work MUCH better than waiting for an OnComm event to occur.

    Within the Timer interrupt routine "Sub Timer1_Timer()" I check to see if there is any data on the serial line.
    Something like...

    Sub Timer1_Timer()
    If RS232.InBufferCount >=4 Then                 'Check to see if buffer has at least 4 characters to read
       RS232.InputLen = 3                           'Throttle data so we ONLY read 3 Bytes
       Header$ = RS232.Input                        'Read 3 Byte Header
    NewHeader:
       If Header$ = "VB!" Then                      'The Stamp sends a 3 Byte Header "VB!" followed by a Function value
          RS232.InputLen = 1                        'Throttle data so we ONLY read 1 Byte
          Fn = Asc(RS232.Input)                     'Get Function
          Select Case Fn
                 Case 1                             'Function request 1
                      RS232.Output = "Ok"           'Send Confirmation back to Stamp ; Stamp waits for "Ok" with a 1 second timeout if not received
     
                      {Do Stuff here that corresponds to Function1 - remember to implement handshaking when necessary}
     
                 Case 2                             'Function request 2
                      RS232.Output = "Ok"           'Send Confirmation back to Stamp ; Stamp waits for "Ok" with a 1 second timeout if not received
     
                      {Do Stuff here that corresponds to Function2 - remember to implement handshaking when necessary}
    
          End Select
       Else
          RS232.InputLen = 1                        'Throttle data so we ONLY read 1 Byte
          Header$ = Mid$(Header$,2)+RS232.Input     'Attempt to Re-Sync Data if current header does not match incoming data ; Shift Bytes Left, MSByte lost
          Goto NewHeader
       End If
    End If
    End Sub
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 11/2/2006 6:03:49 AM GMT
  • SeanTSeanT Posts: 17
    edited 2006-11-03 15:30
    Great, thanks for all the ideas everyone. I'll test them this weekend and keep you updated on my findings.
Sign In or Register to comment.