Reading values from Arduino with pySerial

I am facing a problem when I am trying to read values from Arduino with pySerial

Python code

import serial
import time
import schedule

def main_func():
    #initialize serial port
    ser = serial.Serial()
    ser.port = 'COM5' #Arduino serial port
    ser.baudrate = 9600
    ser.timeout = 10 #specify timeout when using readline()
    ser.open()
    if ser.is_open==True:
        print("\nAll right, serial port now open. Configuration:\n")
        print(ser, "\n") #print serial parameters


    #Aquire and parse data from serial port
    line=ser.readline()      #ascii
    line_as_list = line.split(b',')
    i = int(line_as_list[0])
    relProb = line_as_list[1]
    relProb_as_list = relProb.split(b'\n')
    relProb_float = float(relProb_as_list[0])
    print(relProb)




print('Program started')

# Setting up the Arduino
# Setting up the Arduino
schedule.every(1).seconds.do(main_func)

while True:
    schedule.run_pending()
    time.sleep(1)`

Arduino Code

#include <Arduino.h>

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

}

int i = 1;           //ith trial
int randNo = 1;      //random integer between 0 or 1 (say, 0 = heads, 1 = tails)
int sumTails = 0;   //add up all occurances of tails and store in sumTails
float relFreq = 0;   //compute and store relative frequency of tails here

void loop() {
  randNo=random(0,2);                    //generate random int between 0 and 1
  sumTails = sumTails + randNo;          //update tails tally
  relFreq = float(sumTails)/float(i);    //update relative frequency

  //report results: ,,
  Serial.print(i);
  Serial.print(',');
  //Serial.print(sumTails);
  //Serial.print(',');
  Serial.print(relFreq);
  Serial.print('\n');
  
  i=i+1;                                 //update ith trial
   
  delay(1000);                           //define update rate

}

Serial monitor show everything correctly, but my Python code prints out the the first value whole time. I have no idea what is wrong with my Python code ?

Be aware when you "open" the connection, your Arduino automatically resets in order to synchronize the communication. So, add a delay after the open of a second and see if that helps.

Thanks for your help. It looks like you solved my problem :slight_smile:

I did also test ChatGPT and here is the solution

import serial
import time
import schedule

def main_func():
    #initialize serial port
    ser = serial.Serial()
    ser.port = 'COM5' #Arduino serial port
    ser.baudrate = 9600
    ser.timeout = 10 #specify timeout when using readline()
    ser.open()
    if ser.is_open==True:
        print("\nAll right, serial port now open. Configuration:\n")
        print(ser, "\n") #print serial parameters

    while True:
        #Aquire and parse data from serial port
        line = ser.readline()      #ascii
        line_as_list = line.split(b',')
        i = int(line_as_list[0])
        relProb = line_as_list[1]
        relProb_float = float(relProb)
        print(relProb_float)

        time.sleep(1)


print('Program started')

# Setting up the Arduino
schedule.every(1).seconds.do(main_func)

while True:
    schedule.run_pending()
    time.sleep(1)
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.