Serial communicating in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 5
Reputation: sb3700 is an unknown quantity at this point 
Solved Threads: 0
sb3700 sb3700 is offline Offline
Newbie Poster

Serial communicating in Python

 
0
  #1
Feb 25th, 2009
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:
  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:
  1. 1ON
  2. 1LIMITS(3,0,0)
  3. iD4000
  4. 1G

or
  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Serial communicating in Python

 
0
  #2
Feb 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: sb3700 is an unknown quantity at this point 
Solved Threads: 0
sb3700 sb3700 is offline Offline
Newbie Poster

Re: Serial communicating in Python

 
0
  #3
Feb 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Serial communicating in Python

 
0
  #4
Feb 25th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: sb3700 is an unknown quantity at this point 
Solved Threads: 0
sb3700 sb3700 is offline Offline
Newbie Poster

Re: Serial communicating in Python

 
0
  #5
Feb 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: sb3700 is an unknown quantity at this point 
Solved Threads: 0
sb3700 sb3700 is offline Offline
Newbie Poster

Re: Serial communicating in Python

 
0
  #6
Feb 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Serial communicating in Python

 
0
  #7
Feb 27th, 2009
Mark as solved and give Bear of NH props.

Jeff
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Serial communicating in Python

 
0
  #8
Feb 28th, 2009
... 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Serial communicating in Python

 
0
  #9
Feb 28th, 2009
Just a note, the RS232 serial port is quickly disappearing on most newer computers. Has anyone worked with a USB port?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: sb3700 is an unknown quantity at this point 
Solved Threads: 0
sb3700 sb3700 is offline Offline
Newbie Poster

Re: Serial communicating in Python

 
0
  #10
Feb 28th, 2009
I was using a 4-port USB to RS232 converter (4 RS-232's for one USB port that is).

They work fine
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC