0

I am trying to communicate an Arduino using python. I was able to connect it using the serial module. This is the code:

import serial

while True:
  print "Opening port"
  arduinoData = serial.Serial("com7", 9600)
  print "The port is open"

  while (arduinoData.inWaiting()==0): #I wait for data
    print "There is no data"

  print "Reading data"
  arduinoString = arduinoData.readline()
  print arduinoString

It seems that is hanging when I want to read the data, in the line that says arduinoString = arduino.readline().

What could be the problem?

3 Answers 3

1

instead using the while loop inside of the main while loop you can use an if else statement. Also, to read the data you can use the read function with arduinoData.inWaiting() as the paramater like this : arduinoData.read(arduinoData.inWaiting()). I hope this code will help you:

arduinoData = serial.Serial("com7", 9600)

while True:

    if arduinoData.inWaiting() > 0: # check if there is data available
        print "Reading data"
        arduinoString = arduinoData.read(arduinoData.inWaiting()) '''read and decode data'''
        print arduinoString

    else:
        print "There is no data"
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. I get this error: AttributeError: 'Serial' object has no attribute 'ser'. But when i delete ser , it does not open the port. Am i doing something wrong?
Sorry if this question seems goofy but, did you imported the serial module first?
Yes I did import serial first. I just did not post it in that part of the code.
Sprry for late reply, i had some problems with my pc. Thank you a lot, this worked and is reading the data. Now i have to "translate" it, but that is another thing. Thank you a lot again.
No problem. I'm glad this helped.
1

The structure of your code is strange. I had a similar issue by creating the Serial object in a function without making it global. Maybe you should put this line outside the loop :

arduinoData = serial.Serial("com7", 9600)

Also, your initialization seems a bit light. I usually use more parameters but it depends of your hardware.

ser = serial.Serial( 
    port = 'com4', \
    baudrate = 19200, \
    parity=serial.PARITY_NONE, \
    stopbits=serial.STOPBITS_ONE, \
    bytesize = serial.EIGHTBITS, \
    timeout = 0.25)

A workaround for your readline() issue coud be using the read() function instead and checking if it contains data.

Hope it will help !

1 Comment

Sorry for late reply. Yes, you are right. I must set more paremeters for the best. Thank you for your answer and help.
0

Alright, you are getting the AttributeError: 'Serial' object has no attribute 'ser' error because in reality ser does not exist in the arduinoData object. It's my fault because I was thinking of the class that I created in my program containing ser which is just the another serial object. To fix this just replace arduinoData.ser with arduinoData

To add on, you should probably declare arduinoData outside of the while loop. you should do this because every time the you create a serial object it takes time to connect to the Arduino. For this, your program might not be able to read the data.

I hope this answer will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.