Windows 95 vs. Windows NT Serial Code

randy.price@delphiauto.com randy.price@delphiauto.com
Thu May 27 13:45:00 GMT 1999


Hello;

I have some serial I/O code that works fine on Windows 95.  It is uploading
a program to a HandHeld Products barcode scanner and unfortunately it will
not work on Windows NT.  I was successful in getting it to work with Beta
20.1 and Beta 19.  I am using the cygwin=tty and cygwin32=tty environment
variables.  The program sits in a loop at the beginning waiting for the
barcode Wand user to press the download key which sends an EX character to
the PC.  On Windows 95 it works everytime.  My sincere thanks to anyone who
can help me with this.  Here is the code;

/**************************************************************************
****/
/* Includes
                                                                   */
/**************************************************************************
****/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>

/**************************************************************************
****/
/* Preprocessor Definitions
                                                   */
/**************************************************************************
****/
#define TRUE 1
#define FALSE 0
#define TAB 9
#define SPACE 32
#define RETURN '\n'
#define SNUL '\0'
#define CR 0x0D
#define LF 0x0A
#define EX 0x03

/**************************************************************************
****/
/* Function Prototypes:
                                                       */
/**************************************************************************
****/
/* None at this time.*/

/**************************************************************************
****/
/* Main Program Begins:
                                                       */
/**************************************************************************
****/
int main(int argc, char *argv[])
{
 FILE *f1;                      /* File pointer for main
                      */
 char title[256];               /* Character array to hold title of file to
   */
 static char filename[256];     /* Character array for input file name
        */
 static char command[256];      /* Character array for Laser-Wand Command
     */
 int status,c,d,i,j,k;          /* c = sent char, d = received char
           */
 int fdSer;                     /* Serial port
                                */
 int fdCon;                     /* Console
                                    */
 struct termios t, term, torig; /* termios structures
                         */
 unsigned char buf[1];          /* Buffer for serial I/O
                      */


/**************************************************************************
****/
/*  If user supplies a file name on the command line then use it.
             */
/*  Else give them some help info and ask for a filename.
                     */
/**************************************************************************
****/
strcpy(filename,"NO FILE");     /* Initalize the input file name to "NO
FILE" */
if(argc > 1)
   strcpy(filename,argv[1]);    /* Test to see if the user entered a name
     */
                                /*  from the command line & try to use it
     */

printf("\nhhpload: Hand Held Products Program Loader Version 0.92\n");
printf("           Programmed on 10/28/98 By Randall E. Price \n\n");

if(strncmp(filename,"NO FILE",7) == 0)
{
 strcpy(filename,"/data/barcode/udl/pplwsw.pb1");

 /* Try to open the default file-else give up. */
 if((f1 = fopen(filename,"r")) == NULL)
 {
  printf("           Usage: hhpload2 <filename>\n\n");
  printf("           You did not enter the required filenames!\n\n");
  printf("           Enter a HHP program file to load:");
  scanf("%s", &filename);
 }
}

/* Try to open the file-else give up. */
if((f1 = fopen(filename,"r")) == NULL)
{
 printf("\n\n\t*** Error reading file \"%s\" ***\n\n",filename);
 exit(1);
}

/**************************************************************************
****/
/*  Print out a full set of instructions for loading the program into the
     */
/*  Hand Held Products Laser-Wand.
                                            */
/**************************************************************************
****/

printf("1. Remove the Laser-Wand from the cradle and remove the
battery.\n");
printf("2. Wait approximately 10 seconds and replace the battery.\n");
printf("3. Hold the \"MODE\" key in and pull the trigger.\n");
printf("4. A menu with four selections should appear;\n");
printf("      CLEAR       COOL\n");
printf("             COLD        COM_P\n");
printf("\n");
printf("5. Press F1 which is \"CLEAR\".  The Laser-Wand will clear memory
and\n");
printf("   go back to sleep.\n");

printf("6. Place the Laser-Wand back into the \"HOME BASE\".\n");
printf("7. Hold the \"MODE\" key in and pull the trigger.\n");
printf("8. A menu with four selections should appear;\n");
printf("      CLEAR       COOL\n");
printf("             COLD        COM_P\n");
printf("\n");
printf("9.  Press F4 which is \"COM_P\".\n");
printf("10. The program will begin loading and you will see each line
of.\n");
printf("    code as it loads into the Laser-Wand.\n");
printf("11. Wait for the Laser-Wand to go back to sleep.\n");
printf("12. Pull the trigger alone and the new program should load.\n");


/**************************************************************************
****/
/*  Initialize the serial port for character I/O.
                             */
/**************************************************************************
****/

/* Try /dev/rmb/serial9 for HP 9000/3XX and HP 9000/4XX */
   if ((fdSer = open("/dev/rmb/serial9", O_RDWR)) < 0)
   {
/* Try /dev/tty00 for HP 9000/7XX                       */
    if ((fdSer = open("/dev/tty00", O_RDWR)) < 0)
    {
/*  Try /dev/cua0 for Linux                             */
     if ((fdSer = open("/dev/tty00", O_RDWR)) < 0)
     {
/*   Try /dev/com1 for DOS/Windows                      */
      if ((fdSer = open("/dev/com1", O_RDWR)) < 0)
       {
       perror("Open of the serial port failed");
       return -1;
       }
      }
     }
    }

 tcgetattr(fdSer, &term);
 t = torig = term;        /* Save the original serial port settings in
torig */
 t.c_iflag = IGNBRK | INPCK;
 t.c_oflag = 0;
 t.c_cflag = CS8 | CREAD | CLOCAL | CSTOPB;
 t.c_lflag &= ~(ICANON);
 cfsetispeed(&t,B9600);
 cfsetospeed(&t,B9600);
 t.c_cc[VTIME] = 0;                       /* Wait as long as it takes
        */
 t.c_cc[VMIN] = 1;                        /* Wait for at least one
character */
 tcsetattr(fdSer,TCSANOW,&t);             /* Set the attributes
              */

/**************************************************************************
****/
/*  Wait for Laser-Wand to send <LASER-WAND>EX to begin loading.
              */
/**************************************************************************
****/
while(buf[0] != EX)
{
 read(fdSer,buf,sizeof(buf));
}

/**************************************************************************
****/
/*  Send the "LOADPROG 0 1" command to the Laser-Wand.
                        */
/**************************************************************************
****/
strcpy(command,"LOADPROG 0 1");           /* Initalize the Laser-Wand
Command */
i=0;
j = strlen(command);
while(i < j)
{
  buf[0] = command[i];
  write(fdSer,buf,sizeof(buf));          /* Write character to Laser-Wand
     */
  read(fdSer,buf,sizeof(buf));           /* Wait for echo from Wand to pace
   */
  i++;                                   /* Count sent characters
             */
}
buf[0] = CR;
write(fdSer,buf,sizeof(buf));            /* Send a CR to the Laser-Wand
       */

/**************************************************************************
****/
/*  Wait for the Laser-Wand to echo back "LOADPROG 0 1" with an appended
LF.  */
/**************************************************************************
****/
while(buf[0] != LF)
{
 c = read(fdSer,buf,sizeof(buf));
}
tcflush(fdSer,TCIOFLUSH);                /* Clear line before sending
program */

/**************************************************************************
****/
/*  Send the Laser-Wand program one character at a time.  Reading back the
    */
/*  echoed characters from the Laser-Wand.  If the echoed back character
does */
/*  not equal the sent character the process has failed and must be
restarted.*/
/**************************************************************************
****/
while((c = getc(f1)) != EOF)
{
 buf[0]= c;
 if (c != CR)
 {
  write(fdSer,buf,sizeof(buf));
  read(fdSer,buf,sizeof(buf));
  d = buf[0];
  putchar(d);

    if( d != c)
    {
      printf("\nFatal Tranmission Error --  Sent Character not echoed
back!");
      printf("\nVerify that Laser-Wand home base is powered-up, and that");

      printf("\nall cabling is correctly connected to the computer and");
      printf("\nthe homebase of the Laser-Wand.");
      printf("\n");
      printf("\nRestart loading process from step #1.");
      printf("\n");
      printf("\nSent Character  =  %d  Echoed Character = %d.",c,d);
      exit(1);
    }
  }
}

/**************************************************************************
****/
/*  Send the "EXIT" command to the Laser-Wand.
                                */
/**************************************************************************
****/
strcpy(command,"EXIT");                   /* Initalize the Laser-Wand
Command */
i=0;
j = strlen(command);
while(i < j)
{
  c = command[i];
 buf[0]= c;
 write(fdSer,buf,sizeof(buf));
  read(fdSer,buf,sizeof(buf));
  i++;
}
buf[0] = CR;
write(fdSer,buf,sizeof(buf));             /* Send a CR to the Laser-Wand
      */

/**************************************************************************
****/
/*  Restore the original termios values for the serial port.
                  */
/**************************************************************************
****/
 sleep(2);                       /* Let Laser-Wand finish before re-setting
   */
 t = torig;                      /* the serial port to original settings.
     */
 tcsetattr(fdSer, TCSANOW,&t);

/**************************************************************************
****/
/*  We are finised. Give the user some feedback and count down the seconds
    */
/*  until the Laser-Wand can be restarted with the new program.
               */
/**************************************************************************
****/
printf("\nWait for the Wand to go to sleep and then \n");
printf("squeeze the trigger to start the new program. \n");

return 0;
}




   Randall E. Price
   internet: randy.price@delphiauto.com



--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com



More information about the Cygwin mailing list