Small script to check serial port sucking down system resources.
MRAB
python at mrabarnett.plus.com
Mon Dec 16 14:05:16 EST 2013
On 16/12/2013 14:31, sem2jy at gmail.com wrote:
> i am new to python and programming all together.
>
> i wrote a program to watch a serial port and look for a command.
> then send a tcp packet.
> all works great but it takes my processor load to about %25.
> not sure if there is a way to make this more efficient.
>
> import serial
>
> import socket
> HOST = '127.0.0.1' # The remote host
> PORT = 5250 # The same port as used by the server
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((HOST, PORT))
>
> ##vvvvvvvvvvvv
>
> ser = serial.Serial(
> port='COM10',\
> baudrate=9600,\
> parity=serial.PARITY_NONE,\
> stopbits=serial.STOPBITS_ONE,\
> bytesize=serial.EIGHTBITS,\
> timeout=0)
>
> print("connected to: " + ser.portstr)
>
> #this will store the line
> line = []
> c = 0
> while True:
> for c in ser.readline():
> line.append(c)
>
> if c == '1':
>
> s.send('CG 1-21 ADD 1 reserveisoff 1 \r\n')
> data = s.recv(1024)
> print 'Received', repr(data)
>
> print("one")
> line = []
> break
>
> if c == '2':
>
> s.send('PLAY 1-1 AMB.mp4 \r\n')
> data = s.recv(1024)
> print 'Received', repr(data)
> s.send('LOADBG 1-1 EMPTY MIX 30 AUTO \r\n')
> data = s.recv(1024)
> print 'Received', repr(data)
> print("two")
> line = []
>
> break
>
> s.close()
> ser.close()
>
I think the problem might be that you've set the timeout to 0, so it
doesn't block if there's no data available.
More information about the Python-list
mailing list