What is the Windows equivalent to the capabilities defined in sys/select.h and termios.h
Asked Answered
G

3

24

I have an application in linux, which is compiled successfully. I want to run the same program in windows.

But compilation produces the following errors related to header files.

  1. Cannot find sys/select.h
  2. Cannot find termios.h

How can I fix this?

Gaines answered 1/6, 2009 at 6:36 Comment(0)
G
38

The Windows API is structurally and stylistically very different from the blend of system calls and library routines provided by any flavor of Unix.

termio.h

Windows does terminal I/O with a very different model from any *nix system. As a result, there really is no direct equivalent to the termios.h header and its friends.

You want to read at MSDN about the Windows Communications Resources.

Some things to learn more about include:

In general, you will find that you need to deal a lot more with the Windows API directly because stdio will add to the confusion when doing device I/O.

select.h

There isn't a direct equivalent to the Unix select(2) system call.

In Windows, many kernel objects can be in either a signaled or non-signaled state, and the act of signalling the object can be used to release a thread that called WaitForMultipleObjects(). Some but not all HANDLE objects are signaled when data is available. Specifically, I know that HANDLEs from WinSock have that capability, but I don't know about the Comm API. I know that HANDLEs to an open file do not.

If you need to wait for an event in a thread that is processing window messages, then you should probably use MsgWaitForMultipleObjects() instead, since it will properly deliver messages while the thread is otherwise blocked.

Read about the Windows synchronization primitives at the MSDN article Using Synchronization.

However, there are several kinds of asynchronous I/O built into Windows that can replace the need for select() by a change of design. Both will require extensive use of features that cannot be used in combination with the C stdio library.

MSDN has several articles on I/O techniques, as well as numerous examples:

Note that much of the information on how Windows works is scattered among the overview articles and the remarks sections of the reference material for the API functions and structures. This can give the impression that nothing is completely documented on a first reading.

Porting with Cygwin

Another approach is to use Cygwin to do the port. It provides most of a POSIX layer over the Windows API. However, you will end up with an application that is dependent on the Cygwin DLL which is GPL unless you purchase a commercial use license from them. It can be tricky to use Cygwin to get an application that works well for a Windows user with no Unix experience also, since so many other assumptions about the way the two systems are setup and used differ.

Cygwin has done a fair amount of heavy lifting to build an implementation of select() that works on Windows given a mix of different open file descriptors. This effort is described in the User's Guide.

Do be aware that building against Cygwin is only documented and supported if done from within the Cygwin environment. It usually is not sufficient to just put Cygwin's bin on the Windows PATH and work from a command prompt. You really need to launch Cygwin's build of bash and compile from there so that everything is using the same Cygwin-style mount points and simulated Unix file structure.

Mixing Cygwin header files with third-party tool header files is a sure path to madness.

Edit: I've rearranged a bit, and added some material in response to comments.

Gorgon answered 1/6, 2009 at 6:51 Comment(8)
Thats ok..but wat about the sys/select.h I have includeed the Cygwin path to my commands prompt by set PATH=C:\cygwin\bin;%PATH% then also the same error occurs..Gaines
D:\usr\xtensa\XtDevToolsDE\install\tools\RB-2008.4-win32\XtensaTools\xtensa-elf\src\uclibc\include\s ys\select.h(25) : fatal error C1083: Cannot open include file: 'features.h': No such file or directo ry make.exe: *** [uartsim.obj] Error 2Gaines
I've added some text describing my understanding of the situation with select(). Your issue with features.h is that you've used select.h from some other toolchain rather than from Cygwin. Just putting Cygwin on the PATH won't be enough, you have to commit to using it for the whole build process. That is actually one of the things I don't like about Cygwin.Gorgon
thanks dear friend. I have done all my building procedures inside cygwin.Then its ok and working well.But at execution time some error is there.. I really want to build the same program in windows using MS VS 2005 . For this i need to repalce the whole logic as u have said eariler(No EQVnt API in windows). But i think windows peoples are little bit concious about this r8? so that the the eqvln API's should be included ...Gaines
Windows and Unix have similar capabilities, but not similar APIs. Furthermore, there are things that are easy on one and hard or nearly impossible on the other. It is effectively impossible to implement a true fork() on Windows, for instance.Gorgon
can u help me in finding out the equivalent headers in windows?Gaines
The MSDN section on Communication Resources linked in my answer is the starting point for serial I/O. There are examples and overviews as well as reference documentation. I'm editing in a link to the equivalent starting point for synchronization. You will need to get familiar with a fair amount of the way Windows does things in order to implement this capability.Gorgon
It should be pointed out that there is a select(2) call under Windows; however it can only be used with network sockets, and won't work in conjunction with stdio, etc.Lexicologist
F
2

There's also a termios porting for Windows, called termiWin.

termiWin is a library which purpose is to allow you to use on a Windows system, the same code used in Linux to communicate with a device through a serial port. This is possible because termios’s functions have been rewritten to be compatible with Windows’s COM functions.

Flour answered 20/9, 2021 at 7:19 Comment(2)
Just a reminder for those who found my answer not useful.Flour
Well, even if it works, it might not work for everyone, and here is why: github.com/veeso/termiWin#project-status-%EF%B8%8FLehman
M
-4

I created 2 files using code I found in some forums to circumvent windows.h and windows com port libraries:

"nowindows.h"

/* file nowindows.h v1.0 use at your own risk *
#ifndef DWORD
#define WINAPI
typedef unsigned long DWORD;
typedef short WCHAR;
typedef void * HANDLE;
#define MAX_PATH PATH_MAX
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int BOOL;
#include <sys/types.h>
#include <sys/stat.h>
#include "unistd.h"
#include <fcntl.h>

#define GENERIC_READ                O_RDONLY    //read only mode
#define GENERIC_WRITE               O_WRONLY    //write only mode
#define CREATE_ALWAYS               O_CREAT     //create new file
#define OPEN_EXISTING               0           //fake parameter's value
#define FILE_ATTRIBUTE_NORMAL       0644        // file attributes
#endif

and

"nowindowscomport.h"

/* file nowindowscomport.h v1.0 use at your own risk *//
typedef struct _DCB {
    DWORD DCBlength;
    DWORD BaudRate;
    DWORD fBinary  :1;
    DWORD fParity  :1;
    DWORD fOutxCtsFlow  :1;
    DWORD fOutxDsrFlow  :1;
    DWORD fDtrControl  :2;
    DWORD fDsrSensitivity  :1;
    DWORD fTXContinueOnXoff  :1;
    DWORD fOutX  :1;
    DWORD fInX  :1;
    DWORD fErrorChar  :1;
    DWORD fNull  :1;
    DWORD fRtsControl  :2;
    DWORD fAbortOnError  :1;
    DWORD fDummy2  :17;
    WORD  wReserved;
    WORD  XonLim;
    WORD  XoffLim;
    BYTE  ByteSize;
    BYTE  Parity;
    BYTE  StopBits;
    char  XonChar;
    char  XoffChar;
    char  ErrorChar;
    char  EofChar;
    char  EvtChar;
    WORD  wReserved1;
} DCB, *LPDCB;
typedef struct _COMSTAT {
    DWORD fCtsHold  :1;
    DWORD fDsrHold  :1;
    DWORD fRlsdHold  :1;
    DWORD fXoffHold  :1;
    DWORD fXoffSent  :1;
    DWORD fEof  :1;
    DWORD fTxim  :1;
    DWORD fReserved  :25;
    DWORD cbInQue;
    DWORD cbOutQue;
} COMSTAT, *LPCOMSTAT;
typedef struct _COMMTIMEOUTS {
    DWORD ReadIntervalTimeout;
    DWORD ReadTotalTimeoutMultiplier;
    DWORD ReadTotalTimeoutConstant;
    DWORD WriteTotalTimeoutMultiplier;
    DWORD WriteTotalTimeoutConstant;
} COMMTIMEOUTS, *LPCOMMTIMEOUTS;
#define ERROR_INVALID_HANDLE             6L 
/* Purge functions for Comm Port */
#define PURGE_TXABORT       0x0001  /* Kill pending/current @@-377,11 +382,4 @@ */
#define PURGE_RXCLEAR 0x0008
#define PURGE_TXCLEAR 0x0004
#define PURGE_RXABORT 0x0002
// DTR Control Flow Values.
#define DTR_CONTROL_DISABLE    0x00
#define DTR_CONTROL_ENABLE     0x01
#define DTR_CONTROL_HANDSHAKE  0x02
#define RTS_CONTROL_DISABLE 0x00
#define NOPARITY 0
#define ONESTOPBIT 0
Marcelline answered 5/8, 2012 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.