Can't get the hang of reading Com port

Discussion in 'Questions (Windows Mobile)' started by Zenerdiode, Mar 10, 2009.

  1. Zenerdiode

    Zenerdiode Active Member Licensed User

    I have a piece of calibration equipment that I'm attempting to write a desktop application to read from its Com port. The calibration equipment will either send its datastream when a button is pressed on its front panel, or when RTS is asserted. The manual states it may service up to 5 requests per second. If RTS is asserted (and remains so), it will only send one reading, it has to be cancelled and reasserted again for the next one.

    Fairly regularly I get gobbledegook in the textbox - but I'm sure its because I'm not reading the incomming stream properly. I have to use an 'OnComm' event for the times the front panel button is pressed. I've never quite understood the triggering of OnComm: instuctions in the OnComm may be being executed as more data is arriving in the buffer(?)

    Parameters are fixed at 1200,8,N,1

    'TWA1000Sim' is a simulator of the calibration equipment and behaves exactly the same. (The big 'O' button simulates the on/off switch :)) I compile this for the Device and run it on my TDS Recon connected via RS232 to the desktop. It will compile for the desktop too.
     

    Attached Files:

    • TWA.zip
      File size:
      2.2 KB
      Views:
      43
  2. Erel

    Erel Administrator Staff Member Licensed User

    The OnCom is fired only when the buffer state changes from empty buffer to buffer with some data.
    Make sure that the buffer is empty before leaving the OnCom event sub.
     
  3. agraham

    agraham Expert Licensed User

    That's correct!
    No, you can use it all the time. Just waggle RTS and OnNCom will collect the data.
    OnCom is raised when data is available. As the OS buffers the Serial Port for you it can collect more data while your OnCom code runs so you don't lose any characters. You need to realise that data won't necessarily arrive in neat ready parsed lumps. Try changing your Label1 to a great big multiline textbox and do this
    Code:
    Sub Port_OnCom
      ReceivedString=ReceivedString&Port.InputString
      TextBox1.Text = ReceivedString
    End Sub
    You will see the regular stuff coming from waggling RTS and the extra when you press the button, all arriving correctly interleaved. You just need to parse this incoming stream properly.
     
  4. Zenerdiode

    Zenerdiode Active Member Licensed User

    :eek:
    I tried that and after many, many ours of different fixes in my code and using a Comtest RS232 Line Monitor, I loaded the test application onto a computer that has a legacy RS232 Com port - instead of the USB>RS232 lead I had been using on my modern laptop - and it works perfectly. :eek:

    I know USB>RS232 ports have an increased latency, but I didn't think it would affect my simple program(!) It didn't like the RTS being changed too quickly. If I waggled RTS once every two seconds; the data read by OnCom would be fine, but increasing the sampling to once a second started to corrupt data. If the RTS was left alone and data was 'gunned' at it, the OnCom handled it well. Because my application will eventually run on a Desktop (thankfully with a legacy port) I can now sample at a rate of 500ms.

    The data returned from the measurement equipment is only a maximum of 16 bytes @ 1200 Baud per request so I've been carefull not to oversample.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice