12

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?

4
  • Why Cygwin? PySerial supports Win32 natively.
    – Yann Ramin
    May 24, 2010 at 18:22
  • because it's so easy install packages and i had other to install as well. will try the python win32 native then i guess
    – user349188
    May 24, 2010 at 18:29
  • ok. tried that (native python on windows + pyserial native windows installer) and the same code works as expected... weird. but i really need to understand why using cygwin doesn't work here i have the feeling it's a cygwin problem, not anything related to python i guess
    – user349188
    May 24, 2010 at 19:06
  • 2
    It is a cygwin problem - it doesn't appear to map COM ports to the "/dev" namespace. I am not sure its capable of doing that.
    – Yann Ramin
    May 24, 2010 at 19:13

5 Answers 5

12

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.)

4
  • Did you ever solve it? I am just about to attempt to use a serial port from cygwin. Preferably using some sort of terminal software for it...
    – Kaos
    May 25, 2012 at 14:34
  • I have never solved that one. And I've tried some fairly exotic things. The word on the street (Cygwin-related forums) seems to be that it isn't possible.
    – gbarry
    May 29, 2012 at 3:34
  • 2
    OK, I found a blog post, which does what I want, almost. I can tweak the missing pieces. The comm stuff works nice! plunk.org/~grantham/cgi-bin/blog.cgi?id=00015
    – Kaos
    May 30, 2012 at 8:28
  • 1
    @Kaos. Thanks for the link. I've been trying to get screen working within cygwin with a serial port, without success. serial appears to be working great!
    – DaV
    Sep 18, 2014 at 12:18
6

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()
4

My fav is

socat.exe `tty`,raw,echo=0 /dev/ttyS15,raw,echo=0,setsid,sane

it is COM16 (already setup by Windows to 115200, noparity, no-flow, 8b)

you need:

tty and socat


socat.exe - /dev/ttyS15,raw,echo=0,setsid,sane

Probably will also work (no tty package then), or just simply specify the dev node of the current terminal (might be tricky without tty package)

tty - print the file name of the terminal connected to standard input

my fav links:

  1. Using socat for raw serial connection
  2. https://superuser.com/questions/123790/socat-and-rich-terminals-with-ctrlc-ctrlz-ctrld-propagation

U have better options? Then please refine.

U can use:

stty -F /dev/ttyS15 115200 cs8

to set ur COM

TJ

1

If you are using a laptop without a COM port, you can't open this port and start make operations.

In Windows, there is a function to open this port and change the state of some line (RST), read states and make the transmission. For an LPT port you can't do it in Windows, you have to use some library. For USB it is also problem, you must know the device connected to USB.

0

I found Brad Grantham's tool VERY useful and have been using it extensively at work. It is so easy to build and use.

I am just posting here to mention that I fixed a bug in it where it would exit if you typed tilde, some other text, then a dot. So for example typing:

vim ~/.tmux.confwould eject you from the serial session.

You can find it here: https://github.com/lime45/serial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.