I am using pyserial to send string value to arduino like this:
#!/usr/bin/python
import serial
import syslog
import time
#The following line is for serial over GPIO
port = '/dev/ttyACM0' # note I'm using Mac OS-X
ard = serial.Serial(port,9600,timeout=5)
time.sleep(2) # wait for Arduino
i = 0
while (i < 4):
# Serial write section
ard.flush()
data = "123"
ard.write(str(data))
time.sleep(1) # I shortened this to match the new value in your Arduino code
# Serial read section
msg = ard.read(ard.inWaiting()) # read all characters in buffer
print ("Message from arduino: ")
print msg
i = i + 1
else:
print "Exiting"
exit()
But when i run it, i donot get the correct value. I wanted to get the same value as i've sent. My arduino code is:
// Serial test script
#include <Servo.h>
Servo joint1;
Servo joint2;
Servo joint3;
int setPoint = 55;
String readString;
char c;
void setup()
{
Serial.begin(9600); // initialize serial communications at 9600 bps
joint1.attach(3);
joint2.attach(5);
joint3.attach(6);
}
void loop()
{
while(!Serial.available()) {}
// serial read section
while (Serial.available())
{
delay(30);
if (Serial.available() >0)
{
c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() > 0)
{
Serial.print("Arduino received: ");
Serial.println(readString);
int pos1 = atoi(readString[0])*100+atoi(readString[1])*10+atoi(readString[2]);
joint1.write(pos1);
Serial.println(pos1);
readString = "";
}
delay(30);
}
Here right now i'm only using one servo command. What i want to do it, i want to first send a string of length 9 like "123456789", than in arduino convert that string into 3 parts as pos1 = 123, pos2 = 456, pos3 = 789. But i'm not able to do it.