SW 개발

GCC / Linux,Unix / 예제소스코드 / 시리얼통신 기본코드

. . . 2009. 4. 21. 01:22
반응형
  • 마크다운변환 : 20190905

  • 출처 : 나.
  • 기타사항 : Rebis 보드에서 테스트완료, 틀린사항은 댓글로 달아주세여

예제코드

시리얼을 다루기 위해서는 일반 디바이스와는 틀린 코딩을 하더군요.
걍 open해서 read / write 해서는 동작을 한한다능..;;

일단 터미널 설정을 하는 구조체..<asm/termbits.h>에 정의되어있슴..

      #define NCCS 19 
       struct termios { 
               tcflag_t c_iflag;               /* input mode flags */ 
               tcflag_t c_oflag;               /* output mode flags */ 
               tcflag_t c_cflag;               /* control mode flags */ 
               tcflag_t c_lflag;               /* local mode flags */ 
               cc_t c_line;                    /* line discipline */ 
               cc_t c_cc[NCCS];                /* control characters */ 
       }; 

여기서 각 터미널 사용을 위한 파라미터및 설정을 담고있는 구조체..

위에서 정의한 구조체를 세팅 혹은 얻어오는 함수들..

tcgetattr(); // 현재 설정을 얻어옴 
tcsetattr(); // 설정을 세팅.. 

read시에 block / nonblock 으로 열수도있고 일단 open 하고 한번 read / write 성공했으면 다음부터는 시스템 프로그래밍 하듯이 짜면 될듯합니다.ㅋㅋㅋㅋㅋㅋ

간단테스트코드

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <termios.h> 
#include <fcntl.h> 

int main( void) 
{ 
    int fd; 

    char buf[128]; 

    struct termios newtio; 

    //fd = open( "/dev/tts/1", O_RDWR | O_NOCTTY | O_NONBLOCK ); 

    fd = open( "/dev/tts/1", O_RDWR | O_NOCTTY ); 
    memset( &newtio, 0, sizeof(newtio) ); 


    newtio.c_cflag = B115200;     
    newtio.c_cflag |= CS8;     
    newtio.c_cflag |= CLOCAL;     
    newtio.c_cflag |= CREAD;     
    newtio.c_iflag = IGNPAR;     
    newtio.c_oflag = 0; 
    newtio.c_lflag = 0; 
    newtio.c_cc[VTIME] = 0;   
    newtio.c_cc[VMIN] = 1;   

    tcflush (fd, TCIFLUSH ); 
    tcsetattr(fd, TCSANOW, &newtio ); 

    read(fd,buf,128); 
    printf("buf is [%s]\n",buf); 
    write( fd, "forum.falinux.com", 17);     

    close( fd);     
    return 0;   
}
반응형