Urgent:Serial Port Read/Write
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. 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.write(port,command) def SendPacket(Packet): str = chr(len(Packet)) + Packet #Concatenation of Packet with the PacketLength print str WriteSerialData(str) def CreateFrame(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 return ser else: print Port CLosed ser.close() return 3 except serial.serialutil.SerialException: print Exception ser.close() if __name__ == __main__: CurrDir=os.getcwd() files = glob.glob('./*pyc') for f in files: os.remove(f) OpenPort(26,9600) SetRequest(ER_Address) #SysAPI.SetRequest('ER',ER_Address) print Test Scripts Execution complete -- http://mail.python.org/mailman/listinfo/python-list
Re: Urgent:Serial Port Read/Write
On Fri, May 10, 2013 at 1:35 AM, chandan kumar chandan_...@yahoo.co.in 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. You're assigning to 'ser' inside OpenPort(), but then trying to use it in WriteSerialData(). You'll need to declare 'global ser' in OpenPort to make this work. Alternatively, you may want to cut down on the number of functions you have, since they're called in only one place anyway and have to share state. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Urgent:Serial Port Read/Write
On Thu, 09 May 2013 23:35:53 +0800, 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. [snip] if __name__ == __main__: CurrDir=os.getcwd() files = glob.glob('./*pyc') for f in files: os.remove(f) OpenPort(26,9600) SetRequest(ER_Address) #SysAPI.SetRequest('ER',ER_Address) print Test Scripts Execution complete What kind of 'port' is 26? Is that valid on your machine? My guess is that ser is NULL (because the open is failing, likely due to the port selection), leading to your subsequent problems. HTH.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Urgent:Serial Port Read/Write
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Urgent:Serial Port Read/Write
On Fri, May 10, 2013 at 1:35 AM, chandan kumar chandan_...@yahoo.co.in wrote: 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. Don't do this: except serial.serialutil.SerialException: print Exception ser.close() Just let it propagate up and show you a traceback. There is absolutely no value in suppressing an exception only to print an unhelpful message. ChrisA -- http://mail.python.org/mailman/listinfo/python-list