| | |
Serial communicating in Python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
Hey
I am trying to use Python to control a motor over RS232 (serial ports).
It is connected to COM3, and the connection settings are correct (baud=9600, etc).
I am using pySerial in Python 2.5.
My code is:
If I type exactly the same thing into HyperTerminal/Teraterm it works fine.
However. the motor does not move, but I get varying outputs such as:
or
where *E denotes an error. This error can be in various locations.
I have no idea why this is not working
I am trying to use Python to control a motor over RS232 (serial ports).
It is connected to COM3, and the connection settings are correct (baud=9600, etc).
I am using pySerial in Python 2.5.
My code is:
python Syntax (Toggle Plain Text)
import serial, time # the serial port connections - customise as necessary motor = serial.Serial('COM3', baudrate=9600) # start doing stuff try: motor.write('''1ON 1LIMITS(3,0,0) iD4000 1G ''') time.sleep(.1) print motor.read(motor.inWaiting()) finally: # in case of an error, close the serial ports - avoids them being locked motor.close()
If I type exactly the same thing into HyperTerminal/Teraterm it works fine.
However. the motor does not move, but I get varying outputs such as:
Python Syntax (Toggle Plain Text)
1ON 1LIMITS(3,0,0) iD4000 1G
or
Python Syntax (Toggle Plain Text)
1ON 1LIMITS(3,0,*E ) iD4000 1G
where *E denotes an error. This error can be in various locations.
I have no idea why this is not working
I don't know the answer but suggest two things to consider:
- Sending all four lines in one write() may burst data faster than the receiver can actually handle.
- You might check on the newlines in the string sent by the write(), they could be different from what you type manually to HyperTerm.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
That's cool. I didn't even know there was a PySerial module.
First, the error messages are confusing. Are those the result of the print motor.read(motor.InWaiting()) command?
Second, just to back up Bear's point:
Since the motor is known good, and since hyperterminal works, it therefore follows that PySerial isn't sending the data in the same manner as hyperterminal.
One possibility ("burst data") is that the motor needs to pause, even if a millisecond, after each line. In that case, manually entering into hyperterminal might provide that slight pause that the automated process in Python does not.
The solution there would be to read the motor after each line to make sure that you are getting a 'ready' status.
The other possibility is that the sequence of characters is different. A common culprit is the newline. On some systems, return is 0x0A ('\n'). On others, return is TWO characters 0x0A 0x0D ('\n\r')
So it could be that PySerial is sending a different kind of newline from hyperterminal.
Without knowing more about your motor (and PySerial), I couldn't say how to test for those problems exactly, but those are the problems I would test for.
Jeff
First, the error messages are confusing. Are those the result of the print motor.read(motor.InWaiting()) command?
Second, just to back up Bear's point:
Since the motor is known good, and since hyperterminal works, it therefore follows that PySerial isn't sending the data in the same manner as hyperterminal.
One possibility ("burst data") is that the motor needs to pause, even if a millisecond, after each line. In that case, manually entering into hyperterminal might provide that slight pause that the automated process in Python does not.
The solution there would be to read the motor after each line to make sure that you are getting a 'ready' status.
The other possibility is that the sequence of characters is different. A common culprit is the newline. On some systems, return is 0x0A ('\n'). On others, return is TWO characters 0x0A 0x0D ('\n\r')
So it could be that PySerial is sending a different kind of newline from hyperterminal.
Without knowing more about your motor (and PySerial), I couldn't say how to test for those problems exactly, but those are the problems I would test for.
Jeff
![]() |
Similar Threads
- Serial Port Sniffer (Python)
- send raw data via serial (Python)
Other Threads in the Python Forum
- Previous Thread: Creating zip File Equivalent to Java jar
- Next Thread: Python26 with wxPython, wxWidgets(if need?), XRCed: Where to download compatibles?
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython







