1
votes

I have a serial connection between my raspberry pi and my arduino. I can send data from arduino to the pi but when I try to convert the receiving data into a int or a float I get an error message.

Let's say I try to send the number 35 to the pi, and try to convert it on the python side I get following message:

invalid literal for int() with base 10:''

and when I try to convert it to float I get the following message:

could not convert string to float.

I'm using Idle 3.5.3 on the raspberry pi. I tried many things I've seen in this forum: like strip() but nothing seems to work. What could be wrong?

Arduino Code:

void setup() {
  Serial.begin(9600);

}

void loop() {

  Serial.println(35);

  delay(5000);


}

Python Code:

#!/usr/bin/env python3
import time
import serial
from array import array
import csv

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


print("test")

while 1:
       incoming = arduino.readline().decode('ascii').strip()
       float(incoming)
       print(incoming)

I expected a cast to int or float but I get only error messages

1

1 Answers

1
votes

I got it.

incoming = arduino.readline().decode('ascii')
    if not incoming is "":

            if int(incoming.strip()) == 1:
                    data.append(float(arduino.readline().decode('ascii').strip())/100)