File tree Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+ # SPDX-License-Identifier: MIT
3+
4+ import time
5+ import board
6+
7+ # baud rate for your device
8+ baud = 38400
9+ # Initialize UART for the CH9328
10+ # check for Raspberry Pi
11+ # pylint: disable=simplifiable-condition
12+ if "CE0" and "CE1" in dir (board ):
13+ import serial
14+
15+ uart = serial .Serial ("/dev/ttyS0" , baudrate = baud , timeout = 3000 )
16+ # otherwise use busio
17+ else :
18+ import busio
19+
20+ uart = busio .UART (board .TX , board .RX , baudrate = baud )
21+
22+ print ("Enter commands to send to the RS-232 device. Press Ctrl+C to exit." )
23+ while True :
24+ user_input = input ("Please enter your command: " ).strip ()
25+ if user_input :
26+ # send the command with a telnet newline (CR + LF)
27+ uart .write ((user_input + "\r \n " ).encode ('ascii' ))
28+
29+ # empty buffer to collect the incoming data
30+ response_buffer = bytearray ()
31+
32+ # check for data
33+ time .sleep (1 )
34+ while uart .in_waiting :
35+ data = uart .read (uart .in_waiting )
36+ if data :
37+ response_buffer .extend (data )
38+
39+ # decode and print
40+ if response_buffer :
41+ print (response_buffer .decode ('ascii' ), end = '' )
42+ print ()
You can’t perform that action at this time.
0 commit comments