pyserial

Python to automatically select serial ports (for Arduino)

懵懂的女人 提交于 2019-11-27 06:55:36
Currently the python program must know which port a device (Arduino) is on before Python can communicate the device. Problem: Whenever the device is plugged out and back in, its COM port changes, so the correct serial port must be given to Python again for it to find the device. How can Python (using pySerial ) automatically search for the correct serial port to use? Is it possible for python to correctly identify the device on a serial port as an Arduino? Use the following code to see all the available serial ports: import serial.tools.list_ports ports = list(serial.tools.list_ports.comports(

How can I fix “[Error 6] The handle is invalid.” with PySerial

大憨熊 提交于 2019-11-27 06:54:17
问题 I'm trying to connect to my phone from my Windows 7 PC using PySerial with the following code: import wmi import serial c = wmi.WMI() modem = c.query("SELECT * FROM Win32_POTSModem").pop() ser = serial.Serial(modem.AttachedTo, modem.MaxBaudRateToSerialPort) try: ser.write('at \r\n') print ser.readline() finally: ser.close() But get the following error on the write call: Traceback (most recent call last): File "D:\Alasdair\Documents\Python Scripts\Phone Interface\test.py", line 14, in <module>

Serial import python

徘徊边缘 提交于 2019-11-27 06:00:39
问题 I"m trying to use pyserial. When I do the following script. import serial ser= serial.serial("COM5", 9600) ser.write("Hello worldn") x = ser.readline() print(x) Error code: c:\Python27>python com.py Traceback (most recent call last): File "com.py", line 2, in <module> ser= serial.serial("COM5", 9600) AttributeError: 'module' object has no attribute 'serial' I read a suggestion and changed it to: from serial import serial ser= serial.serial("COM5", 9600) ser.write("Hello worldn x = ser

pySerial write() won't take my string

▼魔方 西西 提交于 2019-11-27 03:54:21
问题 Using Python 3.3 and pySerial for serial communications. I'm trying to write a command to my COM PORT but the write method won't take my string. (Most of the code is from here Full examples of using pySerial package What's going on? import time import serial ser = serial.Serial( port='\\\\.\\COM4', baudrate=115200, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS ) if ser.isOpen(): ser.close() ser.open() ser.isOpen() ser.write("%01#RDD0010000107**\r") out = ''

Using PySerial is it possible to wait for data?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 01:20:36
问题 I've got a Python program which is reading data from a serial port via the PySerial module. The two conditions I need to keep in mind are: I don't know how much data will arrive, and I don't know when to expect data. Based on this I have came up with the follow code snippets: #Code from main loop, spawning thread and waiting for data s = serial.Serial(5, timeout=5) # Open COM5, 5 second timeout s.baudrate = 19200 #Code from thread reading serial data while 1: tdata = s.read(500) # Read 500

pySerial 2.6: specify end-of-line in readline()

五迷三道 提交于 2019-11-26 22:42:10
I am sending commands to Eddie using pySerial. I need to specify a carriage-return in my readline, but pySerial 2.6 got rid of it... Is there a workaround? Here are the Eddie command set is listed on the second and third pages of this PDF. Here is a backup image in the case where the PDF is inaccessible. General command form: Input: <cmd>[<WS><param1>...<WS><paramN>]<CR> Response (Success): [<param1>...<WS><paramN>]<CR> Response (Failure): ERROR[<SP>-<SP><verbose_reason>]<CR> As you can see all responses end with a \r . I need to tell pySerial to stop. What I have now: def sendAndReceive(self,

PySerial non-blocking read loop

半腔热情 提交于 2019-11-26 17:40:47
I am reading serial data like this: connected = False port = 'COM4' baud = 9600 ser = serial.Serial(port, baud, timeout=0) while not connected: #serin = ser.read() connected = True while True: print("test") reading = ser.readline().decode() The problem is that it prevents anything else from executing including bottle py web framework. Adding sleep() won't help. Changing "while True"" to "while ser.readline():" doesn't print "test", which is strange since it worked in Python 2.7. Any ideas what could be wrong? Ideally I should be able to read serial data only when it's available. Data is being

Disable DTR in pyserial from code

核能气质少年 提交于 2019-11-26 17:24:18
问题 I'm trying to use pyserial to send data to an arduino. But when I open the COM port it sets DTR low and resets the board. However, I have my arduino code setup such that I have to put it into serial receive mode by holding two buttons for 1 second. I would rather not have to do the serial input on boot of the arduino, if possible. Apparently you can modify serialWin32.py file, changing the line that reads: self._dtrState = win32.DTR_CONTROL_ENABLE to: self._dtrState = win32.DTR_CONTROL

Listing available com ports with Python

混江龙づ霸主 提交于 2019-11-26 15:45:52
I am searching for a simple method to list all available com port on a PC. I have found this method but it is Windows-specific: Listing serial (COM) ports on Windows? I am using Python 3 with pySerial on a Windows 7 PC. I have found in the pySerial API ( http://pyserial.sourceforge.net/pyserial_api.html ) a function serial.tools.list_ports.comports() that lists com ports (exactly what I want). import serial.tools.list_ports print(list(serial.tools.list_ports.comports())) But it seems that it doesn't work. When my USB to COM gateway is connected to the PC (I see the COM5 in the Device Manager),

pyserial - How to read the last line sent from a serial device

左心房为你撑大大i 提交于 2019-11-26 15:19:12
问题 I have an Arduino connected to my computer running a loop, sending a value over the serial port back to the computer every 100 ms. I want to make a Python script that will read from the serial port only every few seconds, so I want it to just see the last thing sent from the Arduino. How do you do this in Pyserial? Here's the code I tried which does't work. It reads the lines sequentially. import serial import time ser = serial.Serial('com4',9600,timeout=1) while 1: time.sleep(10) print ser