본문 바로가기

Scraps

How to connect to a serial port as simple as using SSH?

( SSH를 새용할 때 처럼 간단하게 시리얼 포트에 접속하는 방법은? )


원문: https://unix.stackexchange.com/questions/22545/how-to-connect-to-a-serial-port-as-simple-as-using-ssh




질문 내용 (질문자)

Is there a way to connect to a serial terminal just as you would do with SSH? There must be a simpler way than tools such as Minicom, like this

(여러분이 SSH를 사용할 때 처럼 간단하게 시리얼 터미널에 접속할 수 있는 방법이 있나요? 뭐, Minicom 처럼 간단한 방법이었으면 좋겠네요)


$ serial /dev/ttyS0 

I know I can cat the output from /dev/ttyS0 but only one way communication is possible that way, from the port to the console. And echo out to the port is just the same but the other way around, to the port.

(저는 /dev/ttyS0를 cat 명령으로 출력하면 된다는 걸 알긴 하는데, 포트에서 우리가 보는 콘솔로 뿌려주는게 가능하긴 한데 그건 단방향 통신만 가능하잖아요. 그리고 마찬가지로 echo 명령으로 출력하는 것도 마찬가지구요)


How can I realize two way communication with a serial port the simplest possible way on Unix/Linux?

(제가 Unix나 Linux에서 시리얼 포트랑 통신할 때 가장 간단한 방법으로 뭐가 있을까요?)


최다 득표 답변:

I find screen the most useful program for serial communication since I use it for other things anyway. It's usually just screen /dev/ttyS0 <speed>, although the default settings may be different for your device. It also allows you to pipe anything into the session by entering command mode and doing exec !! <run some program that generates output>.

(저는 screen 명령을 발견했는 데요, 제가 해본것들 중에서 screen이 시리얼 통신할 때 쓰면 편하다고 생각해요. 그냥 간단하게 "screen /dev/ttyS0 <속도>"을 입력하거나 질문자께서 가지고 있는 기기에 따라서 기본 설정을 바꿔서 쓸 수 있어요. 또 그건 뭔가 실행하는 도중에서도 명령 모드로 세션을 진입시키는 파이프를 만들 수가 있어요. exec !! <출력을 생성해줄 프로그램을 실행> 라고 입력하면 되죠)


2위 답변

Background (배경)


The main reason why you need any program like minicom to communicate over a serial port is that the port needs to be set up prior to initiating a connection. If it weren't set up appropriately, the cat and echo commands would not do for you what you might have expected. Notice that once you run a program like minicom, the port is left with the settings that minicom used. You can query the communication settings using stty program like this:

(저는 질문자가 minicom 같은걸 쓰는 이유가 포트를 사용하기 위해서 연결을 초기화 하는 작업이 필요하다고 생각하는 것 같아요. 그걸 하지 않고서는 cat이나 echo 명령이 질문자가 기대한 대로 동작하지 않기 때문이죠. minicom 같은 프로그램을 실행 할 때 그 포트는 minicom이 사용하는 설정이 되죠. 그걸 질문자 께서는 stty 같은 명령으로 수동으로 할 수 있어요)


stty < /dev/ttyS0

If you have done it right; after booting the computer and before running any other program like minicom, the communication settings will be at their default settings. These are probably different then what you will need to make your connection. In this situation, sending the commands cat or echo to the port will either produce garbage or not work at all.

(만약 질문자가 그걸 올바르게 설정 했다면, 컴퓨터를 켠 후, 호거은 minicom 같은 프로그램을 실행 하기 전에는 통신 설정이 그것들이 가지는 기본 설정으로 되어 있을 거에요. 이런건 아마 질문자가 연결하는데 필요한 것이랑은 좀 다를거에요. 이럴 때 cat 명령이나 echo 명령으로 포트에 뭔가 보내면 쓰레기를 만들어내거나 잘 동작하지 않을 겁니다)


Run stty again after using minicom and you'll notice the settings are set to what the program was using.

(minicom을 사용한 후에 stty를 다시 실행하면 질문자께서는 프로그램 사용했던 설정이 뭔지 알 수 있을거에요)


Minimal serial communication (최소 시리얼 통신)


Basically, what needs to be done in order to have two-way communication through a serial port, is: 1) configuring the serial port, and 2) opening the pseudo-tty read-write.

(기본적으로, 시리얼 포트로 양방향 통신을 하려면 첫번째로 시리얼 포트를 설정해줘야 하고, 두번째로 가상 tty를 RW 모드로 열어야 되요)


The most basic program that I know that does this is picocom. You can also use a tool like setserial to set up the port and then interact with it directly from the shell.

(제가 알기로 picocom이 가장 기본적인 프로그램일거라고 생각해요. 질문자분께서는 뭐, setserial 같은 걸로 포트를 설정하고 나서 쉘로 다이렉트 통신을 해도 되지요)


3위 답변

I found a way using a shell script here that put cat as a background process and a while loop that read the user input and echo it out to the port. I modified it to be more general and it fitted my purpose perfectly.

(전 쉘스크립트를 사용해서 그걸 하는 방법을 알아냈죠. 뭐, cat이 백그라운드 프로세스로서 시리얼 포트에서 받은 걸 출력하고, while 루프를 돌면서 입력받은걸 시리얼 포트로 출력해주는 방법이죠. 제가 이걸 좀더 일반적인 구현으로 바꿨고, 뭐 제가 쓰려던 목적에는 완벽하게 맞네요)


#!/bin/sh


# connect.sh


# 사용법:

# $ connect.sh <device> <port speed>

# 예시: connect.sh /dev/ttyS0 9600


# 장치 설정

stty -F $1 $2


# cat이 장치를 백그라운드에서 읽게 만들고

cat $1 &


# 그 프로세스의 PID를 저장해 둿다가 끝날 때 그걸 죽일거에요.

bgPid=$!


# 사용자로부터 입력을 받아서 그걸 바로 시리얼 포트로 출력합니다.

while read cmd

do

   echo "$cmd" 

done > $1


# 더 필요가 없으니까 아까 만든 백그라운드 프로세스를 죽이죠.

kill $bgPid


역자 주


제가 스크랩 카테고리를 처음 만들면서 생각한건 스크랩을 하되 번역을 달아 놓자는 생각을 했고, 질/답을 모두 하기에는 양이 너무 많아서 질문 + 답변 좋은거 3개를 하기로 했습니다. 이것도 재배포 정책은 번역글이랑 동일합니다!


출처 포기하는 방법:

How to connect to a serial port as simple as using SSH? (Stack Exchange) / 역: Jay K (http://www.jayks.ml/10)

이런 형태로 출처와 원문 저자(위와 같습니다), 역자 및 번역본 주소가 잘 표기되어 있으면 좋겠습니다.

감사합니다.

'Scraps' 카테고리의 다른 글

Websocket Example under Linux  (1) 2017.07.15