I m trying to send data to an hplc pump via the serial port using python and pyserial. I tested the cable and the pump under linux a gentoo derivative, where it.
Serial Port Communication in Python
pyserial is a python module that allows one to access serial port. It’s easy to use and one can start test serial port communication in minutes. If your machine doesn’t have pyserial module installed, you can install the module using easy_install. #!/usr/bin/python import serial, time #initialization and open the port #possible timeout values: # 1. None: wait forever, block call # 2. 0: non-blocking mode, return immediately # 3. x, x is bigger than 0, float allowed, timeout block call ser = serial.Serial() ser.port = "/dev/ttyUSB0" #ser.port = "/dev/ttyS2" ser.baudrate = 9600 ser.bytesize = serial.EIGHTBITS #number of bits per bytes ser.parity = serial.PARITY_NONE #set parity check: no parity ser.stopbits = serial.STOPBITS_ONE #number of stop bits #ser.timeout = None #block read ser.timeout = 0 #non-block read #ser.timeout = 2 #timeout block read ser.xonxoff = False #disable software flow control ser.rtscts = False #disable hardware (RTS/CTS) flow control ser.dsrdtr = False #disable hardware (DSR/DTR) flow control ser.writeTimeout = 2 #timeout for write try: ser.open() except Exception, e: print "error open serial port: " + str(e) exit() if ser.isOpen(): try: ser.flushInput() #flush input buffer, discarding all its contents ser.flushOutput()#flush output buffer, aborting current output #and discard all that is in buffer #write data ser.write("AT+CSQ=?\\x0D") print("write data: AT+CSQ=?\\x0D") time.sleep(0.5) #give the serial port sometime to receive the data numOfLines = 0 while True: response = ser.readline() print("read data: " + response) numOfLines = numOfLines + 1 if (numOfLines >= 5): break ser.close() except Exception, e1: print "error communicating...: " + str(e1) else: print "cannot open serial port " The program send out AT commands to query modem signal strength. If you want to know more about modem AT commands, refer to post here . If you set the timeout as 2, the readline() method is blocking call with timeout, the output will be as below,
This is a step-by-step guide to using the serial port from a program running under Linux; it was written for the Raspberry Pi serial port with the Raspbian Wheezy distribution. However, the same code should work on other systems.
Serial port programming
This is a step-by-step guide to using the serial port from a program running under Linux; it was written for the Raspberry Pi serial port with the Raspbian Wheezy.
Oct 18, 2013 Python Serial Port Extension download. Python Serial Port Extension 2013-10-18 :20 free download. Python Serial Port Extension Multiplatform Serial.
I m trying to access a serial port with Python 2.6 on my Raspberry Pi running Debian. My script named serial.py tries to import pySerial: import serial ser serial.
Set your Twitter account name in your settings to use the TwitterBar Section.