Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with pySerial
#1
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
Error:
Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "C:\Users\steen\AppData\Local\Programs\Python\Python36-32\lib\site-packag es\serial\serialwin32.py", line 31, in __init__     super(Serial, self).__init__(*args, **kwargs)   File "C:\Users\steen\AppData\Local\Programs\Python\Python36-32\lib\site-packag es\serial\serialutil.py", line 240, in __init__     self.open()   File "C:\Users\steen\AppData\Local\Programs\Python\Python36-32\lib\site-packag es\serial\serialwin32.py", line 62, in open     raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port '/dev/ttyUSB0': FileNotFo undError(2, 'Den angivne sti blev ikke fundet.', None, 3) >>>
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.
Reply
#2
(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.
Reply
#3
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
Reply
#4
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?
Reply
#5
Windows uses 'COM1' , 'COM2' etc...
You will need to open Device Manager --> Ports (COM & LPT), and see which COM port you are using.
Reply
#6
I'm using COM32 (it's an Arduino). I found some code on https://petrimaki.com/2013/04/28/reading...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"
Reply
#7
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()"
Reply
#8
Yeah, I know, it really is COM32 - https://drive.google.com/file/d/0B5XNv8m...GRFdjFBS00 - 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...
Reply
#9
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.
Reply
#10
  • Download putty
  • set up your baud rate, parity, stop bits etc as described in Arduino
  • set com port to com32 and see if you can communicate
if all works, then debug your software.
It is necessary to run above test to make sure it is software vs hardware
It is possible you need a driver.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020