Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pyserial ser.read() not working
#1
I'm new to Python and Pyserial.

I have this project that I'm working on where I need to have an Arduino communicate to my Python code through Pyserial.

Here is my Pyserial code.

import serial
import time
import sys

ser = serial.Serial('/dev/cu.usbserial-A505BRMT', 9600)  # open serial port
while True:
    print('write:',str.encode('90')) #print the sent value through the serial to check
    ser.write(str.encode('90'))#send the following value


    
    print('read:',ser.read(0))#read the same value back from the arduino

    time.sleep(1) 
Here is my Arduino code.
char data; //Holds incoming character

void setup()
{
Serial.begin(9600);   //Serial Port at 9600 baud
}

void loop() {
  if (Serial.available() > 0)   //Print only when data is received
  {
    data = Serial.read();   //Read byte of data
    Serial.print(data);   //Print byte of data
  }
}
Basically I'm sending a number (90) to the Arduino to read it in the serial monitor and then send it back to the Python code to read it again.
print('read:',ser.read(0)) This line doesn't seem to work no matter the value I use

Anyone has an idea to fix it?
Quote
#2
Shouldn't it be non-zero value in ser.read(0)? it's the size (in bytes) to read
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Lib PySerial Pelathaon 2 223 Jul-07-2020, 12:41 PM
Last Post: DashOrion
  pySerial why do i need \r Mad_Max 1 279 May-26-2020, 01:29 AM
Last Post: Larz60+
  Unable to read csv file inside full code snippet, at the same time its working seprat sreeraj 2 1,266 Jan-28-2018, 07:25 PM
Last Post: sreeraj
  PySerial Soichiro 1 2,844 Jun-09-2017, 03:56 PM
Last Post: DeaD_EyE
  I need help with pySerial oz1sej 9 13,211 Apr-13-2017, 06:12 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)