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


Groups > comp.lang.python > #45042

Re: Urgent:Serial Port Read/Write

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'else:': 0.03; 'parameters': 0.04; '"""': 0.07; 'indexing': 0.07; 'none,': 0.07; 'string': 0.09; '"__main__":': 0.09; '__name__': 0.09; 'function,': 0.09; 'kumar': 0.09; 'skip:# 30': 0.09; 'try:': 0.09; 'way:': 0.09; 'python': 0.11; 'def': 0.12; '(1,': 0.16; '[])': 0.16; 'addr': 0.16; 'discarded.': 0.16; 'file).': 0.16; 'files:': 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; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'ser.close()': 0.16; 'skip:[ 50': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'script.': 0.24; 'simpler': 0.24; 'script': 0.25; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'character': 0.29; 'characters': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'consisting': 0.31; 'skip:# 10': 0.33; 'except': 0.35; 'skip:s 30': 0.35; 'received:84': 0.35; 'but': 0.35; 'wrong': 0.37; 'skip:- 20': 0.37; 'list': 0.37; 'step': 0.37; 'skip:o 20': 0.38; 'writes': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'read': 0.60; 'new': 0.61; 'name:': 0.61; 'range': 0.61; 'name': 0.63; 'more': 0.64; 'header:Reply-To:1': 0.67; 'reads': 0.68; 'reply-to:no real name:2**0': 0.71; 'serial': 0.72; '(global': 0.84; 'none!': 0.84; 'reply-to:addr:python.org': 0.84; 'subject::': 0.85; 'subject:Port': 0.91; 'subject:Serial': 0.91; 'subject:Urgent': 0.96
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=JsTI8qIC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=7AxPfEIvyrUA:10 a=obvgr7mW35QA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=blP_psnV7T0A:10 a=ox8ebf87rhuYLVM2Ot8A:9 a=OhuVCvgAxIqRYY1N:21 a=teux3jmYD6RrrPRf:21 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Thu, 09 May 2013 16:54:34 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: Urgent:Serial Port Read/Write
References <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com>
In-Reply-To <1368113753.5157.YahooMailClassic@web190506.mail.sg3.yahoo.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1496.1368114871.3114.python-list@python.org> (permalink)
Lines 138
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368114871 news.xs4all.nl 15948 [2001:888:2000:d::a6]:40766
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45042

Show key headers only | View raw


On 09/05/2013 16:35, chandan kumar wrote:
> Hi all,
> I'm new to python and facing issue using serial in python.I'm facing the
> below error
>
> *    ser.write(port,command)*
> *NameError: global name 'ser' is not defined*
> *
> *
> Please find the attached script and let me know whats wrong in my script
> and also how can i read data from serial port for the  same script.
>
>
> Best Regards,
> Chandan.
>
>
> RunScripts.py
>
>
> import time
> import os
> import serial
> import glob
>
>
> ER_Address = [[0x0A,0x01,0x08,0x99,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB]]
> """
> ----------------------------------------------------------------------------------------------------------------------------
> Function Name: RunSequence
> ----------------------------------------------------------------------------------------------------------------------------
> Function Description:
> ---------------------
> A RunSequence function has Multiple calls to the RunSuite function, each call is a single testcase consisting of all the parameters
> required by the RunTesSuite.
> -----------------------------------------------------------------------------------------------------------------------------
> """
> def WriteSerialData(command):

'ser' isn't a local variable (local to this function, that is), nor is
it a global variable (global in this file).

>      ser.write(port,command)
>
> def SendPacket(Packet):
>      str = chr(len(Packet)) + Packet #Concatenation of Packet with the PacketLength
>      print str
>      WriteSerialData(str)
>
>
> def CreateFrame(Edata):

It's more efficient to build a list of the characters and then join
them together into a string in one step than to build the string one
character at a time.

Also, indexing into the list is considered 'unPythonic'; it's much 
simpler to do it this way:

     return chr(0x12) + "".join(chr(d) for d in Edata)

>          evt = chr(0x12)
>          evt = evt + chr(Edata[0])
>          for i in range (1, len(Edata)):
>              evt = evt + chr(Edata[i])
>          return evt
>
> def SetRequest(data):
>      print data
>      new = []
>      new = sum(data, [])
>      Addr = CreateFrame(new)
>      SendPacket(Addr)
>      print "SendPacket Done"
>      ReadPacket()
>
>
> def OpenPort(COMPort,BAUDRATE):
>      """
>      This function reads the serial port and writes it.
>      """
>      comport=COMPort
>      BaudRate=BAUDRATE
>      try:
>          ser = serial.Serial(
>              port=comport,
>              baudrate=BaudRate,
>              bytesize=serial.EIGHTBITS,
>              parity=serial.PARITY_NONE,
>              stopbits=serial.STOPBITS_ONE,
>              timeout=10,
>              xonxoff=0,
>              rtscts=0,
>              dsrdtr=0
>              )
>
>          if ser.isOpen():
>              print "Port Opened"
>              ser.write("Chandan")
>              string1 = ser.read(8)
>              print string1

This function returns either ser ...

>              return ser
>          else:
>              print "Port CLosed"
>              ser.close()

... or 3 ...
>              return 3
>      except serial.serialutil.SerialException:
>          print "Exception"
>          ser.close()
>
... or None!
>
>
>
>
>
> if __name__ == "__main__":
>
>      CurrDir=os.getcwd()
>      files = glob.glob('./*pyc')
>      for f in files:
>          os.remove(f)

OpenPort returns either ser or 3 or None, but the result is just
discarded.

>      OpenPort(26,9600)
>      SetRequest(ER_Address)
>      #SysAPI.SetRequest('ER',ER_Address)
>
>      print "Test Scripts Execution complete"
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Urgent:Serial Port Read/Write MRAB <python@mrabarnett.plus.com> - 2013-05-09 16:54 +0100

csiph-web