1

I connect my Rasberry Pi to Arduino Nano (Uno) and all works when I send every command through python shell:

pi@raspberrypi ~ $ python2
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/arduino_nano', 9600)
>>> ser.write('4')
1

If I'm trying run script (below) with this commands, don't happen anything. A little note: if I run scripts with running python shell in other SSH session, script works excellent, and if I'm running screen command (sudo screen /dev/ttyUSB0 115200), script also works well. Script:

#!/usr/bin/env python

import serial
import time

ser = serial.Serial('/dev/arduino_nano', 9600)
ser.write('4')

print("Test")

So, I think my problem relationships with run serial connect.

  • 2
    How are you trying to run this script? Also your interactive example is python2, but your code is python3(ish). – Nick Bastin Sep 14 '15 at 7:30
  • I run this command: pi@raspberrypi ~ $ python2 /home/pi/scripts/go.py. And see - "Test". Why you think that my code for python3? I send command to python shell v.2. – Fedor Malkin Sep 14 '15 at 11:06
  • print("this") is python3, print "this" is python2 – bryn Dec 14 '15 at 10:04
1

Your problem is the definition of "don't happen anything". For what you've clarified in the comment, when you run the script, the script shows you the "Test", meaning that went through all the commands. The only difference is that it is not showing you the output of the ser.write('4') command.

If you want to be sure that you wrote to the Arduino, you can try something like:

#!/usr/bin/env python
import serial

ser = serial.Serial('/dev/arduino_nano', 9600)
ans = ser.write('4')
print(ans)
print("Test")
ser.close()

Note that I've also added the ser.close() command to be sure that you are liberating the arduino after the execution. It may not look important now, but trust me that when your system becomes more complex (more devices, more process running simultaneously, etc.) you really want to close the communication with the device and ensure no data is lost.

Moreover, you can try:

#!/usr/bin/env python
import serial

with serial.Serial('/dev/arduino_nano', 9600) as ser:
    ans = ser.write('4')
    print(ans)
    print("Test")
  • Thanks for your reply, but your advice don't resolve my problem. I try add to the script delay between a commands (time.sleep(2)), and my scripts works. But not as it should - if I run one script with send a number 5 and run script with number 4 (for example), between running those scripts I have a delay, when don't happen anything. – Fedor Malkin Sep 14 '15 at 19:33
0

I had the same problem today using an Arduino Uno (not an Arduino Nano). It worked in the Python console, but it did not work for the Python script. It looks as if the Serial Connection takes a while until is established. I was able to get it running by using a sleep command of 1.5 seconds:

import serial
import time
usb = serial.Serial('/dev/arduino_nano', 9600)
time.sleep(1.5)
usb.write('4')
usb.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.