| | |
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 |
Tag cloud for Python
accessdenied apache application argv beginner book change code color converter dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython







