Skip to content

Commit 4aa63fd

Browse files
committed
rs-232 pal demos
CircuitPython and Arduino RS-232 Pal demos. uses serial console to send commands
1 parent 4df862a commit 4aa63fd

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

RS-232_Pal_Demos/Arduino_RS-232_Pal/.uno.test.only

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <SoftwareSerial.h>
6+
7+
// update this for your RS-232 device baud rate
8+
#define baud 38400
9+
// define RX and TX pins for the software serial port
10+
#define RS232_RX_PIN 2
11+
#define RS232_TX_PIN 3
12+
13+
SoftwareSerial rs232Serial(RS232_RX_PIN, RS232_TX_PIN);
14+
15+
void setup() {
16+
Serial.begin(115200);
17+
while ( !Serial ) delay(10);
18+
19+
rs232Serial.begin(baud);
20+
21+
Serial.println("Enter commands to send to the RS-232 device.");
22+
Serial.println();
23+
}
24+
25+
void loop() {
26+
27+
if (Serial.available() > 0) {
28+
String userInput = Serial.readStringUntil('\n');
29+
userInput.trim(); // remove any trailing newlines or spaces
30+
if (userInput.length() > 0) {
31+
// send the command with a telnet newline (CR + LF)
32+
rs232Serial.print(userInput + "\r\n");
33+
Serial.print("Sent: ");
34+
Serial.println(userInput);
35+
}
36+
}
37+
38+
// check for incoming data from RS-232 device
39+
while (rs232Serial.available() > 0) {
40+
char response = rs232Serial.read();
41+
// print the incoming data
42+
Serial.print(response);
43+
}
44+
45+
delay(50);
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import busio
7+
8+
# baud rate for your device
9+
baud = 38400
10+
uart = busio.UART(board.TX, board.RX, baudrate=baud)
11+
12+
print("Enter commands to send to the RS-232 device. Press Ctrl+C to exit.")
13+
while True:
14+
user_input = input("Please enter your command: ").strip()
15+
if user_input:
16+
# send the command with a telnet newline (CR + LF)
17+
uart.write((user_input + "\r\n").encode('ascii'))
18+
19+
# empty buffer to collect the incoming data
20+
response_buffer = bytearray()
21+
22+
# check for data
23+
time.sleep(1)
24+
while uart.in_waiting:
25+
data = uart.read(uart.in_waiting)
26+
if data:
27+
response_buffer.extend(data)
28+
29+
# decode and print
30+
if response_buffer:
31+
print(response_buffer.decode('ascii'), end='')
32+
print()

0 commit comments

Comments
 (0)