0

1.Reading strings sent from Arduino on RPI

Below you can see that I am communicating between a rpi and arduino serially. I am sending "start", then move_played to the rpi. The readline() reads until a newline so, first start gets read then the move_played string(it's like read the move played only after encountering the string start)

Python code on RPI

s1 = serial.Serial('/dev/ttyACM1', 9600)
stockfish = Stockfish('/usr/games/stockfish')
while 1 :
    if str(s1.readline(),'UTF-8').strip('\n\r') == 'start' :
           move_played = str(s1.readline(),'UTF-8').strip('\n\r')
           print(move_played)

Code on Arduino

void SendMove();
String movePlayed ="e2e4";
void setup() 
{
  Serial.begin(9600);
}
void loop() 
{
  SendMove();
  exit(0);
}
void SendMove()
{
  Serial.println("start");
  Serial.println(movePlayed);
}

2.Reading strings send from RPI on Arduino

The goal for the arduino is to read the bestMove string after it reads "Rstart". But when I run the code on the rpi(sending the strings). The lcd prints "Rstart" and not the bestMove string. So from what I can see is that once the "Rstart" string is sent , the first readString() in the if condition , reads " Rstart" and the the second readString() in the print statement reads "Rstart".

Why is the string"Rstart" stll in the serial Buffer even after being read once?

Python Code on RPI

bestMove = "b3c7"
s1 = serial.Serial('/dev/ttyACM1', 9600)
s1.write(bytes("Rstart","ascii"))
s1.write(bytes(bestMove,"ascii")) 

Code on Arduino

#include "LiquidCrystal.h"
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // 4 bit mode
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
  lcd.begin(16,2);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available()>0) 
  {
    if(Serial.readString() == "Rstart")
    {
      lcd.setCursor(0,0);
      lcd.print(Serial.readString());
      delay(10000);
      exit(0);
    }
  }
}
  • 1
    it should not work at all. you send "Rstartb3c7" and readString should read it in one piece – Juraj Jul 2 '18 at 14:19
  • so, how do I send it as 2 separate strings and read them separately,I've been trying but can't seem to find out. – Roshan Jul 2 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.