None blocking bi serial communication between a microcontroller and SoC (e.g. RPI/Linkit/Yun)

If the yun bridge is not suffiecient for your, due to the fact that you would like to use bi directional communcation, you could disable the yun bridge and use the serial1 communcation between the MCU and SoC part of the Yun/Linkit. Or you want to setup a serial connection between an arduino and RPI, please see this post for the wiring.

For the software side you should install pyserial with: pip install pyserial

Please use the following code for bidirectional and none blocking communication.

The python part:

import serial
import time

s = serial.Serial("/dev/ttyS0", 57600, timeout=0.1)
s.write("Hello")
arduinodata = s.readline()

while (True):
    if (s.inWaiting()>0): #if incoming bytes are waiting to be read from$
                arduinodata = s.readline()
                print(arduinodata)

The C code:

String incoming_data;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  Serial1.begin(57600);
}

void loop() {
  // put your main code here, to run repeatedly:
  incoming_data  = Serial1.readStringUntil('\n');
  
  if(incoming_data!="") { 
    
    if(incoming_data=="Hello") {
      Serial1.println("World");
      Serial.println("World");
    }
  }
  Serial1.println("Writing in none blocking mode");
  delay(50);
  
}

I would like to thank Michael Kirsch for above code.

Leave a Reply

Your email address will not be published.