Python – Arduino serial via python not working

arduinopython

I have a arduino running with bitlash running on it connected to a shiftbrite.

I can use a terminal program to send it commands such as rgb(1023,0,1023) and it will work but when I do it with python it doesn't.

Here is my python code. I don't know why it doesn't work. I can even print the string on an LCD connected to it and it comes out correctly

#Python Menu
#File: PythonMenu.py
#Basic menu system for use with Arduino and Shiftbrites
#over a wireless xbee link

import os #For clear
import time #For delay
import serial #For serial comms to the device

def printMenu():
    """Prints the menu ready with all options"""
    print "/--------------------------------------------\\"
    print "|      |  | |  | /--- |  |  __  ---|         |"
    print "|      |--| |  | |  - |--| |__ |___          |"
    print "|      |  | |__| |__| |  | |__  ___|         |"
    print "|--------------------------------------------|"
    print "| Please select from the following options   |"
    print "|                                            |"
    print "| (1) White                                  |"
    print "| (2) Red                                    |"
    print "| (3) Green                                  |"
    print "| (4) Blue                                   |"
    print "| (5) Fade                                   |"
    print "| (6) Off                                    |"
    print "|--------------------------------------------|"
    print "| (0) Exit                                   |"
    print "\\--------------------------------------------/"


def clear():
    """Clears the console screen"""
    os.system('clear')

def sendColour( r, g, b ):
    """Sends a RGB value to the shiftbrite"""
    #build up string
    tmpData = "rgb("
    tmpData += str(r)
    tmpData += ","
    tmpData += str(g)
    tmpData += ","
    tmpData += str(b)
    tmpData += ")"
    ser.write(tmpData)
    print(tmpData)

    return

###
#
###

#Setup serial
ser = serial.Serial('/dev/tty.usbserial-A600dJm8',9600)
#Start main code LOOP
while True:
    clear()
    printMenu()
    userInput = raw_input('Selection:')
    if userInput == '1':
        sendColour(1023,1023,1023)
    elif userInput == '2':
        sendColour(1023,0,0)
    elif userInput == '3':
        sendColour(0,1023,0)
    elif userInput == '4':
        sendColour(0,0,1023)
    elif userInput == '5':
        sendColour(0,0,0) #To be implemented
    elif userInput == '6':
        sendColour(0,0,0)
    elif userInput == '0':
        print "Quitting"
        ser.close()
        exit()
    time.sleep(2)

Anyone got any ideas?

Best Answer

I think you are forgetting a line-feeds / carriage-return character to finish off the command. If you wrote a command in the terminal and hit enter they would be sent.

Change code to:

def sendColour( r, g, b ):
    """Sends a RGB value to the shiftbrite"""
    #build up string
    tmpData = "rgb("
    tmpData += str(r)
    tmpData += ","
    tmpData += str(g)
    tmpData += ","
    tmpData += str(b)
    tmpData += ")\n"
    ser.write(tmpData)
    print(tmpData)
    return

Note the \n.

The bit lash-cmdline.c file has this in it:

void doCharacter(char c) {
if ((c == '\r') || (c == '\n') || (c == '`')) {
    speol();
    *lbufptr = 0;
    doCommand(lbuf);
    initlbuf();
}

(rest of function not included)

This suggests a '\r', '\n' or a '`' character can be used to terminate the command.