If statment isn't catching Serial DataAvailable

Hello all,

I have an Arduino with a Stepper Motor that I’m controlling via SerialPort. I’d like to catch the ‘Done Moving X’ sent via the Arduino however sometimes it catches and some times it doesn’t. Any feedback would be appreciated.

Here is my code

[code]If InStr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) > 0 Then
streamIn = Me.ReadAll(Encodings.ASCII)
if streamIn.IndexOf(“Done Moving X”)>0 then
msgbox(“Huray”)
end
OutputArea.Text = OutputArea.Text + streamIn

End If[/code]

The problem is most likely that you aren’t reading a single line of data. First you check to see if the line is complete (terminated with CR/LF) then you read the entire buffer. So try changing your code to

[code]While Len(Me.Lookahead) > 0

If InStr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) > 0 Then
streamIn = Me.Read(InStr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) + 1, Encodings.ASCII)
if streamIn.IndexOf(“Done Moving X”)>0 then
msgbox(“Huray”)
end
OutputArea.Text = OutputArea.Text + streamIn

End If

Wend[/code]

Which will read one line from the buffer each time.

Thanks, @Wayne Golding, but now it seems to give me the beachball of death. But you are right that I need to wait for EOL to check the passed serial command.

Sorry it’s been a while since I looked at this stuff. Change the While statement to:

While InStr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) > 0

You can also remove the If InStr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) > 0 Then and it’s associated End If as this is checked by the loop.

Yet another user struggling with the serial port, but according to Schmitz and other luminaries here, “serial is dead” and not worth fixing. But I digress.

A key to reliable serial port operation is doing as little as possible in the DataAvailable event. Append (or do we now “addrow” to strings, too?) the new data to a buffer string, then parse the data in the event of a timer. You can fire a one-shot timer from DataAvailable, as described by Jon Ogden in this thread https://forum.xojo.com/43176-serial-communication-in-a-sequential-way or you could probably have a free-running timer instead if you prefer. The point of the timer is not to add a delay, but rather to move the parsing code out of the DataAvailable event.

Thanks for your help. But my boneheadedness hand lead me to a formatting issue. I stepped to each line of code to verify the strings were actually the same, then I notice there were off by two characters and changed encoding from ASCII to UTF8, and it seems to be working.

To be fair @Julia Truchsess this isn’t a serial port issue, but a serial communications one. The exact same problem could have occurred using a TCP Socket.

Do I ever disagree. I have a current client project that has 4 different serial devices (each with different protocols). Each uses a Serial to USB Converter and they work great (most of the time - don’t use cheap converters!). And that’s not my only serial project. Serial is alive and well in the modern world.

Edit: or maybe I originally misunderstood your comment. I disagree with THEIR take that serial is dead.

1 Like

Whether its a Xojo Serial or Xojo SerialConnection if there are bugs they should be fixed as serial is still widely used for a lot of things

1 Like

Serial is practically UART which is Alot in use today.

Yeah, I jumped to a conclusion to make my snarky point, and the short-term issue in this case turned out to be a coding bug, not a reflection of the bugginess of the control. But I’ll wager that if S. James expands his code much and/or tries to run it under Windows, he’ll be back!

Yes, thanks, Bob. When Christian recently asked for ideas for new plug-ins, I requested (via email) a serial port based on modern Windows API calls to make DataAvailable more robust. His reply was that “Serial has been dying for at least a decade” and that he wasn’t interested in working on it. I then tried to drum up support for the idea on the recent thread in this forum discussing plug-in suggestions for MBS, but most folks seemed to agree with Christian, with comments like “You should use ethernet” and “You should use wi-fi” and “You should use BlueTooth”, all of which are impractical in many applications.

As I said before, USB to serial is everywhere in my world (probably in a lot of products people assume are using HID or CDC). And yes, I do use genuine FTDI chips :slight_smile:

1 Like