Send data from Arduino to raspberry pi through USB cable (Solved)

Imgur: The magic of the Internet <—link to what I am getting
Hi there, its my first time posting here, I have the following problem. The data I am sending is getting read very weirdly, as you can see on the screenshot . The code I use to take the readings is this:
This code is taking readings from load cells(Its taken from SparkFun).

#include "HX711.h"

#define calibration_factor -7050.0

#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);

void setup() {
  Serial.begin(115200);
  //Serial.println("Scale Test");
  
  scale.set_scale(calibration_factor);
  scale.tare();
  
  //Serial.println("Reading: ");
}

void loop(){
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" kg");
  Serial.println();
  delay(1000);
}

and use this code to read the data from the serial port:

import serial
import time

def Read_Weight():
        while 1:
                ser = serial.Serial(
                        port='/dev/ttyACM0',
                        baudrate = 115200,
                        bytesize = serial.EIGHTBITS,
                        parity = serial.PARITY_NONE,
                        stopbits = serial.STOPBITS_ONE,
                        xonxoff = False,
                        rtscts = False,
                        dsrdtr = False,
                        timeout = 1
                        )

                #force = False



                data = ser.readline()
                return data

#while 1:
#       x = ser.readline()
#       print x

My question is, whats the reason for the data being sent being so random and corrupted? Random letters are being removed, or added. Is it because the code on Arduino is in different language than the one on raspberry pi? I am sorry if its something obvious but I am very green in all this. :slight_smile:

Hi,

just an assumption: you re-open the serial port in every iteration of you while loop. Try to move the initialization of the serial port outside the loop amd only do the reading inside.

Besides: try to check the data transmitted with a logic analyzer or by simply attaching it to a serial port on your computer and display the data to verify what is actually send.

import serial
import time

def Read_Weight():
        time.sleep(2)

        ser = serial.Serial(
        port='/dev/ttyACM0',
        baudrate = 115200,
        bytesize = serial.EIGHTBITS,
        parity = serial.PARITY_NONE,
        stopbits = serial.STOPBITS_ONE,
        xonxoff = False,
        rtscts = False,
        dsrdtr = False,
        timeout = 1
        )

        while 1:

                for _ in range(10):
                        data = ser.readline()
                        return data
#                       Serial.flush();
                Serial.flush();

Thats the code after I did all I could think off, but I still loose data the same as before, its bit more rare but still happens. :confused:

Have a look at this Python - Arduino demo

...R

#include "HX711.h"

#define calibration_factor -7050.0

#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);

void setup() {
  Serial.begin(115200);
  scale.set_scale(calibration_factor);
  scale.tare();
}

void loop(){
  Serial.println(scale.get_units(), 1);
  delay(1000);
}

The Problem I found was that when sending numerous Serial.print() they somehow mix, so sending a single one fixes the issue, as well as Serial.flush() every 10 repeats helps with the data corruption.