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 » Communicating both ways?

June 13, 2012
by mcgroover
mcgroover's Avatar

Hi all,

I have built a program so that the microcontroller controls a python program and another where python controls the microcontroller. However, when I try both at the same time, my python program freezes! I have looked at all the tutorials but can't see one with bi-directional communication. Have I missed something?

I could probably some error handlers when it detects no signal (as it may just encounter an error when the microcontroller is reading and python is trying to read too), but just wanted to see if anyone had any experience in this.

June 14, 2012
by missle3944
missle3944's Avatar

Mcgroover,

Let's take a look at your python code.

-Dan

June 14, 2012
by mcgroover
mcgroover's Avatar

My python code is this:

import os, pygame, math, serial   #loads the pygame module
from pygame.locals import *
from pygame.compat import geterror
from time import clock, time
pygame.init()
w = 1100   #sets pygame screen width
h = 642   #sets pygame screen height
screen = pygame.display.set_mode((w, h),0,32)  #make and display screen

pygame.display.flip()   #Update screen
running = 1
font = pygame.font.Font(None, 36)
clock = pygame.time.Clock()
port = serial.Serial("COM2", 115200)

while running:          #Loop this
   for event in pygame.event.get():    #get user input
      if event.type == pygame.QUIT:    #if user clicks the close X
           running = 0                 #make running 0 to break out of loop

   temp = float(port.readline())
   clock.tick(100)
   speed = font.render(str(temp), 1, (100, 100, 100))
   screen.blit(speed, (280,165))
   pygame.display.flip()   #Update screen
   port.write('3')
June 14, 2012
by mcgroover
mcgroover's Avatar

I should actually post my microcontroller code as well. The python code reads in the value fine if I comment out the "output = uart_read();" but will hang if I leave it in the microcontroller code.

    #define F_CPU 14745600

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

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

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

    int main() {

      uart_init();
      FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
      stdin = stdout = &uart_stream;
      int32_t temp = 20;
      int32_t output = 0;

      while(1) {

      printf_P(PSTR("%d\r\n"), temp);   //send info to serial port
      output = uart_read();

    }
      return 0;
    }
June 15, 2012
by missle3944
missle3944's Avatar

Were you able to get it to work with out the pygame stuff? I noticed that pygame makes everything more bogged down in the code.

Dan

June 15, 2012
by mcgroover
mcgroover's Avatar

I haven't tried that yet, but I want to be able to create a graphical interface, so I am kind of tied to leaving it in pygame (I think).

June 26, 2012
by mcgroover
mcgroover's Avatar

I think I may know what I have been doing wrong. Both sets of code began with a readline before a writeline (and hence both waiting for something to read). I changed the python code to write then read and the program runs without freezing. :)

June 27, 2012
by missle3944
missle3944's Avatar

Mcgroover,

That sounds great! Sometimes I face that issue were I try to read something before writing. Sometimes in programming you have to think outside of the box. (at least for me anyways!)

-Dan

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...