开发者

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 开发者_JAVA百科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?


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


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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜