Problem using Arduino DUE

Hi,

i’m facing a problem using Arduino DUE in which i have uploaded this sketch : https://github.com/PFTL/SimpleDaq/blob/master/extras/arduino_firmware/arduino_firmware.ino

So i started with the simple python file at page 43 of the book

import serial
device = serial.Serial('/dev/ttyACM0') # <---- CHANGE THE PORT!
device.write(b'IDN\n')
answer = device.readline()
print('The answer is: {}'.format(answer))
device.close() 

First problem is that python wait indefinitely before line number 4… but if i modify it into…
answer = device.readline(device.inWaiting())
python goes forward but the answer is this : The answer is: b’'

Maybe i’m doing some stupid mistake… someone could help me to solve this simple issue ?

Hello!
Thanks for your message. Indeed, it is something that I need to discuss a bit more on the book.
What I do during the workshops is the following (so you can have a deeper understanding of what is going on):

start python, and then type each line into the interpreter, one by one, and see what happens.

This should give you an output such as:

The answer is: General DAQ Device built by Uetke. v.1.2019

So, you can see the code works by itself. Now, what happens if you run it as a file? It hangs, as you have seen…

So, where is the problem? Can you guess an answer?

Hello !
It works step by step !!

So i guess it was a timing problem, i have searched a bit on Google and see that Arduino performs a reset after establishing a serial connection.

May be i was writing b’IDN\n’ while it was still resetting itself… and so no answer could have come from Arduino.

I have placed a large delay after serial connection :

device = serial.Serial('/dev/ttyACM0') # <---- CHANGE THE PORT!
time.sleep(3)

Thank you !! :+1::+1:

Indeed! That is the problem!
And while you work in the lab, you will see that this can be a problem with other devices as well. Not only when you start the communication, but imagine you send a command that takes longer to run than what you were expecting…

The main problem is that this issues are very hard to debug, because you see no errors, just a program not behaving how you expect. With experience, you’ll be able to quickly catch them before they even appear.