From 5c670fc3ae03778e01297e2fa37639e8a84d252d Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 11 May 2024 14:41:57 +0700 Subject: [PATCH] Create serial_communication.py --- hardware_interface/serial_communication.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 hardware_interface/serial_communication.py diff --git a/hardware_interface/serial_communication.py b/hardware_interface/serial_communication.py new file mode 100644 index 0000000..4359281 --- /dev/null +++ b/hardware_interface/serial_communication.py @@ -0,0 +1,20 @@ +import serial + +def open_serial_port(port, baudrate): + # Open a serial port using PySerial or PyVISA + + ser = serial.Serial(port=port, baudrate=baudrate, timeout=1) + + return ser + +def send_serial_data(ser, data): + # Send data over a serial port + + ser.write(data.encode()) + +def receive_serial_data(ser): + # Receive data over a serial port + + data = ser.read(ser.inWaiting()).decode() + + return data