Search code examples
cubuntuusbttycom-port

"/dev/ttyUSB0" equivalent in windows


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.


Solution

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