Parse data in Python


I've got project which needs to take input from a set of switches (keypad) on an arduino, send the keypress to my windows box where it will be read, probably by python, and then a keystroke sent to the currently active window. It is going to be a way to control a carpc with the buttons from a standard head unit. I've got the circuit and connections sorted, and I can send the data, but i would like some advice on the best way to implement the serial.

Is Python going to be the easiest and least resource hungry way to do this? Can anyone offer any sample code on how to go about parsing a simple serial protocol? I've done some googling but i cant really find what i'm after.

The protocol only needs to be simple, something like Startbit, key pressed, end bit, maybe '@[keypressed]#'.

Thanks for your help!

A simple python capture program that stores anything that comes from Arduino in a file. I was sending ; separated values thats why the split has this param. Tested only a few times and it worked for me. Modify the baudrate to your need.

import sys, os, serial, datetime

def capture():

    print "Start capture"

    ser = serial.Serial(4, 115200, timeout=0)

    while (1):
        line = ser.readline()
        if (line != ""):
            #print line[:-1]         # strip \n
            fields = line[:-1].split('; ');
            ID = fields[0]
            # etc

            # write to file
            filename = str( + ".log"
            text_file = open(filename, "a")

""" -------------------------------------------
print "Start Application"


Thanks, I'll give that a go and see how i get on!

Did this work ok for you?