I need help with pySerial - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I need help with pySerial (/thread-2811.html) |
I need help with pySerial - oz1sej - Apr-12-2017 I'm trying to write a program that reads from a serial port and writes to a file. But I can't make pySerial work. "import serial" at the start of the program triggers no errors despite the fact that the file serial.py is not in the directory. Shouldn't it? Line two of the introduction (can't post link, sorry) however triggers an error: >>> import serial >>> ser = serial.Serial('/dev/ttyUSB0') # open serial port If I can't even make the introduction work, I don't know what to do. Any help would be greatly appreciated :-)Moderator Larz60+: Added error tags.
RE: I need help with pySerial - j.crater - Apr-12-2017 (Apr-12-2017, 05:56 AM)oz1sej Wrote: "import serial" at the start of the program triggers no errors despite the fact that the file serial.py is not in the directory. Shouldn't it? That is ok, the location where module got installed is (considering your observation) in Python's search path, and that's all that is needed. '/dev/ttyUSB0': FileNotFoundError suggests there is no 'ttyUSB0' device in the /dev/ directory, can you verify that?I assume you are using this resource. It is generic, so you may need to modify things here and there (e.g. change port/device names, to avoid issues like you've encountered). Also, to avoid unpleasant surprises in future... when you run code that opens serial port, make sure to close it afterwards with ser.close() or you will get errors due to port already open next time try to open it. Or you can use with serial.Serial... which will close it for you after its work is done. RE: I need help with pySerial - buran - Apr-12-2017 well, the error description is quite explicit: could not open port '/dev/ttyUSB0': FileNotFoundError Den angivne sti blev ikke fundet -> The specified path was not found RE: I need help with pySerial - oz1sej - Apr-12-2017 Ah, okay, that makes sense. Only I'm on Windows, so I don't have a /dev/ directory. So how do I open a COM port? RE: I need help with pySerial - j.crater - Apr-12-2017 Windows uses 'COM1' , 'COM2' etc... You will need to open Device Manager --> Ports (COM & LPT), and see which COM port you are using. RE: I need help with pySerial - oz1sej - Apr-12-2017 I'm using COM32 (it's an Arduino). I found some code on https://petrimaki.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/ which I can't get to work either: import serial import time ser = serial.Serial('COM32', 9600, timeout=0) while 1: try: print ser.readline() time.sleep(1) except ser.SerialTimeoutException: print('Data could not be read') time.sleep(1)First I get a syntax error in the print statement, because parentheses are lacking, so I add those. Then I get the error "serin = ser.read() NameError: name 'ser' is not defined" RE: I need help with pySerial - j.crater - Apr-12-2017 COM32 sounds almost unbeliveable. Did you go to Device Manager and check ports? Is it really named COM32 there? Yeah in Python 3 print statement requires parentheses. And about NameError, you must be using different pieces of code. Because in the code you posted I don't see "serin = ser.read()" RE: I need help with pySerial - oz1sej - Apr-12-2017 Yeah, I know, it really is COM32 - https://drive.google.com/file/d/0B5XNv8mdo4GkRVU4UGRFdjFBS00 - I'm using an APC220 wireless interface. I just realised: The error isn't in my file (logserial.py) - I had a file called serial.py in my dir, so obviously that's why it failed. I renamed the file, and I had data: b'122;234\r\n b'123;234\r\n b'124;234\r\n b'125;234\r\n b'126;234\r\n ... I removed the b' and \r\n with .decode('utf-8') and everything was fine, except for a superfluous line break. But after a while, I started getting empty lines. I removed the .decode('utf-8'), and now, all I get is b'' b'' b'' b'' ... The connection is fine, because when I use another terminal program, I have nice data. Now it's only the python program, that doesn't "see" the data... RE: I need help with pySerial - j.crater - Apr-13-2017 Sorry, from the given information I couldn't think of any suggestions. Perhaps if you give me an update of your situation and attempts, I could get some ideas. RE: I need help with pySerial - Larz60+ - Apr-13-2017
It is necessary to run above test to make sure it is software vs hardware It is possible you need a driver. |