943,918 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5467
  • Python RSS
Feb 25th, 2009
0

Serial communicating in Python

Expand Post »
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:
python Syntax (Toggle Plain Text)
  1. import serial, time
  2.  
  3. # the serial port connections - customise as necessary
  4. motor = serial.Serial('COM3', baudrate=9600)
  5.  
  6. # start doing stuff
  7. try:
  8. motor.write('''1ON
  9. 1LIMITS(3,0,0)
  10. iD4000
  11. 1G
  12. ''')
  13.  
  14. time.sleep(.1)
  15. print motor.read(motor.inWaiting())
  16.  
  17. finally:
  18. # in case of an error, close the serial ports - avoids them being locked
  19. 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)
  1. 1ON
  2. 1LIMITS(3,0,0)
  3. iD4000
  4. 1G

or
Python Syntax (Toggle Plain Text)
  1. 1ON
  2. 1LIMITS(3,0,*E
  3. )
  4. iD4000
  5. 1G

where *E denotes an error. This error can be in various locations.

I have no idea why this is not working
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sb3700 is offline Offline
5 posts
since Feb 2009
Feb 25th, 2009
0

Re: Serial communicating in Python

I don't know the answer but suggest two things to consider:
  1. Sending all four lines in one write() may burst data faster than the receiver can actually handle.
  2. You might check on the newlines in the string sent by the write(), they could be different from what you type manually to HyperTerm.
My first guess would be [1] based on the varying error pattern.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Feb 25th, 2009
0

Re: Serial communicating in Python

I've sent the data line at a time, and even character at a time, so I don't think 1 is the issue. (what do you mean by burst data?)

I'll try changing the newlines, but I've also used triple quoted line breaks, which is exactly the same as would be sent by hyperterminal.

Thanks anyway.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sb3700 is offline Offline
5 posts
since Feb 2009
Feb 25th, 2009
0

Re: Serial communicating in Python

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 26th, 2009
0

Re: Serial communicating in Python

Ok thanks.

The error messages are sent by the motor, which normally should return every byte sent (which it does, but sometimes with an error in a random spot).

I'll play around with pauses, and I'll try the other kind of newline.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sb3700 is offline Offline
5 posts
since Feb 2009
Feb 26th, 2009
0

Re: Serial communicating in Python

I managed to fix the problem.

By re-reading the manual, I found out that a carriage return (\r) was required to delimit a command. Using this fixed the problem.

Thanks for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sb3700 is offline Offline
5 posts
since Feb 2009
Feb 27th, 2009
0

Re: Serial communicating in Python

Mark as solved and give Bear of NH props.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 28th, 2009
0

Re: Serial communicating in Python

... and thank sb3700 for closing the loop here. Six months from now somebody else will have the identical problem and with a bit of googling will see the solution, thus reducing global aggravation.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Feb 28th, 2009
0

Re: Serial communicating in Python

Just a note, the RS232 serial port is quickly disappearing on most newer computers. Has anyone worked with a USB port?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 28th, 2009
0

Re: Serial communicating in Python

I was using a 4-port USB to RS232 converter (4 RS-232's for one USB port that is).

They work fine
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sb3700 is offline Offline
5 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Creating zip File Equivalent to Java jar
Next Thread in Python Forum Timeline: Python26 with wxPython, wxWidgets(if need?), XRCed: Where to download compatibles?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC