Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use COM and USB ports within Cygwin?

I want to send/receive data from my Arduino board with a Python script. I would like to do it using Python and its pySerial module which seems to fit my needs. So I installed Python and pySerial within cygwin (windows XP behind).

The Python script is rather straightforward:

$ cat example.py

#print "testing my COM26 port using python"

import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 26
ser
ser.open()
ser.isOpen()

However at runtime I get the following error.

$ python example.py
Traceback (most recent call last):
  File "example.py", line 9, in <module>
    ser.open()
  File "/usr/lib/python2.5/site-packages/serial/serialposix.py", line 276, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port 26: [Errno 2] No such file or directory: '/dev/com27'

Could not open port 26: [Errno 2] No such file or directory: '/dev/com27'

How do I know my Arduino is connected to port COM27?

Well, it's simple. The Arduino IDE says so, I can send and receive data from the Serial Port Monitor tool for the IDE using that port. Besides, I managed to get the hyperterminal working using that port too.

However, it seems Cygwin is not aware of such USB and COM ports:

$ ls -lah /dev
total 4,0K
.
..
fd -> /proc/self/fd
mqueue
shm
stderr -> /proc/self/fd/2
stdin -> /proc/self/fd/0
stdout -> /proc/self/fd/1

It should be mentioned that I am running this on a Dell laptop that has no classic serial COM port, just USB ports. (So I guess it's plain normal for instance that /dev/com1 does not exist.)

I don't know if I'm asking correctly, but my question is: how can I configure Cygwin so that it becomes aware of this COM27 port?

like image 504
user349188 Avatar asked May 24 '10 18:05

user349188


People also ask

Can you connect USB to COM port?

Since USB can do the same thing as many COM ports, most computers now lack serial ports. If you need one to satisfy your software, though, USB's benefits don't help you. The solution is to plug a USB to serial adapter into a USB port and set it to act as COM1.


2 Answers

Serial ports in windows are mapped to cygwin as:

COM -> /dev/ttyS

For example COM3 -> /dev/ttyS2

The example.py can be rewritten for opening COM3 as:

import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = "/dev/ttyS2"
ser
ser.open()
ser.isOpen()
like image 74
Sudeep Avatar answered Oct 06 '22 10:10

Sudeep


If Hyperterminal can access it, then it's installed as a "virtual COM port". Cygwin will let you access it as /dev/ttyS26 (called COM27 by Windows). You may still have an issue with the input blocking until a CR is received--I do. (Trying to solve that, is how I found this.)

like image 34
gbarry Avatar answered Oct 06 '22 10:10

gbarry