Python Forum
Not receiving serial data from arduino
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not receiving serial data from arduino
#1
Hi guys. So, I am trying to receive serial data from my Arduino Uno.
The Arduino sends the heading from a magnetometer over serial, but the Python code does not receive it.
This is my Python code:
import serial

Arduino = serial.Serial("COM3", baudrate = 9600) # serial device
angle = 0
Compass() #This function draws the compass 

while (True):
    
    try:
        flushInput()
        angle = Arduino.readline()
        angle = angle.strip()
        angle = int(angle)
        compasspointer.settiltangle(-angle+90)  #update heading
        print(angle)
    except:
        pass
My Arduino code:

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

int heading=105;//just a value
void loop()
{
Serial.println(heading);
thanks for your help!
Reply
#2
First thing, did you try to receive data with Arduino Serial Monitor, does it work fine there?
Reply
#3
Another thing is you should catch your exception and print it.
Also you may want to print angle before you use strip(). It may look something like "105\n105\n105\n105\n105\n105\n". Even if you use strip(), it can't be converted to integer like that. But, as said, if this error happens you can't know, because you don't catch the exception.

    except as e:
        print(e)
Reply
#4
Thank you very much. I caught an exception and saw that it was receiving 'b[value]\r\n
Then I googled and found that I should have decoded the data.
Now the code looks:
Arduino = serial.Serial("COM3", baudrate = 9600) # serial device
angle = 0

Compass()
while (True):
    
    try:
        angle = Arduino.readline()
        print(angle.decode('utf-8'))
        angle = int(angle)
        compasspointer.settiltangle(angle) 
    except:
        pass
Thank you very much. This is the second time I am interacting with python code. I am using C/C++ but I can't deny that when you need to write something fast, python is the best way to go
Reply
#5
I am glad you got it working, and thanks for posting your solution!
I regret not mentioning decoding earlier, since not too long ago I had almost same issue because of that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,327 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,913 Aug-13-2023, 12:16 AM
Last Post: cubangt
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 3,869 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,620 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,408 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 2,154 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  Help reading data from serial RS485 korenron 8 13,610 Nov-14-2021, 06:49 AM
Last Post: korenron
  serial connection to Arduino Jack9 4 2,421 Oct-22-2021, 10:18 AM
Last Post: Jack9
  Reading Serial data Moris526 6 5,286 Dec-26-2020, 04:04 PM
Last Post: Moris526
  Serial loopback with Arduino doesn't work ThomasS 3 2,713 Sep-19-2020, 12:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020