Assigning label value contents of serial readall syntax

I have a serial device that I am playing with, expanding on the serial port tutorial posted to U tube.

If I send a command “XP?” to the device, it responds which its ‘position’ in hex. (It is a motion controller).

If I send the command manually in my app, it works and I get a response. I wanted to create a Timer that sends this command and then changes a pair of label.Value, as want continuous position reporting (well, every 10 seconds per the Timer, just to play with it).

I created the Timer and added some code. I get the response in my TextArea no problem, but this snippet of mine doesn’t change the label to the response value and it has to be something dumb. Instead of changing the label.Value to the response, it changes it to “”.

In my timer event I have

If Serial1.Open Then Serial1.Write("XP?") + Chr(13) xpLabel.Value = Str(Serial1.Readall(Encodings.ASCII)) Serial1.Write("YP?") + Chr(13) ypLabel.Value = Str(Serial1.Readall(Encodings.ASCII)) Else MsgBox("The serial port could not be opened.") End If

My logic is I send “XP?” plus the EndOfLine and then assign xpLabel.Value to be the contents of the ReadAll that follows. Then turn around and do the same thing with the YP? plus EndOfLine command sent to the serial device. Even if the timing were off (eg I read before there is a response), I should at least get previous values assigned to the labels, but I just get “”. I have tried it as Str() and without, same effect.

As mentioned, my TextArea aka RecWin does get the hex response as shown in my posted picture and it updates with each trigger of the timer so the Serial1Write portion of the Timer works fine. But while the text area gets updated as expected the labels are made blank and kept blank. By default I gave xpLabel.Value as XP and ypLabel.Value as YP so when the app starts, I see XP and YP. As soon as the Timer triggers, the labels change from XP to “” and YP to “” (they become blank).

Data Available event for Serial1 if it matters.

RecWin.Text = RecWin.Text + Me.Readall(Encodings.ASCII) 
RecWin.VerticalScrollPosition = 9999999 'Keeps vert scroll position at the bottom

I was hoping that (Serial1.Readall(Encodings.ASCII)) was a command I could run from anywhere at any time.

In other news, I couldn’t get the new 2019r2 Serial Connection to work at all (no connection, some syntax issues, etc), so I gave up on that (not enough documentation for me to get it working), which is why I am Super Serial.

the send followed immediately by a read probably happens faster than the device actually responds

I would

  1. have the timer send the initial XP? and now disable the timer
  2. implement the various events for the serial controls so you get an event driven response from the device
  3. when you have read the response from the XP? THEN send YP? and again use the events to respond to the data the device sends back
  4. once you have read the YP? response NOW re-enable the timer and have it start the whole sequence again

Oh, in reading your response I think I know what is happening.

The Data Available is doing it’s separate ReadAll, and once that ‘as soon as anything is received’ event driven ReadAll happens the buffer is emptied. So by the time my little Timer wants to read, it was already read the instant before so there is nothing left to read.

–Edit. That was it. Commented out the data available snippets and the labels started working.