Shop OBEX P1 Docs P2 Docs Learn Events
Connecting to Serial Port with python — Parallax Forums

Connecting to Serial Port with python

bomberbomber Posts: 297
edited 2013-09-29 09:12 in General Discussion
Recently I have been trying to use a Python program to send and receive data over USB (similar in function to the Parallax Serial Terminal). The example that I am modifying was originally designed for the arduino on a Linux OS, but I am trying to get this to work for my Propeller on a Windows machine. I am trying to find what the USB com port's device's name is (usually /dev/<device-name>) but haven't found any useful tutorials online (either outdated or for Linux). The code on the propeller (which is connected to COM7) is very straightforward. It just repeatedly sends a message over the Serial port once every second.
CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
OBJ   
  pst  : "Parallax Serial Terminal"
  time : "Timing"   
PUB PST_test 
  pst.Start(115_200)
  repeat
     pst.Str(String(" Test "))
     time.pause(1000)
My python code is rather simple too. It imports the module PySerial, initializes the com port, and infinitely prints any incoming serial messages to the shell.
import serial
BAUD = 115200
ser = serial.Serial('/dev/tty0', BAUD)
while True:
    print(ser.readline())
First I turn on my PropBOE running the aforementioned code snippet. Then I run the python code and get the following error:
Traceback (most recent call last):
File "C:/Python33/Doc/SerialTest.py", line 2, in <module>
ser = serial.Serial('/dev/tty0', 115200)
File "C:\Python33\lib\site-packages\serial\serialwin32.py", line 31, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python33\lib\site-packages\serial\serialutil.py", line 261, in __init__
self.open()
File "C:\Python33\lib\site-packages\serial\serialwin32.py", line 59, in open
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port /dev/tty0: [WinError 3] The system cannot find the path specified.

I have looked online and have found several possible replacements for /dev/tty0, and none of them have worked. I was not able to find a way to list the connected devices. Does anyone know what I am doing wrong?

Comments

  • dgatelydgately Posts: 1,630
    edited 2013-09-29 08:45
    bomber,

    On Windows, you actually do use use the COM port designation...

    Try
    ser = serial.Serial('COM7', BAUD, timeout=1)
    



    dgately
  • bomberbomber Posts: 297
    edited 2013-09-29 08:56
    Thanks Dgatley! It works, and the Prop recognizes the connection!
  • dgatelydgately Posts: 1,630
    edited 2013-09-29 09:12
    Good to hear that it works!

    I'm used to always converting the other way around... Whenever I see 'COMn' in example code, I always know to use the '/dev/...' format :thumb:


    dgately
Sign In or Register to comment.