I am trying to communicate between Arduino Due and Jetson nano through usb serial communication. I want send two separate signals (integer values) from Jetson to Arduino to control a DC motor and servo motor and get their speed and position feedback back to Jetson from Arduino. The value will be greater than 200 ans lesser than 1000.
I have tried sending as bytes but the Arduino is not receiving the data properly when the value is greater than 255. I tried structpack, now I am sending two variables as a single string and at the receiving side of the Arduino I am converting the string back to integers and it is working. I want to know whether this is the only solution for sending integer values greater than 255 ? I tried various suggestions and it didn't work for me. I am attaching my codes:
#python
import serial
import time
import struct
arduino = serial.Serial('/dev/ttyACM0',115200)
time.sleep(1)
while True:
print('Sending 430180')
arduino.write(struct.pack('<6s', b'430180')) # 430 is the servo position and 180 is the DC motor speed
data = arduino.readline()
print(data)
print('Sending 330180')
arduino.write(struct.pack('<6s', b'330200')) # 330 is the servo position and 200 is the DC motor speed
data = arduino.readline()
print(data)
time.sleep(5)
//Arduino
String servo,dc;
int servo1,dc1;
String data_in[6];
void setup()
{
Serial.begin(115200);
}
void loop()
{
if(Serial.available() >= 3)
{
for (int i = 0; i < 3; i++)
{
data_in[i] = Serial.readString();
}
servo= data_in[0].substring(0,3);
dc= data_in[0].substring(3,6);
servo1 = servo.toInt();
dc1 = dc.toInt();
while(Serial.available() ==0)
{
//setting limits for servo position
if(servo1>=300 && servo1<=500)
{
servo_position = servo1
}
else
{
Servo_position = 380 //safer position when the data is garbage value
}
if(dc1>=50)
{
//minimum motor speed = 50 (pwm signal)
motor_speed = dc1
}
}
Serial.println(servo1);
Serial.println(dc1);
}
}
Another issue is the python program is not working in a loop. Any help would be appreciated.