www.pudn.com > wsc4c30.zip > DEVICE.C
/* ** DEVICE.C ** ** This WIN32 console mode program sends a command string ** to a serial device and gets a response. ** */ #include#include #include #include "wsc.h" #define RESPONSE_WAIT 500 #define QUOTE 0x22 static char Temp[512]; static int Port; /* trap WSC error codes */ int ErrorCheck(int Code) {if(Code<0) {printf("ERROR %d ",Code); SioDone(Port); exit(1); } return Code; } /* display error & exit */ void SayError(char *Msg) {printf("ERROR: %s: %d\n", Msg, GetLastError() ); exit(1); } /*** main ***/ void main(int argc, char *argv[]) {char c; int i; int Baud; int CmdLen; int Code; LPSTR CmdPtr; long CharWait; /* process args */ if(argc!=5) {printf("Usage: device \n"); printf(" Eg: device 2 19200 AT 100\n"); printf(" Eg: device 1 9600 %cThis is a test%c 100\n",QUOTE,QUOTE); return; } Port = atoi(argv[1]) - 1; Baud = atoi(argv[2]); CmdPtr = (LPSTR)argv[3]; CharWait = (long) atoi(argv[4]); /* reset (initialize) the port */ ErrorCheck( SioReset(Port,1024,1024) ); ErrorCheck( SioBaud(Port,Baud) ); ErrorCheck( SioDTR(Port,'S') ); ErrorCheck( SioRTS(Port,'S') ); ErrorCheck( SioParms(Port,WSC_NoParity,WSC_OneStopBit,WSC_WordLength8) ); printf(" Port : COM%1d\n", 1+Port); printf(" Baud : %u\n", Baud); printf(" DTR : set\n"); printf(" RTS : set\n"); #if 1 ErrorCheck( SioFlow(Port, 'H') ); printf(" Flow : Hardware\n"); #endif printf(" Parity : None\n"); printf(" Command : %s\n", CmdPtr); printf("CharWait : %ld\n",CharWait); printf("StopBits : 1\n"); printf("DataBits : 8\n"); if(SioDSR(Port)) printf(" DSR : set\n"); else printf(" DSR : clear\n"); if(SioCTS(Port)) printf(" CTS : set\n"); else printf(" CTS : clear\n"); CmdLen = strlen(CmdPtr); printf("Sending %c%s%c with %d mSec delay between characters\n", QUOTE,CmdPtr,QUOTE,CharWait); // send one character at a time for(i=0;i