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
  Data transfer from Arduino to Pyhton code_noob 2 697 Jan-29-2025, 09:57 PM
Last Post: DeaD_EyE
  Listening on receiving Interface (using scapy) CodyTheCodeNoob 1 1,829 Dec-22-2024, 10:51 PM
Last Post: PolandoFaker
  sending strings to arduino over serial/accessing DLLs HeWhoHas 0 809 Nov-09-2024, 06:01 PM
Last Post: HeWhoHas
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 22,684 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Handling receiving updates from user in Telebot mohsenamiri 0 1,449 Aug-26-2024, 09:25 AM
Last Post: mohsenamiri
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 5,528 Aug-13-2023, 12:16 AM
Last Post: cubangt
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 8,645 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 3,301 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 2,170 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Receiving snmp traps with more than one Community String ilknurg 0 3,784 Jan-19-2022, 09:02 AM
Last Post: ilknurg

Forum Jump:

User Panel Messages

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