save_jeff
Posts: 27
Joined: Thu Sep 27, 2012 8:49 am

Send Commands over Serial - Arduino

Wed May 08, 2013 4:42 pm

hallo.

I wrote a little programm in python, that sends a little command like "P081" over the serial port to an Arduino UNO board.
The Arduino board reacts and works perfectly fine as long as the "Serial Monitor" is opend in the Arduino 1.0.1 application.

But if the Serial Monitor is closed .. the Arduino does nothing. :shock:

I start the programm with:

Code: Select all

sudo python ~/python/SendOverSerial.py P081
The Python programm is:

Code: Select all

import serial
import sys
ser = serial.Serial('/dev/ttyACM0','9600')
commands = sys.argv[1]
ser.write(commands)
What changes if i start the serial monitor? :?:

Jeff

User avatar
mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Send Commands over Serial - Arduino

Fri May 10, 2013 4:41 pm

try something like this:

install pyserial, if you have allready not (i guess you have)

get the zipped tar file from http://sourceforge.net/projects/pyseria ... urce=files

extract it

in terminal, go:

cd pyserial-2.5 (replace 2.5 if higher version is released)

sudo python setup.py install

then, some of the first bit of code could look like this:

Code: Select all

import serial
ser = serial.Serial('/dev/ttyACMO', 9600) # this opens up connection from pi to arduino

[code/]

try it!

hope this helps.
    mrpi64
I'm happy to help.
https://www.raspberrypi.org/forums/viewtopic.php?f=78&t=51794 - List of games that work on the Pi.

save_jeff
Posts: 27
Joined: Thu Sep 27, 2012 8:49 am

Re: Send Commands over Serial - Arduino

Sat May 11, 2013 10:34 am

I used pyserial already in the first code.
It works so far then that the serialport-led on the arduinoboard blinks but the arduino board doesn't do anything

Might this be caused by some Debugging mode over usb?

The Python programm works perfectly fine as long as the serial monitor in the Arduino Apllication is opend

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Send Commands over Serial - Arduino

Sat May 11, 2013 11:54 am

This is what I use , where Vcorrt / Hcorrt= ':Mgn1000' for example

ser = serial.Serial('/dev/ttyACM0',9600)
ser.write(bytes(Vcorrt.encode('ascii')))
time.sleep(0.5)
ser.write(bytes(Hcorrt.encode('ascii')))

It seems to need the delay between commands

Gordon77

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Send Commands over Serial - Arduino

Sat May 11, 2013 12:22 pm

Opening the serial port most probably resets the arduino by toggling the DTR line.

You can test this by making the sketch blink the LED (DIO13) in setup(). Chances are the LED will blink every time you start the program.

There are 2 ways to fix this: mod the Arduino board (basically disconnect DTR from the reset pin) or put in a sleep (open port, wait for bootloader to time out, send command).

Return to “Python”