I have given UART communication a go, and seem to be having a slight problem. Even though the COM port is defined as THS1 and not ACM0, the code only works when the USB cable is plugged into the Nano. When the USB cable is plugged in, serial communication works as intended (even though it should be using THS1 to transmit/receive data, i.e. the RX/TX GPIO pins.) When the USB cable is unplugged, the code that was previously flashed to the Arduino no longer works, and doesn’t seem to respond when serial data is sent via the RX/TX pins.
I am looking to calculate two stepper motor values to move in the x and y axes respectively within the python script, and then send these two values down the UART port to the Arduino where it will use those stepper motor values to move two NEMA17 stepper motors using the A4988 stepper motor drivers. The Arduino code also sends a signal to a relay to actuate a laser pointer. The code below is purely to get the communication working so that I can send data from the Nano to the Uno - the data should just be two numbers, so in the code I have sent “500 500” to the Nano.
Python script:
import serial
print("UART Demonstration Program")
print("NVIDIA Jetson Nano Developer Kit")
serial_port = serial.Serial(
port="/dev/ttyTHS1",
baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
# Wait a second to let the port initialize
time.sleep(1)
try:
# Send a message to the Arduino
serial_port.write("500 500".encode())
while True:
if serial_port.inWaiting() > 0:
data = serial_port.readline().decode()
print(data)
except KeyboardInterrupt:
print("Exiting Program")
except Exception as exception_error:
print("Error occurred. Exiting Program")
print("Error: " + str(exception_error))
finally:
serial_port.close()
pass
Arduino script:
int data1;
int data2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
{
delay(10);
data1 = Serial.readStringUntil(' ').toInt();
data2 = Serial.readStringUntil(' ').toInt();
delay(10);
Serial.print("\nReceived Data:");
Serial.print(data1, data2);
}
}