Milky Way Icon.
MultiverseSocial.com
wtermio.c Documentation
Milky Way Icon.
Cell 3

Function Reference

# Function Parameters Description Return Value
0 int open(const char *pathname, int flags, ...) pathname: Path (e.g., "COM3")
flags: POSIX flags (e.g., O_RDWR)
Opens a file or serial port, assigns FD. FD or -1 (sets errno)
1 int close(int fd) fd: File descriptor Closes an open FD. 0 or -1 (sets errno)
2 static HANDLE _get_handle(int fd) fd: File descriptor Internal: Gets HANDLE from FD. HANDLE or INVALID_HANDLE_VALUE
3 int tcgetattr(int fd, struct termios *termios_p) fd: FD
termios_p: termios ptr
Gets terminal attributes. 0 or -1
4 int tcsetattr(int fd, int optional_actions, const struct termios *termios_p) fd: FD
optional_actions: TCSANOW, etc.
termios_p: termios ptr
Sets terminal attributes. 0 or -1
5 int tcsendbreak(int fd, int duration) fd: Serial FD
duration: Break time (ms)
Sends a break signal on serial port. 0 or -1
6 int tcdrain(int fd) fd: FD Waits for output to drain. 0 or -1
7 int tcflush(int fd, int queue_selector) fd: FD
queue_selector: TCIFLUSH, etc.
Flushes queues. 0 or -1
8 int tcflow(int fd, int action) fd: Serial FD
action: TCOOFF, etc.
Controls serial flow. 0 or -1
9 speed_t cfgetispeed(const struct termios *termios_p) termios_p: termios ptr Gets input baud rate. Baud rate
10 speed_t cfgetospeed(const struct termios *termios_p) termios_p: termios ptr Gets output baud rate. Baud rate
11 int cfsetispeed(struct termios *termios_p, speed_t speed) termios_p: termios ptr
speed: Baud rate
Sets input baud rate. 0
12 int cfsetospeed(struct termios *termios_p, speed_t speed) termios_p: termios ptr
speed: Baud rate
Sets output baud rate. 0
13 int cfsetspeed(struct termios *termios_p, speed_t speed) termios_p: termios ptr
speed: Baud rate
Sets both baud rates. 0
14 pid_t tcgetsid(int fd) fd: FD Gets session ID (process ID on Windows). Process ID
15 int _kbhit(void) None Non-Blocking: Checks for console keypress. 1 if key available, 0 otherwise
16 int GetC(void) None Non-Blocking: Gets printable char or -1. ASCII value or -1

Note: Non-blocking functions _kbhit() and GetC() are highlighted in yellow.

Example Usage

int main(void) {
    struct termios tty;
    char c;
    int fd = open("COM3", O_RDWR);
    if (fd >= 0) {
        tcgetattr(fd, &tty);
        cfsetispeed(&tty, B9600);
        cfsetospeed(&tty, B9600);
        cfsetspeed(&tty, B9600);
        tty.c_cflag |= CS8 | CREAD;
        tcsetattr(fd, TCSANOW, &tty);
        printf("Speeds: %u, %u\n", cfgetispeed(&tty), cfgetospeed(&tty));
        tcsendbreak(fd, 500);
        tcflush(fd, TCOFLUSH);
        tcflow(fd, TCOON);
        tcdrain(fd);
        close(fd);
    }
    tcgetattr(0, &tty);
    tty.c_lflag &= ~ISIG;
    tty.c_cc[VEOF] = _POSIX_VDISABLE;
    tty.c_cc[VERASE] = _POSIX_VDISABLE;
    tcsetattr(0, TCSANOW, &tty);
    printf("Session ID: %lld\n", (long long)tcgetsid(0));
    printf("Press 'q' to quit...\n");
    for (;;) {
        if (_kbhit()) {
            if ((c = GetC()) == 'q') break;
            if (c != -1) printf("Key: %c (%d)\n", c, c);
        }
        Sleep(100);
    }
    fd = open("test.txt", O_RDWR | O_CREAT | O_TRUNC);
    if (fd >= 0) {
        WriteFile(_get_handle(fd), "Hello\n", 6, NULL, NULL);
        close(fd);
    }
    printf("Exiting...\n");
    return 0;
}

Download wtermio.c Source File

Cell 5
Cell 6
Cell 7
Cell 8