Problem Serial Communication Between Pi And Arduino

Hi there,

I’m working on my final task in school and am stuck with the serial communication between a Pi and Arduino.

I am using a HC-06 and HC-05 module for this to work. I’ve already gotten communication to work between the 2 codes in one direction, I don’t need any more than this.

Here comes the problem: I need to send 2 numbers between 0-100. These represent the PWM cycle for 2 DC motors. (this has to be quite fast) If I do this I get 2 things that happen depending on how I read it.

Or I get these numbers all behind eachother, for example: If I send 90 and 85 I get: “908590859085…” in 1 string.
Or I get with the same example: “9”, “0”, “8”, “5”, etc… All in seperate strings.

What I need is to get these as 90 and 85 as a final product.

Using substrings may solve this but I have to constantly refresh the motors so I can’t wait for a long string to divide. I hope I explained this clearly enough.

I use a Arduino UNO and pins 2 and 3 with softwareserial. I use a PCB so I can’t chance these pins altough this really shouldn’t be a problem.

If anyone knows how to do this or could give some guidelines, feel free to leave some advice or answers.

My code should be in the attachment, I’ll leave my python code for good measure if this may give some insight in my thougt proces.

Thanks in advance.

Lander Renaerts

Arduino code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int totaalmotor1 = "0";
int motorlinks = 0;
int motorrechts = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Ready to receive!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(totaalmotor1 = mySerial.read());
    Serial.println("\n");
  }
}

Python code

import time
import serial

ser = serial.Serial(
	port='/dev/ttyS0',
	baudrate = 9600,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS,
	timeout=1
	)

while 1:
	send = input("What to send: ")
	ser.write(str(send))
	time.sleep(1)

arduino_gip.ino (602 Bytes)

What I need is to get these as 90 and 85 as a final product.

Take a look at Serial input basics - updated

Put start and end markers on the data pairs and a separator character between them to create a data packet then you can receive the packet and parse it into separate values to be used on the receiver

You can use SerialTransfer.h (installable through the Arduino IDE) and pySerialTransfer (pip-installable) to easily communicate between your Arduino and Python.

Example Arduino Sketch:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(115200);
  myTransfer.begin(mySerial);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
 
  myTransfer.sendData(3);
  delay(100);

  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Example Python Script:

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('COM13')
        
        link.open()
        sleep(2) # allow some time for the Arduino to completely reset
    
        link.txBuff[0] = 'h'
        link.txBuff[1] = 'i'
        link.txBuff[2] = '\n'
        
        link.send(3)
        
        while not link.available():
            if link.status < 0:
                print('ERROR: {}'.format(link.status))
            
        print('Response received:')
        
        response = ''
        for index in range(link.bytesRead):
            response += chr(link.rxBuff[index])
        
        print(response)
        link.close()
        
    except KeyboardInterrupt:
        link.close()