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.

Microcontroller Programming » need help with my code

July 12, 2011
by bhovik
bhovik's Avatar

Here is my source code

#define F_CPU 14745600

#include <stdio.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"

// PIN DEFINITIONS:
//
int main() {
  // start up the LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

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

  // initialize variables
  int16_t y = 0;
  double z = 0.0;
  int r1,r2;

  // loop doing this forever!
  while(1) {
    // ask some questions and collect responses
    printf_P(PSTR("\n\rHow old are you?"));
    r1=scanf_P(PSTR("%d"), &y);

    printf_P(PSTR("\n\rWhat's your favorite number? "));
    r2=scanf_P(PSTR("%lf"), &z);

    // send results back over the serial port
    printf_P(PSTR("\n\rCool! You are %d years old and like the number %f."), y, z);

    // send results to the LCD as well
    lcd_clear_and_home();
    lcd_line_one();
    fprintf_P(&lcd_stream, PSTR("Age: %d %d %d"), y,r1,r2);
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("Fav. Number: %7f"), z);
    lcd_line_four();
    fprintf_P(&lcd_stream, PSTR("  www.NerdKits.com  "));
    if(r1>0&&r2>0){
      r1=0;
      r2=0;
    }
  }

  return 0;
}

And now Host code on PC

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <termios.h>
#include <sysexits.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <AvailabilityMacros.h>

static struct termios gOriginalTTYAttrs;
    char        buffer[2048];   // Input buffer
    char        *bufPtr;        // Current char in buffer
    ssize_t     numBytes;       // Number of bytes read or written

int main(int argc, char **argv) {
  char *x;
  int y = 0;
  double z = 0.0;
    int             handshake;
    struct termios  options;

  // note the use of fgets instead of scanf("%s",...)
  // to prevent buffer overflows by specifying a maximum length!
    // Open the serial port
 system("stty -f /dev/cu.PL2303-00001004 115200");
 int port = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK);
     if (port == -1)
    {
        printf("Error opening serial port %s - %s(%d).\n",
               argv[1], strerror(errno), errno);
        goto error;
    }
    // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
    // See fcntl(2) ("man 2 fcntl") for details.

    if (fcntl(port, F_SETFL, 0) == -1)
    {
        printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
            argv[1], strerror(errno), errno);
        goto error;
    }
   // Get the current options and save them so we can restore the default settings later.
    if (tcgetattr(port, &gOriginalTTYAttrs) == -1)
    {
        printf("Error getting tty attributes %s - %s(%d).\n",
            argv[1], strerror(errno), errno);
        goto error;
    }

    // The serial port attributes such as timeouts and baud rate are set by modifying the termios
    // structure and then calling tcsetattr() to cause the changes to take effect. Note that the
    // changes will not become effective without the tcsetattr() call.
    // See tcsetattr(4) ("man 4 tcsetattr") for details.

    options = gOriginalTTYAttrs;

    // Print the current input and output baud rates.
    // See tcsetattr(4) ("man 4 tcsetattr") for details.
    cfsetspeed(&options, B115200);
    tcsetattr(port, TCSANOW, &options);

    printf("Current input baud rate is %d\n", (int) cfgetispeed(&options));
    printf("Current output baud rate is %d\n", (int) cfgetospeed(&options));

    //  int port;
    //port = open(argv[1], O_RDWR);
  //    FILE * port = fopen(argv[1], "w+");
 //  if (port < 0) {
 //  fprintf(stderr, "Unable to open %s\n", argv[1]);
 //   return 1;
 // }

    char ans[20];
      while(1){
    fcntl(port, F_SETFL, 0);

    bufPtr = buffer;
    numBytes = read(port, buffer, sizeof(buffer));
    printf("%d %s\n",int(numBytes),buffer);
    scanf("%s",ans);
    sprintf(ans,"%s\n",ans);
    numBytes = write(port, ans, sizeof(ans));

    numBytes = read(port, buffer, sizeof(buffer));
    printf("%d %s\n",int(numBytes),buffer);
    scanf("%s",ans);
    sprintf(ans,"%s\n",ans);
    numBytes = write(port, ans, sizeof(ans));

    numBytes = read(port, buffer, sizeof(buffer));
    printf("%d %s\n",int(numBytes),buffer);

    usleep(4000);

    if (numBytes == -1)
      {
    printf("Error reading from modem - %s(%d).\n", strerror(errno), errno);
      }
    usleep(2000);

        //    read(port,buffer,sizeof(x));

    /*printf("%s\n", x);
    fprintf(port,"10\n");
    fscanf(port,"%s",x);
    fprintf(port,"1\n");*/

    }
 error:
  close(port);

  return 0;
}

When I use the "screen /dev/cu 115200" the source code changer r1 and r2 values to 1 and all reading of the value are correct.

On the other hand when I use PC core r1 and r2 are always negative even though first scanf_p is reading correct value but the second one does not read anything. And my buffer readings are wrong. Could you help me with that??

July 13, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi bhovik,

I'm not sure what the issue you are having here is. Under what conditions is the code doing what you expect? Are you trying this on both a Mac and PC and one of them is not working?

Humberto

July 13, 2011
by bhovik
bhovik's Avatar

Hi Humberto I would expect the PC code to behave as screen command. in addition to that I would expect r1 and r2 to change to positive whenever I enter the number. But r1 and r2 are always -1 even though the y value(only) is changing z is not changing.

Post a Reply

Please log in to post a reply.

Did you know that the microcontroller's crystal oscillator can be used to keep accurate time? Learn more...