I am using pySerial in Python 3.10 to do a command exchange with a motor controller.
I send:
SN[4]\n\r
and I receive back the serial number
12345678\n
Before I go on I will say that this motor controller has a poor data sheet. It doesn’t actually say how the terminal interface works(!) so I am making a bit of this up, however largely what I’m writing works and I think once I get this oddity explained I’ll be able to work the rest out.
However - when I get the response back from the controller, the byte array when converted to a string does this:
length is: 11
21511772;
result is S #
result is #
result is 2
result is 1
result is 5
result is 1
result is 1
result is 7
result is 7
result is 2
result is ;
from the code:
print("length of serial number: " + str(len(value)))
print(value)
for i in value:
print ("result is " + i)
Where value
is the variable with the serial number in.
The code that converts the byte array from the serial port is this:
string_response = response.decode('ASCII')
From what I’ve seen of the protocol, the response I get should be as shown when the string is displayed in full (ie <12345678;> with that semicolon terminator). The same happens with other commands but they’re less conventional (ie they just return a ; with the two additional characters which I’ve marked with the # symbol).
In summary, I’m not sure why there are two additional characters at the start of the string when I try and access the elements of the string individually or try to do a comparison but these don’t display when I show the string in a more conventional way. I’m certain they are not being transmitted by the remote device as I’ve put it on a standard terminal and they’re not there.
There is always some link between the letter (the S here comes from SN…) and the command so that’s not random. It happens running through different pythons (eg Anaconda, from the Windows command line etc.).
Thanks for any comments. Perhaps now I’ve described the problem, if it’s not obvious it will be easier to post snippets which might help.