0

I have a Raspberry Pi and an Arduino. On my Arduino I wrote a program, if you see the last two functions i.e. getMove() and SendMove() you will see that I am sending a move serially and the RPi will read the string only after it reads start. It is sending it to the RPi through serial communication using a USB (Note: As you can see the Arduino will send the string only one time and not again and again since once it sends the string the control will go into getMove() which waits for "Rstart").

Once I uploaded the code in the Arduino, I opened the serial monitor to check the output that I should be expecting in my RPi. Then I removed the USB from my laptop and connected the Arduino to the RPi and ran the Python code to read the string sent from the Arduino serially and got the string sent. I ran the code again on the RPi and got the string sent.

Now my question is, the first time when I opened the serial monitor on the laptop there was serial communication between the Arduino and my laptop, so I expected the string I sent to have gone to the laptop. So, when I connected the Arduino to the RPi, I didn't expect a string to be received on the RPi since the string was already sent to the laptop. Then again I ran the code on the RPi but still got the string sent.

Can someone explain how Serial communication works? Once I send something from the Arduino serially, will it always be in the serial buffer?

EDIT:
I have understood that unless the DTR pin of the serial connector is disabled the Arduino will keep resetting itself when connected to something new.

Now my question is, why does Arduino reset itself when I run the Python code (to read the serial buffer) again and again?

ARDUINO CODE

#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);
int SensorValStore_Prev[8][8] = { {0,0,0,0,0,0,0,0}, \
                                  {0,0,0,0,0,0,0,0}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {1,1,1,1,1,1,1,1}, \
                                  {0,0,0,0,0,0,0,0}, \
                                  {0,0,0,0,0,0,0,0} };

int SensorValStore_Curr[8][8]={ {0,0,0,0,0,0,0,0}, \
                                {0,0,0,0,0,0,0,0}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,1,1,1,1,1,1}, \
                                {1,1,0,1,1,1,1,1}, \
                                {0,0,0,0,0,0,0,0}, \
                                {0,1,0,0,0,0,0,0} };
//int SensorValStore_Curr[8][8];
int sum1[8],sum2[8],temp[8][8],k,l,i,j,rank,nextRow;
String file,moveSan,Temp,bestMove;
char Buffer[20];
void doAction();
void ReadSensor();
void Difference();
void binToIntCalc();
void check();
void getMovePlayed();
void SendMove();
void getmove();

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(22,INPUT_PULLUP); // Reed switch - Row1
  pinMode(24,INPUT_PULLUP); // Reed switch - Row2
  pinMode(26,INPUT_PULLUP); // Reed switch - Row3
  pinMode(28,INPUT_PULLUP); // Reed switch - Row4
  pinMode(30,INPUT_PULLUP); // Reed switch - Row5 
  pinMode(32,INPUT_PULLUP); // Reed switch - Row6 
  pinMode(34,INPUT_PULLUP); // Reed switch - Row7 
  pinMode(36,INPUT_PULLUP); // Reed switch - Row8 
  pinMode(2,INPUT_PULLUP);// Push Button
  pinMode(38,OUTPUT);
  pinMode(40,OUTPUT);
  pinMode(42,OUTPUT);
  pinMode(44,OUTPUT);
  pinMode(46,OUTPUT);
  pinMode(48,OUTPUT);
  pinMode(50,OUTPUT);
  pinMode(52,OUTPUT);
  pinMode(3,OUTPUT);
  digitalWrite(38,HIGH);
  digitalWrite(40,HIGH);
  digitalWrite(42,HIGH);
  digitalWrite(44,HIGH);
  digitalWrite(46,HIGH);
  digitalWrite(48,HIGH);
  digitalWrite(50,HIGH);
  digitalWrite(52,HIGH);
  digitalWrite(3,LOW);
}

void loop()
{
  //if( digitalRead(10) == 0 )
  //{
  Serial.print("--------------------Initial Matrix-----------------------------\n");
  Print(SensorValStore_Prev);
  // doAction();
  delay(40);
  Serial.print("--------------------Final Matrix------------------------------\n");
  Print(SensorValStore_Curr);
  delay(40);
  Difference();
  delay(40);
  binToIntCalc();
  delay(40);
  check();
  delay(40);
  getMovePlayed();
  delay(40);
  getMove();
  delay(40);
  //}
}

void Print(int matrix[8][8])
{
  for(i=0;i<8;i++)
  {
    for(j=0;j<8;j++)
    {
      Serial.print(matrix[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void doAction()
{
  nextRow = 0;
  for(i=0;i<8;i++)
  {
    digitalWrite(38 + nextRow,LOW);
    delay(40);
    ReadSensor(i);
    digitalWrite(38 + nextRow,HIGH);
    delay(40);
    nextRow +=2;
  }
}

void ReadSensor(int iter)  
{
  delay(10);
  for(j=0;j<8;j++)
    SensorValStore_Curr[iter][j] = digitalRead(j+2);
  Serial.print("----------------------------------------------");
  Serial.println("Value of sensory board after move is played");
}

void Difference()
{
  for(i=0;i<8;i++)
    for(j=0;j<8;j++)
      temp[i][j] = abs(SensorValStore_Curr[i][j] - SensorValStore_Prev[i[j]);
  Serial.print("-----------------------Difference---------------------------\n");
  Print(temp);
}

void binToIntCalc()
{
  for(i=0;i<8;i++)
    for(j=0;j<8;j++)
    {
      sum1[i] += pow(2,j)*SensorValStore_Prev[i][j];
      sum2[i] += pow(2,j)*SensorValStore_Curr[i][j];
    }
  Serial.print("---------------------sum1 and sum --------------------------\n");
  for(i=0;i<8;i++)
  {
    Serial.print(sum1[i]);
    Serial.print(" ");
  }
  Serial.println();

  for(i=0;i<8;i++)
  {
    Serial.print(sum2[i]);
    Serial.print(" ");
  }
  Serial.println();
}

void check()
{
  k=0;
  l=0;
  for(i=0;i<8;i++)
  {   
    if(sum2[i]>sum1[i])
      k = i;
    if(sum2[i]<sum1[i])
      l = i;
    delay(40);
  }
}

void getMovePlayed()
{
  for(j=0;j<8;j++)
  {
    if(temp[k][j] == 1)
    {
      rank =k+1;
      file = String(char(97+j));
      delay(40);
      moveSan = file + rank;
      Temp = moveSan;
    }
  }
  for(j=0;j<8;j++)
  { 
    if( temp[l][j] == 1)
    {
      rank =l+1;
      file = String(char(97+j));
      delay(40);
      moveSan = file + rank;
      Temp.concat(moveSan);
    }
  }
}

void SendMove(String movePlayed)
{
  Serial.println("start");
  Serial.print(movePlayed);
  Serial.println();
}

void getMove()
{
  while(1)
  {
    if(Serial.available()>0)
    {
      if(Serial.readString() == "Rstart")
      {
        bestMove = Serial.readString(); 
        lcd.setCursor(0,0);
        lcd.print(bestMove);
        delay(5000);
        lcd.clear();  
        Serial.println(bestMove);
        exit(0);
      }
    }
  }
}

PYTHON CODE

import serial
from stockfish import Stockfish
s1 = serial.Serial('/dev/ttyACM0', 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)
                // stockfish.set_position(move_played)
                // print("Rstart")
                // print(stockfish.get_best_move().encoded())
                break
  • the Arduino resets on new usb connection. and of course at power-up setup() runs – Juraj Jun 30 '18 at 6:41
  • new usb connection is serial.Serial('/dev/ttyACM0', 9600) – Juraj Jun 30 '18 at 9:02
-1

It would help to see your code but bear in mind when you connect the serial monitor to your arduino it resets so any code will re-run

  • true, but the connection from PI resets it too – Juraj Jun 30 '18 at 6:38
  • The arduino doesn't know what's connecting to it, opening the serial port from anything will reset it unless asserting the DTR pin on the serial connector is disabled by whatever is connecting. A quick google seems to indicate python on the pi can't reliably disable DTR but a hardware solution could be implemented – Bra1n Jun 30 '18 at 7:53
  • @Ba1n,@juraj I understand that the arduino re-runs, when conected to the rpi or serial monitor. But why does it re-run if I run the code on the rpi again. Shouln't it give back an error that it can't read anything from serial? – Roshan Jun 30 '18 at 8:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.