Urgent:Serial Port Read/Write

MRAB python at mrabarnett.plus.com
Thu May 9 11:54:34 EDT 2013


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




More information about the Python-list mailing list