Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62066 > unrolled thread

Small script to check serial port sucking down system resources.

Started bysem2jy@gmail.com
First post2013-12-16 06:31 -0800
Last post2013-12-16 20:16 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Small script to check serial port  sucking down system resources. sem2jy@gmail.com - 2013-12-16 06:31 -0800
    Re: Small script to check serial port sucking down system resources. MRAB <python@mrabarnett.plus.com> - 2013-12-16 19:05 +0000
      Re: Small script to check serial port sucking down system resources. Grant Edwards <invalid@invalid.invalid> - 2013-12-16 20:16 +0000

#62066 — Small script to check serial port sucking down system resources.

Fromsem2jy@gmail.com
Date2013-12-16 06:31 -0800
SubjectSmall script to check serial port sucking down system resources.
Message-ID<f25bc39b-2626-4019-b789-8d39f2f4772c@googlegroups.com>
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()


[toc] | [next] | [standalone]


#62093 — Re: Small script to check serial port sucking down system resources.

FromMRAB <python@mrabarnett.plus.com>
Date2013-12-16 19:05 +0000
SubjectRe: Small script to check serial port sucking down system resources.
Message-ID<mailman.4231.1387220710.18130.python-list@python.org>
In reply to#62066
On 16/12/2013 14:31, sem2jy@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.

[toc] | [prev] | [next] | [standalone]


#62098 — Re: Small script to check serial port sucking down system resources.

FromGrant Edwards <invalid@invalid.invalid>
Date2013-12-16 20:16 +0000
SubjectRe: Small script to check serial port sucking down system resources.
Message-ID<l8nn2q$mr9$1@reader1.panix.com>
In reply to#62093
On 2013-12-16, MRAB <python@mrabarnett.plus.com> wrote:
> On 16/12/2013 14:31, sem2jy@gmail.com wrote:
>
>> 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
>>

[...]

>> 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

The line above is useless -- it binds 'c' to the integer value 0, but
then you rebind 'c' to something else without ever referencing that
value.

>> 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.

That is indeed the problem.  When there's no serial data you're
spinning around in that loop as fast as your CPU can go.  Try setting
"timeout = None" when you set up the serial port.  That way the call
to ser.readline() will block (IOW won't return) until there's actually
data to be processed.  While blocked waiting for serial data, your
program won't be using up any CPU time.

Your for loop with breaks looks odd2.  Can you explain what it's
suposed to be doing?  You seem to be using the for loop to iterate
through the characters in each input line, but then breaking out of
the loop the first time you see either a '1' or '2' in each line.  Is
that what you intended?

-- 
Grant Edwards               grant.b.edwards        Yow! Quick, sing me the
                                  at               BUDAPEST NATIONAL ANTHEM!!
                              gmail.com            

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web