I am writing a python project to control a device with PyVisa over a USB cable. I am having a lot of success. I noticed that the device datasheet supported 9600, 19200, 38400, 57600, 115200, 230400, 460800 baud rates so I created some functions for changing the baud rate.
But I wanted a nifty way to detect which baud rate the device is set to so I can automatically connect to it without magically knowing what baud the device is on. The device "remembers" the last baud you set it to - it does not reset to a default baud on power loss. The device seems like it doesn't respond at all if you have the wrong baud rate set. So, given a random baud rate, I cycle through all the supported ones and send a short command. If the device answers, THAT'S the baud rate currently set and I use that to talk to the device more.
However, during stress testing I noticed that when the device was randomly set on 38400 baud and I went to detect it, my program would fail to detect that baud rate. (The device wouldn't answer a call on this baud.) When I removed it from the list of supported bauds, the device worked fine.
Is there something about 38400 baud rate that makes it more unstable than other bauds?
I also thought that this use-case was interesting. If you are having problems like this, try dropping support for certain bauds and see if that fixes your errors. Hope it helps.
EDIT: The code really doesn't matter but there was a request for it so...
import visa
BAUDRATES = [
9600,
19200,
38400,
57600,
115200,
230400,
460800,
]
rm = visa.ResourceManager('@py')
dev = rm.open_resource('ASRLCOM7:INSTR')
for i in range(1, 101): # run 100 tests
# select a random baud rate from the list
rand_baud = BAUDRATES[randrange(0,len(BAUDRATES))]
dev.write('SETBAUD' + str(rand_baud)) # Device response with 'OK'
print('The random baud was {0}'.format(rand_baud))
# now detect the random baud by sending commands to the instrument and looking for a response
detected_baud = None
for baudrate in BAUDRATES:
dev.baud_rate = baudrate # sets the com port baud rate
resp = dev.query('GETINFO')
if resp != '':
detected_baud = baudrate
break
print('The detected baud was {0}'.format(detected_baud))
This code is abbreviated from the real code but the idea is the same. Just loop through, send a command, and listen for a response. The instrument will not send a response over the com port if a message is sent with the incorrect baud or the command is unintelligible. This detection method ALWAYS failed on 38400 and never fails on any other baud rate. The only way in which this method can fail is that it never receives a response on ANY baud, which is exactly what it was doing. This makes me think maybe there is a clock mismatch only at that baud rate or similar. I was curious to know if certain bauds have higher rate of errors than others.