[EDIT] Clarification of question. [/EDIT]
I have connected my Arduino Nano with a Raspberry Pi 3 using Serial transmission. The Pi uses a Python script which sends a string to the Arduino. Once this is received, the Arduino increments the first two digits (representing an index), overwriting the original char array in which the string is stored. This is then sent back to the Pi.
The weird thing is this; while the index (first two digits) is successfully incremented and over-written, other random two digits are also getting incremented. This is very weird since as you can see in my code below, the function altering these digits (buildIndex()) is specifically calling characters at locations 0 and 1 in my array.
Any idea what could be causing this? I have also, at one point, included a counter with the serialEvent function to check how many times it is being called. The counter increments twice for each string sent, while it should be just once obviously.
Code:
#include <LiquidCrystal.h>
#include <stdio.h>
#include <stdlib.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char sign[300]="";
int init_index = 0;
int rgb_values = 0;
int sensor_location = 0;
bool chk = false;
void setup(){
Serial.begin(115200);
lcd.begin(16,2);
lcd.print("Hello World");
}
void refreshSign() {
for(int i = 0; i < sizeof(sign); i++) {
sign[i] = '\0';
}
Serial.flush();
}
void parseSign() { //parse the sign into a string
char tmp[2];
//bool chk = false;
tmp[0] = sign[0];
tmp[1] = sign[1];
init_index = atoi(tmp);
}
void buildIndex() { //building of index into new sign
//chk = true;
int incr_index = init_index+1;
//Serial.write(incr_index);
char tmp[2];
itoa(incr_index, tmp, 10);
if(incr_index <= 9) {
sign[0] = '0';
sign[1] = tmp[0];
} else {
sign[0] = tmp[0];
sign[1] = tmp[1];
}
//Serial.write(sign);
}
void serialEvent() { //to trigger upon receiving sign
int index = 0;
lcd.clear();
while(Serial.available() > 0) {
if(index < sizeof(sign)) {
sign[index] = Serial.read();
index++;
} else {
char ext = Serial.read();
}
}
sign[index]='\0';
parseSign();
buildIndex();
for(int i=0; i < index; i++) {
Serial.print(sign[i]);
lcd.print(sign[i]);
}
refreshSign();
}
void loop() {
}
Python Code on Pi:
if __name__ == "__main__":
t0 = datetime.datetime.now()
t2=datetime.datetime.now()
t1=datetime.datetime.now()
s=""
s1=""
#testStr = "(233,43,12):(13,13,13):(24,5,67)%(3.05,4.06,12.3)%3"
try:
with serial.Serial('/dev/ttyAMA0', baudrate=115200,rtscts=True, dsrdtr=True, timeout=0.05) as port:
s1 = "03abcdefghijkl%112233445566778899001122\0"
print s1
t1=datetime.datetime.now()
print("Pre IO: "+ str((t1-t0).total_seconds()*1000)+"ms")
port.write(s1)
print("write IO: "+ str((t1-t0).total_seconds()*1000)+"ms")
s = port.readline()
except serial.SerialException:
port.close()
port.open()
except Exception as e:
print(e)
pass
t2=datetime.datetime.now()
print (s)
print("read IO: "+ str((t2-t1).total_seconds()*1000)+"ms")
if port.open():
port.close()
print("End: "+ str((datetime.datetime.now()-t2).total_seconds()*1000)+"ms")
print("Total: "+ str((datetime.datetime.now()-t0).total_seconds()*1000)+"ms")