From 4aa63fd4ecb95e1d693c2135aefd133a1436752f Mon Sep 17 00:00:00 2001 From: Liz Date: Thu, 11 Jul 2024 12:27:11 -0400 Subject: [PATCH 1/2] rs-232 pal demos CircuitPython and Arduino RS-232 Pal demos. uses serial console to send commands --- .../Arduino_RS-232_Pal/.uno.test.only | 0 .../Arduino_RS-232_Pal/Arduino_RS-232_Pal.ino | 46 +++++++++++++++++++ RS-232_Pal_Demos/CircuitPython/code.py | 32 +++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 RS-232_Pal_Demos/Arduino_RS-232_Pal/.uno.test.only create mode 100644 RS-232_Pal_Demos/Arduino_RS-232_Pal/Arduino_RS-232_Pal.ino create mode 100644 RS-232_Pal_Demos/CircuitPython/code.py diff --git a/RS-232_Pal_Demos/Arduino_RS-232_Pal/.uno.test.only b/RS-232_Pal_Demos/Arduino_RS-232_Pal/.uno.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/RS-232_Pal_Demos/Arduino_RS-232_Pal/Arduino_RS-232_Pal.ino b/RS-232_Pal_Demos/Arduino_RS-232_Pal/Arduino_RS-232_Pal.ino new file mode 100644 index 000000000..d65dd108d --- /dev/null +++ b/RS-232_Pal_Demos/Arduino_RS-232_Pal/Arduino_RS-232_Pal.ino @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include + +// update this for your RS-232 device baud rate +#define baud 38400 +// define RX and TX pins for the software serial port +#define RS232_RX_PIN 2 +#define RS232_TX_PIN 3 + +SoftwareSerial rs232Serial(RS232_RX_PIN, RS232_TX_PIN); + +void setup() { + Serial.begin(115200); + while ( !Serial ) delay(10); + + rs232Serial.begin(baud); + + Serial.println("Enter commands to send to the RS-232 device."); + Serial.println(); +} + +void loop() { + + if (Serial.available() > 0) { + String userInput = Serial.readStringUntil('\n'); + userInput.trim(); // remove any trailing newlines or spaces + if (userInput.length() > 0) { + // send the command with a telnet newline (CR + LF) + rs232Serial.print(userInput + "\r\n"); + Serial.print("Sent: "); + Serial.println(userInput); + } + } + + // check for incoming data from RS-232 device + while (rs232Serial.available() > 0) { + char response = rs232Serial.read(); + // print the incoming data + Serial.print(response); + } + + delay(50); +} diff --git a/RS-232_Pal_Demos/CircuitPython/code.py b/RS-232_Pal_Demos/CircuitPython/code.py new file mode 100644 index 000000000..a7ab3613b --- /dev/null +++ b/RS-232_Pal_Demos/CircuitPython/code.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import board +import busio + +# baud rate for your device +baud = 38400 +uart = busio.UART(board.TX, board.RX, baudrate=baud) + +print("Enter commands to send to the RS-232 device. Press Ctrl+C to exit.") +while True: + user_input = input("Please enter your command: ").strip() + if user_input: + # send the command with a telnet newline (CR + LF) + uart.write((user_input + "\r\n").encode('ascii')) + + # empty buffer to collect the incoming data + response_buffer = bytearray() + + # check for data + time.sleep(1) + while uart.in_waiting: + data = uart.read(uart.in_waiting) + if data: + response_buffer.extend(data) + + # decode and print + if response_buffer: + print(response_buffer.decode('ascii'), end='') + print() From b5ee85acea231a2b33efe6630a04a1239625f0fa Mon Sep 17 00:00:00 2001 From: Liz Date: Thu, 11 Jul 2024 14:53:26 -0400 Subject: [PATCH 2/2] update for rpi --- RS-232_Pal_Demos/CircuitPython/code.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/RS-232_Pal_Demos/CircuitPython/code.py b/RS-232_Pal_Demos/CircuitPython/code.py index a7ab3613b..a5e6fa332 100644 --- a/RS-232_Pal_Demos/CircuitPython/code.py +++ b/RS-232_Pal_Demos/CircuitPython/code.py @@ -3,11 +3,21 @@ import time import board -import busio # baud rate for your device baud = 38400 -uart = busio.UART(board.TX, board.RX, baudrate=baud) +# Initialize UART for the CH9328 +# check for Raspberry Pi +# pylint: disable=simplifiable-condition +if "CE0" and "CE1" in dir(board): + import serial + + uart = serial.Serial("/dev/ttyS0", baudrate=baud, timeout=3000) +# otherwise use busio +else: + import busio + + uart = busio.UART(board.TX, board.RX, baudrate=baud) print("Enter commands to send to the RS-232 device. Press Ctrl+C to exit.") while True: