NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Using serial in python problems

June 14, 2011
by awkwardsaw
awkwardsaw's Avatar

I'm having trouble using serial for this project, this is my first "solo" project, so i'm pretty much doing this blindly :p

python:

import ConfigParser
import time

import serial

#screen size 320x480

s = serial.Serial("/dev/ttyUSB2", 115200)

def handleMouseMove():
    try:
        a = config.get('mouse', 'move')
        l = config.get('mouse', 'left')
        r = config.get('mouse', 'right')
        print 'Movement: ', a
        print 'Left: ', l
        print 'Right: ', r

        if a:               //mouse movement
            tmp_A = 't'
        else:
            tmp_A = 'f'

        if l:               //left click
            tmp_L = 't'
        else:
            tmp_L = 'f'

        if r:               //right click
            tmp_R = 't'
        else:
            tmp_R = 'f'

        s.write(tmp_A + "\n") //move
        s.write(tmp_L + "\n") //left
        s.write(tmp_R + "\n") //right

    except:
        print "Error occurred. Ignore and move on"

while(1):
    config = ConfigParser.RawConfigParser()
    config.read('pipe.ini') //pipe between Simba + Python
    handleMouseMove();
    time.sleep(1)

c:

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/uart.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:
//
//PC5 -- move
//PC4 -- left
//PC3 -- right

void set_move(char in) {
    if (in = 't') {
        PORTC |= (1<<PC5);
    } 
    if (in = 'f') {
        PORTC &= ~(1<<PC5);
    }
}

void set_left(char in) {
    if (in = 't') {
        PORTC |= (1<<PC4);
    }   
    if (in = 'f') {
        PORTC &= ~(1<<PC4);
    }
}

void set_right(char in) {
    if (in = 't') {
        PORTC |= (1<<PC3);
    } 
    if (in = 'f') {
        PORTC &= ~(1<<PC3);
    }
}

int main() {

  // start up the LCD
  lcd_init();

  lcd_home();

    DDRC |= (1<<PC5);
    DDRC |= (1<<PC4);
    DDRC |= (1<<PC3);

  // init serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  char m;
  char r;
  char l;
  char tmp_m;
  char tmp_l;
  char tmp_r;
  while(1) {
    // listen for bytes

    m = scanf_P(PSTR("%c"), &tmp_m);

    r = scanf_P(PSTR("%c"), &tmp_r);
    l = scanf_P(PSTR("%c"), &tmp_l);

   // write message to LCD

    lcd_write_string(PSTR("m: "));
    lcd_write_data(m);
    lcd_write_string(PSTR(" r: "));
    lcd_write_string(l);
    lcd_write_string(PSTR(" l: "));
    lcd_write_string(r);

    set_move(m);
    set_left(l);
    set_right(r);

  return 0;
  }
}

What it does is python gets info from a pipe, and sends either a 't' or 'f' for 3 different variables via serial to the MCU. Then the mcu turns on leds depending on which variable is 't' or 'f'(also debugs them to the lcd). The problem is that the mcu doesn't read the characters correctly, so it doesn't turn on the leds. Sorry that I don't know how to explain it better, hopefully the code makes more sence :)

June 14, 2011
by Noter
Noter's Avatar

I don't know python but maybe a \n is needed in the scanf_P like PSTR("%c\n") ?

Also, when you compare variables in c, two equal signs are used like this

if (in == 't') { ...
June 15, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi awkardsaw,

It looks like you are trying to do a whole lot there, and its hard to tell where the problem is. I suggest you simplify so you can solve the problems one at a time. Like Noter mentioned you you don't have the equality quite right, you want to use == to check equality. Another issue I see is that you are printing chars using lcd_write_data, which is a way to send commands to the LCD (like clear, and set position), not characters to write to the LCD. Use lcd_write_byte for that instead. lcd_write_string doesn't work either because it expects the string to be in program memory, which your char isn't.

Humberto

June 15, 2011
by awkwardsaw
awkwardsaw's Avatar

Thanks, that explains why there was weird charachters on the display. I'll play around with it when i get the chance

Post a Reply

Please log in to post a reply.

Did you know that our USB NerdKit works on Windows, Linux, and Mac OS X? Learn more...