Changing baud rate doesn't allow second command

R

rjmccorkle

hi - I need to open a serial port in 9600 and send a command followed
by closing it, open serial port again and send a second command at
115200. I have both commands working separately from the python
command line but it won't work in the script. Any idea why?

import serial
from time import sleep

ser = serial.Serial(0, baudrate=9600)
status = ser.isOpen()

print "baud bitch:"
ser.isOpen()


#init to TIPY
ser.write('\x00\x00\x00\x00\xE0\xE0\xE0\xE0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x13\x00\x02\x00\xEB\xFD\xFF
\x10\x87\x02\xFE\x10\x03\x10\x87\x02\xFE\x10\x03') #hex command msg
ser.close()

sleep(5) #wait

ser = serial.Serial(0, baudrate=115200)

ser.baudrate

ser.write('\x10\x1C\x0D\x02\x0B\x0B\x02\x01\x21\x06\x00\x13\x07\x18\x7F
\x10\x03')
ser.close()

#import os
#program = 'C:\Program Files\Program.exe'+'-start'
#os.system(r'program)
 
R

Roy Smith

rjmccorkle said:
hi - I need to open a serial port in 9600 and send a command followed
by closing it, open serial port again and send a second command at
115200. I have both commands working separately from the python
command line but it won't work in the script.

I've never used this module (I assume you're talking about
http://pyserial.sourceforge.net/), but my guess is that if it works when
you type it in but not when you run it as a script, there's probably
some kind of race condition around the driver resetting the bit rate.

I would try a 1 second sleep after every call that touches the device
driver (including the serial.Serial() calls) and see if that helps.
It's a bit of a shot in the dark, but doesn't cost anything to try.

Another possibility is that the UART (I assume such things still exist?)
sends a few bits of gibberish onto the line when the bit rate changes,
and this is corrupting your data. It shouldn't, but you never know with
this stuff. If that's the case, you're probably screwed.

Just out of curiosity, what are you trying to do? Serial ports are
pretty much ancient history these days.
 
T

Thomas Rachel

Am 25.04.2011 14:46, schrieb rjmccorkle:
hi - I need to open a serial port in 9600 and send a command followed
by closing it, open serial port again and send a second command at
115200. I have both commands working separately from the python
command line but it won't work in the script. Any idea why?

What does "won't work" mean? I can't see any issues in the code.

I suppose the string sent with 9600 tells the device to change the
speed. Are you sure that it is received correctly?

Additionally, maybe you have output issues. E.g. you just write
"ser.baudrate" instead of "print ser.baudrate", so that could be a
source of behavioral differences.

#import os
#program = 'C:\Program Files\Program.exe'+'-start'
#os.system(r'program)

I suppose this doesn't belong to the problem?


Thomas
 
R

rjmccorkle

Am 25.04.2011 14:46, schrieb rjmccorkle:


What does "won't work" mean? I can't see any issues in the code.

I suppose the string sent with 9600 tells the device to change the
speed. Are you sure that it is received correctly?

Additionally, maybe you have output issues. E.g. you just write
"ser.baudrate" instead of "print ser.baudrate", so that could be a
source of behavioral differences.


I suppose this doesn't belong to the problem?

Thomas

The code is fine but it seems it won't switch baud rates using
pyserial. I have to initiate the first msg in 9600 to change the
setting of the gps
And then send the second command in 115200 because it's in
configuration mode on the unit. I can see hooking two pcs together
via null modem that the command is sent but I don't see the next msg
go across. I'll try sleeps but even w a 15 sec sleep after close
connection to allow for configuration mode to set (Which only takes
about 4 seconds) it wasn't sending the second command. Weird. My
buddy suggested I make two files for the separate calls but I wouldn't
think that would make a diff or be necessary...
 
T

Thomas Rachel

Am 25.04.2011 16:41, schrieb rjmccorkle:
The code is fine but it seems it won't switch baud rates using
pyserial. I have to initiate the first msg in 9600 to change the
setting of the gps
And then send the second command in 115200 because it's in
configuration mode on the unit.
Ok.

I can see hooking two pcs together
via null modem that the command is sent but I don't see the next msg
go across.

The receiver changes speed as well, I suppose?
I'll try sleeps but even w a 15 sec sleep after close
connection to allow for configuration mode to set (Which only takes
about 4 seconds) it wasn't sending the second command.

mmm... strange.
My buddy suggested I make two files for the separate calls but I wouldn't
think that would make a diff or be necessary...

Normally not, but it might worth trying.

Anyway, have you tried to use setBaudrate() instead of close() and
creating a new connection?

I can't see why it should make a difference, but I as well can't see why
your approach is not working, especially as it works if you do it
manually...

Maybe your target device expects to get something signalled via the
status lines?


Thomas
 
R

rjmccorkle

Am 25.04.2011 16:41, schrieb rjmccorkle:


Ok.

 > I can see hooking two pcs together


The receiver changes speed as well, I suppose?

 > I'll try sleeps but even w a 15 sec sleep after close


mmm... strange.

 > My buddy suggested I make two files for the separate calls but I wouldn't


Normally not, but it might worth trying.

Anyway, have you tried to use setBaudrate() instead of close() and
creating a new connection?

I can't see why it should make a difference, but I as well can't see why
your approach is not working, especially as it works if you do it
manually...

Maybe your target device expects to get something signalled via the
status lines?

Thomas

Ya the gps box goes into configuration and expected another carriage
return. Added \r before the second write and it works like a charm. I
only found that by having to run the second write twice to discover it
was wanting a second command from me... It worked to send the command
twice but that's sloppy cuz it just needed a \r.

Thanks for everyones help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top