[질문] 시리얼 통신

양선모의 이미지

VMWare에 Redhat 9.X 를 설치한 후
시리얼 통신 프로그램을 작성하고 있습니다.

인터넷에 굴러다니는 소스를 바탕으로
소스는 작성했구요.

NULL모뎀으로 테스트를 하고있는데,
송/수신은 문제없이 되고 있는데,

읽은 값을 계속해서 읽는것이 문제가 되고 있습니다.

다시말하면,

COM1으로 "aaa"를 쓰면
"aaa"가 계속해서 읽히는 문제 입니다.

한번 "aaa"를 읽으면 read버퍼가 초기화 되어서
대기상태로 들어갔으면 좋겠는데, 어떻게 해야될지 모르겠네요.

고수님들의 조언 부탁드립니다.

poplinux의 이미지

소스 올려 주시면 도움이 될 겁니다.

========================
조직 : E.L.D(Embedded Linux Developer/Designer)
블로그 : poplinux@tistory.com
카페 : cafe.naver.com/poplinux

임베디드 리눅스 관련 프리렌서 지향

양선모의 이미지

아래 소스는 인터넷에서 구한 소스를
테스트 프로그램에 맞게 조금 수정하였습니다.

OpenPort는 COM1을 오픈하는 부분이고,
ClosePort는 COM1을 닫는 부분

readPort는
메인함수에서 쓰레드를 생성하고 While Loop에서 반복 호출하도록 하였습니다.

Read함수에서 리턴되는 str의 길이를 체크하여 0 이상일대 출력하도록 하였는데
반복적으로 같은 값이 나오네요.

조언 부탁드립니다.

---------------------------- 소스 ----------------------------------

#include "serial.h"

int OpenPort()
{
if(bConnected == 0)
{
char readbuffer[2048] ;
memset(readbuffer, 0, 2048) ;

handle = open("/dev/ttyS0", O_RDWR | O_NOCTTY) ;
if(handle < 0)
{
printf("Serial Open failed \n") ;
return 0 ;
}
tcgetattr(handle, &oldtio) ;
bzero(&newtio, sizeof(newtio)) ;
newtio.c_cflag = B9600|CS8|CLOCAL|CREAD ;
newtio.c_iflag = IGNPAR ;
newtio.c_oflag = 0 ;
newtio.c_lflag = 0 ;
newtio.c_cc[VTIME]=1;
newtio.c_cc[VMIN] =0 ;

tcflush(handle, TCIFLUSH) ;
tcsetattr(handle, TCSANOW , &newtio) ;
bConnected = 1 ;
return 1 ;
}
return 0;
}

void ClosePort()
{
if(bConnected == 1)
{
bConnected = 0 ;
tcsetattr(handle, TCSANOW, &oldtio) ;
close(handle) ;
}
}
int write_port(char *str, int len)
{
if(len > 0 && bConnected == 1)
{
char *buf[256] ;
if(write(handle,str,len) < 0)
{
printf("Write Error \n") ;
}
usleep(1000) ;
}
}

void read_port(char **str)
{
int RxCount = 0 ;
char Buffer[256] ;

if(bConnected == 1)
{
RxCount = read(handle, Buffer,256) ;

if(RxCount > 0 && RxCount < 255)
{
Buffer[RxCount] = '\0' ;
memcpy(str, Buffer , RxCount+1) ;
usleep(2000);
}
}
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.