티스토리 뷰
이번 포스팅 내용은 파이썬에서 serial 통신을 하는 내용이다.
먼저 추가해줘야 할 모듈이 두개 있다.
C:\..\Python\Python38-32\python.exe "D:/develop/python/ Python_test/Education/day1.py" Traceback (most recent call last): File "D:/develop/python/ Python_test/Education/day1.py", line 2, in <module> import serial ModuleNotFoundError: No module named 'serial'
Process finished with exit code 1 |
pip 를 통해 serial 모듈 설치 완료!!
D:\develop\python\ Python_test\mysite>pip install serial
Collecting serial Using cached serial-0.0.97-py2.py3-none-any.whl (40 kB) Requirement already satisfied: pyyaml>=3.13 in c:\..\python\python38-32\lib\site-packages (from serial) (5.3.1) Requirement already satisfied: iso8601>=0.1.12 in c:\..\python\python38-32\lib\site-packages (from serial) (0.1.13) Requirement already satisfied: future>=0.17.1 in c:\..\python\python38-32\lib\site-packages (from serial) (0.18.2) Installing collected packages: serial Successfully installed serial-0.0.97 |
시리얼 포트 연결을 위해 아래와 같이 시리얼 디스크립터를 로드한다.
ser = serial.Serial(port='COM3', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) |
그럼 아래와 같이 serial 모듈에 'Serial' attribute 를 추가 설치!!
C:\..\Python\Python38-32\python.exe "D:/develop/python/ Python_test/Education/day1.py" Traceback (most recent call last): File "D:/develop/python/ Python_test/Education/day1.py", line 6, in <module> ser = serial.Serial(port='COM3', baudrate=115200, parity=serial.PARITY_NONE, AttributeError: module 'serial' has no attribute 'Serial'
Process finished with exit code 1 |
pip 를 통해 'pyserial' 패키지를 설치하여 Serial attribute 를 사용할 수 있다.
D:\develop\python\ Python_test\mysite>pip install pyserial Collecting pyserial Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB) |████████████████████████████████| 90 kB 803 kB/s Installing collected packages: pyserial Successfully installed pyserial-3.5
D:\develop\python\ Python_test\mysite> |
목적이 serial 포트로 input 되는 데이터를 그대로 출력하는 코드는 간단한다.
코드 공유하면 아래와 같다.
Serial 함수 호출 시, port, baudrate, parity 비트, stopbits, bytesize 를 정의해주면 된다.
우선 프로그래밍 하기 전 데이터를 확인하기 위해서는 하이퍼터미널이나 테라텀(TeraTerm)을 활용할 수 있다.
import serial
ser = serial.Serial(port='COM3', baudrate=115200, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
if ser.readable():
res = ser.readline()
print(res.decode()[:len(res) - 1])
대부분의 경우, 위의 코드로 serial 포트로 들어오는 데이터를 활용할 수 있겠지만,
하지만 저자의 경우는 readline()으로 데이터를 읽어오면 일정한 구분자를 통해서 들어오는 데이터를 잘라서 활용할 수 없어서, byte 단위로 데이터를 받아서 처리해야만 했다.
buffer=""
while True:
oneByte = f.read(1)
if oneByte == b"\r": #byte단위로 read, 구분자 '\r'
break
else:
buffer += oneByte.decode()
print (buffer.strip())
다음 포스팅에서는 이렇게 받아들인 데이터로 matplotlib.pyplot를 통한 차트 생성 포스팅 예정이다.
'개발 > 파이썬(PYTHON)' 카테고리의 다른 글
[Python] matplotlib.pyplot 활용한 그래프 동적 업데이트 (2/2) (0) | 2021.01.04 |
---|---|
[Python] matplotlib.pyplot 활용한 그래프 동적 업데이트 (1/2) (0) | 2021.01.03 |
[PYTHON] 네이버 주식정보 가져오기 (4/4) - email 보내기 (0) | 2020.12.13 |
[PYTHON] 네이버 주식정보 가져오기 (3/4) (7) | 2020.12.13 |
[PYTHON] 네이버 주식정보 가져오기 (2/4) (8) | 2020.12.01 |
- Total
- Today
- Yesterday
- 아파트
- cortarNo
- 크롤링
- 네이버 주식
- REST API
- 네이버
- 경매
- json
- 매물
- beautifulsoup
- Excel
- tkinter
- DICTIONARY
- 네이버쇼핑
- Export
- 크몽
- matplotlib
- 상가
- 파이썬
- pyplot
- eum.go.kr
- 대항력있는 임차인
- 개발자도구
- 평형정보
- 네이버 부동산
- PYTHON
- 단지정보
- 경제적 자유
- pandas
- 부동산
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |