CSC231 Modified Arduino-serial.c

From dftwiki3
Revision as of 10:11, 23 November 2008 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut 15:07, 23 November 2008 (UTC)


THIS PROGRAM SHOULD BE USED FOR DEBUGGING ONLY. IT HAS BEEN MODIFIED SO THAT IT DOES NOT TALK TO AN ARDUINO, BUT SIMPLY DISPLAYS INFORMATION THAT WOULD NORMALLY BE SENT TO THE ARDUINO, AND IT CREATES FAKE RESPONSE STRINGS AS IF THEY WERE GENERATED BY A CONNECTED ARDUINO.
If you are looking for the working version of this program, check here.

/*
 * Arduino-serial
 * --------------
 * 
 * A simple command-line example program showing how a computer can
 * communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC) 
 * 
 *
 * Compile with something like:
 * gcc -o arduino-serial arduino-serial.c
 *
 * Created 5 December 2006
 * Copyleft (c) 2006, Tod E. Kurt, tod@todbot.com
 * http://todbot.com/blog/
 *
 * 
 * Updated 8 December 2006: 
 *  Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
 *  I've included his patch, but commented out for now.  One really needs a
 *  real make system when doing cross-platform C and I wanted to avoid that
 *  for this little program. Those baudrates aren't used much anyway. :)
 *
 * Updated 26 December 2007:
 *  Added ability to specify a delay (so you can wait for Arduino Diecimila)
 *  Added ability to send a binary byte number
 *
 * Update 31 August 2008:
 *  Added patch to clean up odd baudrates from Andy at hexapodia.org
 *
 * Modified 11/06/08 by D. Thiebaut
 *  Force program to wait until a \n is received from Arduino
 *  Added linking with assembly program
 *
 * Modified 11/19/08 by D. Thiebaut
 *  This modification is only to allow the grading of the homework assignment:
 *  The functions that initialize the USB Port and send messages to the Arduino
 *  have been modified so that the program can work WITHOUT an Arduino stamp.
 *
 */

#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h> 
#include <stdint.h>   /* Standard types */
#include <string.h>   /* String function definitions */
#include <unistd.h>   /* UNIX standard function definitions */
#include <fcntl.h>    /* File control definitions */
#include <errno.h>    /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>

extern int asm_main( void );  // link to assembly program

//-------------------------- prototypes ---------------------------
void usage(void);
int serialport_init(const char* serialport, int baud);
int serialport_writebyte( );
int serialport_write( );
int serialport_read_until( );
int displayBuffer( );

//---------------------------- globals ----------------------------
int  fd = 0;             // file descriptor for USB device
char serialport[256];    // name of the serial port (/dev/ttyUSB0)
int  baudrate = B9600;   // default baud rate
char buf[1024];          // buffer for sent/receive strings
uint8_t byte;
int  pin;

//-----------------------------------------------------------------
//-----------------------------------------------------------------
void usage(void) {
    printf("Usage: arduino-serial -p <serialport> [OPTIONS]\n"
    "\n"
    "Options:\n"
    "  -h, --help                   Print this help message\n"
    "  -p, --port=serialport        Serial port Arduino is on\n"
    "  -b, --baud=baudrate          Baudrate (bps) of Arduino\n"
    "  -s, --send=data              Send data to Arduino\n"
    "  -r, --receive                Receive data from Arduino & print it out\n"
    "  -n  --num=num                Send a number as a single byte\n"
    "  -d  --delay=millis           Delay for specified milliseconds\n"
    "\n"
    "Note: Order is important. Set '-b' before doing '-p'. \n"
    "      Used to make series of actions:  '-d 2000 -s hello -d 100 -r' \n"
    "      means 'wait 2secs, send 'hello', wait 100msec, get reply'\n"
    "\n");
}

//-----------------------------------------------------------------
int main(int argc, char *argv[]) {
    int rc, n;

    if (argc==1) {
        usage();
        exit(EXIT_SUCCESS);
    }

    /*--- parse options ---*/
    int option_index = 0, opt;
    static struct option loptions[] = {
        {"help",       no_argument,       0, 'h'},
        {"port",       required_argument, 0, 'p'},
        {"baud",       required_argument, 0, 'b'},
        {"send",       required_argument, 0, 's'},
        {"receive",    no_argument,       0, 'r'},
        {"num",        required_argument, 0, 'n'},
        {"delay",      required_argument, 0, 'd'}
    };
    
    /*--- process the command line arguments ---*/
    while(1) {
        opt = getopt_long ( argc, argv, "hp:b:s:rn:d:",
                           loptions, &option_index );
        if (opt==-1) break;
        switch (opt) {
        case '0': break;
        case 'd':
            n = strtol(optarg,NULL,10);
            usleep(n * 1000 ); // sleep milliseconds
            break;
        case 'h':
            usage();
            break;
        case 'b':
            baudrate = strtol(optarg,NULL,10);
            break;
        case 'p':
            strcpy(serialport,optarg);
            fd = serialport_init( optarg, baudrate );
            if(fd==-1){
	      printf( "Could not initialize USB port\n\n" );
	      return -1;
	    }
            break;
        case 'n':
            n = strtol(optarg, NULL, 10 ); // convert string to number
	    byte = (uint8_t) n;
            rc = serialport_writebyte( );
            if(rc==-1) return -1;
            break;
        case 's':
            strcpy( buf, optarg );
            rc = serialport_write( );
            if (rc==-1) return -1;
            break;
        case 'r':
	    serialport_read_until( );
            printf("%s\n",buf);
            break;
        }
    }

    //--- call assembly language program ---
    asm_main();

    //--- exit ---
    exit(EXIT_SUCCESS);    

} // end main
    
//-----------------------------------------------------------------
// displayBuffer: debugging function to see what is in the buffer
//-----------------------------------------------------------------
int displayBuffer( ) {
  printf( "buffer = [%s]\n", buf );
  return 0;
}

//-----------------------------------------------------------------
//-----------------------------------------------------------------
int serialport_writebyte( ) {
    int n = write( fd, &byte, 1);
    if( n!=1)
        return -1;
    return 0;
}

//-----------------------------------------------------------------
// serialport_write: MODIFIED version of original function.
//  Does NOT send info to arduino, but instead prints it on 
//  the screen.
//-----------------------------------------------------------------
int serialport_write() {
  int len;
  strcat( buf, "\n" );
  strcat( buf, "\0" );
  printf( "serialport_write(%s)\n", buf );
  return 0;
}

//-----------------------------------------------------------------
// seriport_read_until: MODIFIED version of original routine.
//    instead of reading something from the Arduino, puts a
//    predetermined string in buffer as if it were coming from
//    the arduino.
//-----------------------------------------------------------------
int serialport_read_until( ) {
  strcpy( buf, "D 0 1 0 1 0 1 0 1 0 1 0 1" );
  return 0;
}

//-----------------------------------------------------------------
// MODIFIED version of original routine.  Does nothing except
// returning 1 as if everything went ok.
//-----------------------------------------------------------------
int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;
 
    return 1;
}