Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'socket': 0.07; 'subject:script': 0.09; 'python': 0.11; 'wrote': 0.14; '#this': 0.16; 'command.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'port))': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'ser.close()': 0.16; 'subject:port': 0.16; 'tcp': 0.16; 'timeout': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'programming': 0.22; 'import': 0.22; 'print': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'processor': 0.24; 'together.': 0.24; 'header:In-Reply-To:1': 0.27; 'host': 0.29; "doesn't": 0.30; 'mix': 0.30; 'skip:# 10': 0.33; 'problem': 0.35; 'skip:s 30': 0.35; 'received:84': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'remote': 0.38; 'server': 0.38; 'auto': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'break': 0.61; 'subject: ': 0.61; 'new': 0.61; "you've": 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'great': 0.65; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'serial': 0.72; 'reply-to:addr:python.org': 0.84; 'subject:check': 0.84; 'subject:system': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=BtWkn+n5 c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=baqLi5_GPZIA:10 a=iuU7UyGdnLIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=qm2WL7zdENkA:10 a=pGLkceISAAAA:8 a=o_2qCtMl01TXtQyW4JQA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Mon, 16 Dec 2013 19:05:16 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Small script to check serial port sucking down system resources. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387220711 news.xs4all.nl 2838 [2001:888:2000:d::a6]:53419 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:62093 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.