I have an issue with sending serial data through python. Let me explain it this way. I have a motor code on arduino which receives data from serial and moves through the received data. However, whenever I send data in this case 8 lists, it doesn't read the 8th list. By playing with time.sleep in python code I could see some differences(I could see 4 lists being sent,then I increased time.sleep to 8 and now it sends 7 lists.) I know playing with time.sleep isn't a solution though. I have also tried emptying input and output buffers and I am out of ideas on what to do next. Here are the codes. The Python Codes: The incoming byte will be the data sent as you can see with a for loop, I am sending the lists. The first three values inside each list is to control functions. 1,0,0 means that it will only work the motor function which I have shared in the arduino part. The 3rd and 4th variables here are for controlling the motor, meaning 1st motor will turn 2000 steps and the other will turn for a 1000 steps.
import time
import serial
def close():
arduino=serial.Serial("COM4",9600)
time.sleep(7)
incomingByte=[[1,0,0,2000,1000,1000],[1,0,0,200,100,1000],[1,0,0,250,650,1000],[1,0,0,1000,1000,1000],[1,0,0,2000,1000,1000],[1,0,0,200,100,1000],[1,0,0,250,650,1000],[1,0,0,1000,1000,1000]
length= len(incomingByte)
for i in range(uzunluk):
arduino.write(str.encode(str(incomingByte[i])))
arduino.reset_input_buffer()
arduino.reset_output_buffer()
time.sleep(7)
arduino.close()
close()
The Arduino Code:
void setup() {
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(enable,OUTPUT);
pinMode(endstop1, INPUT);
pinMode(endstop2, INPUT);
Serial.begin(9600);
void motor(){
for (i=0; i < incomingdata[3]; i++) {
digitalWrite(stepPin1,HIGH);
delay(2);
digitalWrite(stepPin1,LOW);
delay(2);
}
for (i=0; i < incomingdata[4]; i++) {
digitalWrite(stepPin2,HIGH);
delay(2);
digitalWrite(stepPin2,LOW);
delay(2);
}
act_motor=0;
delay(incomingdata[5]);
Serial.flush();
}
void loop() {
if(Serial.available()){
for (a=0; a < 6; a++) {
incomingdata[a] = Serial.parseInt();
}
identifier=(incomingdata[2]);
act_enable=(incomingdata[1]);
act_motor=(incomingdata[0]);
}
if (identifier) homee();
if (act_enable) Enable();
if (act_motor) motor();
}
To simply explain the arduino code; according to the serial data the 3rd and 4th data from each array will be step numbers of how much the stepper motors will move. The 5th data on the list will be the delay for each of the motors kept waiting. I hope I explained this clearly. I would be glad if anyone could help. P.S: I am sorry If I have violated any rules. I will be sure to correct my mistakes.