"/dev/ttyUSB0" equivalent in windows
Asked Answered
M

1

6

In Linux I can write and read data from an USB device by calling C's fopen('/dev/ttyUSB0', 'rw')

Specifically what is the equivalent of the directory "/dev/ttyUSB0" in windows I'd like to do the same in windows for COM3.

Martguerita answered 9/4, 2020 at 0:25 Comment(0)
T
3

If you are using a runtime environment like Cygwin or msys-2.0.dll which provides a POSIX compatibility, you can run ls /dev/tty* in the shell provided by the environment to see what kind of entries you get. It looks like COM3 would correspond to /dev/ttyS2, at least with msys-2.0.dll.

If you are writing a native Windows program, you should be able to open "COM3" with fopen or CreateFile. Using CreateFile is probably better than fopen because it returns a native Windows handle which allows you to use the SetCommTimeouts and SetCommState API functions. COM ports higher than COM9 need a prefix of \\.\, which is written as "\\\\.\\" in C because we need to escape the backslashes.

Toowoomba answered 9/4, 2020 at 0:51 Comment(1)
Please also note that using "\\\\.\\" is always possible for devices (also for COM1-COM9): CreateFile("\\\\.\\COM1", ...)Squinty

© 2022 - 2024 — McMap. All rights reserved.