serial programming 문제

익명 사용자의 이미지

안녕하세요
serial 통신 프로그램을 만들어 보려고하는데요
지금은 실험정도 수준으로 간단하게 만들려고 하는데 write 가 안됩니다.
일단 소스를 보시고 조언을 주시면 감사하겠습니다.
한대의 컴퓨터에서 com1 과 com2 를 시리얼 케이블(크로스)로 연결하고 하고 있습니다.

//serial_write.c
#include
#include
#include
#include
#include
#include

int main(void)
{
int fd, n;
struct termios options;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
perror("open_port Unable to open /dev/ttyS0 - ");
}else
{
fcntl(fd, F_SETFL, 0);
}

printf("\nfd = %d\n", fd);

tcgetattr(fd, &options);

cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);

options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_cflag |= CRTSCTS;

options.c_lflag |= ICANON;

options.c_oflag |= OPOST;

tcsetattr(fd, TCSANOW, &options);

n = write(fd, "ATZ\r\n", 5);
if(n < 0)
perror("write A write error has occurred - ");

close(fd);
}

//serial_read.c
#include
#include
#include
#include
#include
#include

int main(void)
{
int fd, nread;
char buffer[255];
struct termios options;

fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
perror("open_port Unable to open /dev/ttyS0 - ");
}else
{
fcntl(fd, F_SETFL, 0);
}

printf("\nfd = %d\n", fd);

tcgetattr(fd, &options);

cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);

options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_cflag |= CRTSCTS;

options.c_lflag |= (ICANON | ECHO | ECHOE);

options.c_oflag |= OPOST;

tcsetattr(fd, TCSANOW, &options);

nread = read(0, buffer, 128);
if(nread == -1)
{
perror("read A read error has occurred - ");
}

printf("read msg = %s", buffer);

close(fd);
}

익명 사용자의 이미지

tldp에 시리얼 프로그래밍 2번째 판이 나왔던데요...

그 문서에 나와 있는 소스를 참고 하신는게 어떨까요?

저도 그것때문에 삽질하다가 방금 전에야 문자 하나를 주고 받았답니다

ㅜ.ㅜ

read 프로그램은 그냥 보시면 될것 같구
문서에 나와 있는거 그대로 치시면 될 것 같구

write 프로그램은

//newtio.c_iflag = 0;
newtio.c_oflag = OPOST | OCRNL;

이렇게 바꾸어 주시면 될 것 같내요.

꼭 성공하세요