By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

How to listen to serial port

P: 9
Hello,

I need to write a script to listen to a serial port basically forever. The python script will be launched by another program and the input to the com port passed to the other program. As long as this other program is running, I need the python script to run and handle the com port traffic.

I have this working (thats about it),

Expand|Select|Wrap|Line Numbers
  1. import serial
  2. from scala5 import sharedvars
  3. import time
  4. scalavars = sharedvars()
  5. enable = 1
  6.  
  7.  
  8. while (enable == 1):
  9.     ser = serial.Serial('COM1', timeout = 3)
  10.     ser.write('ready')
  11.     value = ser.read(10)
  12.     if (value != ''): ser.write(value + '_ok')
  13.     scalavars.command = value
  14.     ser.close()
  15.  
but it is not very elegant or proper coding. The biggest problem with it is that the loop tries to open the com port that is already open until the timeout runs out. Any suggestions for cleaning this up?
Sep 22 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.