newtype
::: newtype의 지식창고 :::
newtype
전체 방문자
오늘
어제
  • 분류 전체보기 (392)
    • Dev (214)
      • C, C++ (43)
      • Go (5)
      • Web (49)
      • DBMS (21)
      • DevOps (8)
      • Java (2)
      • Windows, Win32 (4)
      • Visual Basic (5)
      • C# (2)
      • Mobile (25)
      • SQL CE (7)
      • Google Map (6)
      • Python (2)
      • cygwin (2)
      • 기타 (32)
      • Ruby (1)
    • 명언 (10)
    • 모임 (18)
      • 붕주회 (3)
      • 신흥컴정 (14)
      • 웹20기 (1)
    • 사진 (8)
    • 불펌 (29)
    • 막글 (98)
    • 게임 (6)
    • 여행 (8)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

  • whoami
05-19 18:39
hELLO · Designed By 정상우.
newtype

::: newtype의 지식창고 :::

Dev/C, C++

Serial-port 관련 자료 정리

2014. 8. 29. 21:43

centos 에서 Serial-Port를 이용한 파일전송 모듈을 개발하면서 정리한 자료 입니다.

시리얼 포트 상태 확인

장치 확인

$ cat /proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A port:000003F8 irq:4 tx:930342833 rx:215304 brk:145317 RTS|CTS|DTR|DSR
1: uart:16550A port:000002F8 irq:3 tx:1090 rx:0 RTS|CTS|DTR|DSR|CD
2: uart:unknown port:000003E8 irq:4
3: uart:unknown port:000002E8 irq:3

커널에서 시리얼 드라이버 load 되었는지 확인

$ dmesg | grep tty

정상적인 경우

serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
00:09: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A

예외 상황

Xen virtual console succefully installed as ttyS0
  • Xen에서 가상콘솔을 사용하는 듯.
  • 출처: https://kldp.org/node/91683
  • 해결 방안: 부팅 관리자에서 Xen이 없는 것으로 부팅(default값 수정 Xen이 없는 것으로 부팅 '0' 시작 Index)
  • $vi /boot/grub/grub.conf default = 1

포트 확인

간략한 포트 확인

$ setserial -a /dev/ttyS0
/dev/ttyS0, Line 0, UART: 16550A, Port: 0x03f8, IRQ: 4
  Baud_base: 115200, close_delay: 50, divisor: 0
  closing_wait: 3000
  Flags: spd_warp skip_test

조금 더 자세한 포트 확인

$ stty -F /dev/ttyS0
speed 115200 baud; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>; start = <undef>; stop = <undef>; susp = <undef>;
rprnt = <undef>; werase = <undef>; lnext = <undef>; flush = <undef>; min = 0; time = 100;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

상세한 포트 확인

stty -a < /dev/ttyS0
speed 115200 baud; rows 0; columns 0; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = <undef>; stop = <undef>; susp = <undef>; rprnt = <undef>; werase = <undef>; lnext = <undef>; flush = <undef>;
min = 0; time = 100;
-parenb -parodd cs8 -hupcl -cstopb cread clocal crtscts -cdtrdsr
-ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke
  • 사용중일 경우엔 아무 반응 없음.
  • 앞에 -가 붙은것은 disable 상태, 붙지 않은것은 enable 상태
  • 각 파라미터에 대한 상세 정보는 stty manpage 참고
  • http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/termios
  • http://www.nxmnpg.com/ko/1/stty
  • http://www.linuxcertif.com/man/1/stty/ko/
  • http://man.cx/stty(1)/ko
  • http://rpm.pbone.net/index.php3/stat/45/idpl/2404578/numer/1/nazwa/stty

시리얼 포트 설정 변경 및 복구

시리얼 포트 자동 설정 하기

setserial /dev/ttyS0 -v autoconfig

시리얼 포트 기본 설정으로 복구 하기

stty sane < /dev/ttyS0

시리얼 포트 속도 변경 하기

stty 115200 < /dev/ttyS0

장치 이름이 지워진 경우 복구하기

mknod -m 666 /dev/ttyS0 c 4 64
  • 출처: http://www.linuxquestions.org/questions/linux-hardware-18/can-write-to-but-not-read-from-ttys0-188998/

시리얼 포트로 접속 테스트

가장 간단한 방법

수신측

cat /dev/ttyS0

송신측

date > /dev/ttyS0

진보된 방법

screen 유틸 사용

  • 사용법: http://nicejhong.tistory.com/50
  • 사용법: https://www.gnu.org/software/screen/manual/screen.html

minicom 유틸 사용 (시스템 권한 필요)

  • 사용법: http://www.lug.or.kr/docs/LINUX/others/minicom.htm
  • 사용법: http://www.linuxcertif.com/man/1/minicom/ko/
  • 사용법: http://heestory.me/252
minicom -s

OSX 에서 시리얼 포트 테스트 방법

  • 시리얼 포트가 없으므로 USB2Serial 젠더나 케이블이 필요하다.
    • 제조사 칩셋에 맞는 드라이버 설치 필요.
    • 출처: http://mcchae.egloos.com/10562218
  • 터미널에서 screen 유틸 사용
    • GUI에서 SerialTools 유틸 사용(AppStore에 있음) - 연결 상태를 눈으로 확인 할 수 있어서 추천함.

시리얼 포트 통신 프로그래밍

시리얼 통신 특이사항

  • BAUDRATE을 높게 잡아도 특정 수치 이후로는 속도가 같다.
    • null cable 사용시 11,531 byte per sec 정도 나옴.
  • 버퍼를 255 이상 잡으면 짤려서 전송된다.
    • 송/수신시 나눠서 전송 할 필요가 있다.
  • 전송중에 케이블을 빼도 상태를 알수가 없다.
    • 프토토콜을 정의할 필요가 있다.
  • non-canonical, x초(0.1초이상) 동안 입력이 없으면, timeout 처리 되록 하는것이 최적인듯 하다.
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 100;
newtio.c_cc[VMIN] = 0;
  • 속도가 매우 느리므로 데이터를 압축해서 보내는 것이 좋다.
    • snappy
      • zlib보다 빠르다.
      • 13배 정도의 속도 개선 효과가 있었다.
      • http://code.google.com/p/snappy/참고 문서
  • https://wiki.kldp.org/wiki.php/Serial-Programming-HOWTO
  • ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Serial-Programming-HOWTO
  • http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux

시리얼 포트로 터미널 접속 셋팅(서버측)

/etc/inittab 에 ttyS0 추가

s0:2345:respawn:/sbin/agetty 57600 ttyS0

/etc/securetty 에 ttyS0 추가

  • root 사용자(또는 root 권한을 가진 사용자)는 시리얼접속이 막혀있어서, 해당 포트를 추가해주어야만 한다.
  • 출처: http://wiki.soekris.info/Connecting_to_the_serial_console#Mac_OS_X

참고 문서

  • http://wiki.soekris.info/Connecting_to_the_serial_console#Mac_OS_X
  • http://www.tldp.org/HOWTO/html_single/Serial-HOWTO/
  • http://www.lug.or.kr/docs/LINUX/others/00-04-5.htm
  • http://www.linuxlab.co.kr/docs/00-05-5.htm
반응형
저작자표시 (새창열림)

'Dev > C, C++' 카테고리의 다른 글

Visual Leak Detector 를 사용하여 Leak을 감지하는 방법.  (0) 2016.02.15
debian 계열 linux에서 core 파일 생성 방법  (0) 2015.07.30
core 파일이 생성되는 한가지 상황  (1) 2011.10.31
Pro*C/C++ Precompiler Programmer's Guide  (0) 2009.03.13
한글이 포함된 문자열을 특정 byte로 자르기  (0) 2009.02.16
    'Dev/C, C++' 카테고리의 다른 글
    • Visual Leak Detector 를 사용하여 Leak을 감지하는 방법.
    • debian 계열 linux에서 core 파일 생성 방법
    • core 파일이 생성되는 한가지 상황
    • Pro*C/C++ Precompiler Programmer's Guide
    newtype
    newtype
    지극히 개인적인 지식의 창고

    티스토리툴바