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;

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

꼭 성공하세요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.