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))
MsgBox("The serial port could not be opened.")
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.