SW 개발

[system api] LINUX APP 으로 RS232 RTS 신호핸들링 : 예제

. . . 2015. 7. 29. 22:55
반응형

RTS 신호 핸들링하는 예제코드를 정리한다.

RTS 신호 핸들링

#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>

main() {
    int fd, sercmd, serstat;

    sercmd = TIOCM_RTS;
    fd = open("/dev/ttyS0", O_RDONLY); // Open the serial port.

    printf("Setting the RTS pin.n");
    ioctl(fd, TIOCMBIS, &sercmd); // Set the RTS pin.

    // Read the RTS pin status.
    ioctl(fd, TIOCMGET, &serstat);
    if (serstat & TIOCM_RTS)
        printf("RTS pin is set.n");
    else
        printf("RTS pin is reset.n");

    getchar(); // Wait for the return key before continuing.

    printf("Resetting the RTS pin.n");
    ioctl(fd, TIOCMBIC, &sercmd); // Reset the RTS pin.

    // Read the RTS pin status.
    ioctl(fd, TIOCMGET, &serstat);
    if (serstat & TIOCM_RTS)
        printf("RTS pin is set.n");
    else
        printf("RTS pin is reset.n");

    close(fd);
}

이어서..

관련 api 들..
void
start_tx(int fd)
{
    int status;

    ioctl(fd, TIOCMGET, &status);
    status |= TIOCM_RTS;

    if (ioctl(fd, TIOCMSET, &status))
    {
        perror("ioctl");
        exit(1);
    }
}
  
void
end_tx(int fd)
{
    int status;
    ioctl(fd, TIOCMGET, &status);
    status &= ~TIOCM_RTS;
    ioctl(fd, TIOCMSET, &status);
}



반응형