본문 바로가기

찐s/Raspberry Pi

[RPi] Lab.02-3: UART

PC 와 라즈베리파이 UART 를 이용하여 시리얼 통신을 실습한다.

[PC]
[serialRx.m]

%%%%%serialRx.m------------------------------%%%%% 
clear 
clc 
%%%%%----------------------------------------%%%%% 
uart = serialport('COM#', 115200, 'TimeOut',1); 
flush(uart); 
%%%%%----------------------------------------%%%%% 
write(uart, 's', 'char'); 
pause(0.1); 
rxd = read(uart, 16, 'string') 
pause(0.1); 
write(uart, 'e', 'char'); 
clear uart

[serialRx.py]

#####serialRx.py-----------------------------##### 
import serial 
import time 
#####----------------------------------------##### 
uart = serial.Serial('COM#', 115200, timeout=1) 
uart.flushInput() 
#####----------------------------------------##### 
uart.write(b's') 
time.sleep(0.1) 
rxd = uart.readline() 
print(rxd) 
time.sleep(0.1) 
uart.write(b'e') 
uart.close()


[RPi]
[serialTx.py]

import serial
import time

uart = serial.Serial('/dev/ttyAMA1', 115200, timeout=1)
uart.flushInput()           # 수신버퍼 지우기

try:
    print('Ready')
    time.sleep(1)
    while True:
        rxd = uart.read()
        if rxd == b's':
            print('Start')
            for x in ['01', '02', '03', '04', '05', '06', '07', '08']:
                uart.write(x.encode())
        if rxd == b'e':
            print('End')

except KeyboardInterrupt:
        uart.close()


[Test Result]



[참조]
www.raspberrypi.org

 

'찐s > Raspberry Pi' 카테고리의 다른 글

[RPi] Lab.04-1: piCamera  (0) 2020.12.05
[RPi] Lab.03: UDP socket communication  (0) 2020.11.29
[RPi] Lab.02-2: UART (RPi 4B)  (0) 2020.11.22
[RPi] Lab.02-1: UART (RPi 3B)  (0) 2020.11.21