home *** CD-ROM | disk | FTP | other *** search
- /*** TERM_IO.C ***/
-
- #include "windows.h"
- #include <stdio.h>
- #include <string.h>
- #include "pcl4w.h"
- #include "term.h"
- #include "ascii.h"
- #include "sioerror.h"
- #include "term_io.h"
- #include "paint.h"
-
- #define CAPTURE 0
- #define DEBUG 0
-
- void Delay(int);
-
- #if CAPTURE
- #define CAPTURESIZE 4400
- WORD CaptureBuffer[CAPTURESIZE+1];
- WORD Tail=0;
- #endif
-
- #if DEBUG
- int DebugHandle;
- #endif
-
- static char Temp[256];
- static int GetCharWait = 0;
-
- void TermInit(void)
- {
- #if DEBUG
- DebugHandle = _lcreat("debug.dat",0);
- #endif
- }
-
- /*** write capture buffer to disk ***/
-
- void TermDone(void)
- {
- #if DEBUG
- _lclose(DebugHandle);
- #endif
- #if CAPTURE
- int Handle;
- char hi, lo;
- int i;
- char Temp[40];
- char *Ptr;
- Handle = _lcreat("capture.dat",0);
- _lwrite(Handle,"CAPTURE\n",9);
- sprintf(Temp,"Tail=%d\n",Tail);
- _lwrite(Handle,Temp,strlen(Temp));
- for(i=0;i<Tail;i++)
- {hi = (char) (CaptureBuffer[i]>>8);
- lo = (char) (CaptureBuffer[i]&0x00ff);
- if((hi>=' ')&&(hi<='~')) sprintf(Temp,"%4d: '%c' %c\n",i,hi,lo);
- else
- {sprintf(Temp,"%4d: %2xh %c\n",i,0x00ff&hi,lo);
- Ptr = &Temp[6];
- if(*Ptr==' ') *Ptr = '0';
- }
- _lwrite(Handle,Temp,strlen(Temp));
- }
- _lclose(Handle);
- #endif
- #if DEBUG
- _lclose(DebugHandle);
- #endif
- } /* end TermDone */
-
-
- /*** output character to serial port ***/
-
- int PutChar(int Port, char ch)
- {int rc;
- /* transmit character */
- #if CAPTURE
- /* capture transmit */
- CaptureBuffer[Tail] = (WORD)(ch<<8) | (WORD)('T');
- if(Tail<CAPTURESIZE) Tail++;
- #endif
- rc = SioPutc(Port,ch);
- if(rc<0)
- {/* error */
- SioError(rc,"SioPutc");
- SioDone(Port);
- }
- return(rc);
- } /* end PutChar */
-
- /*** set wait before re-try for timeout in GetChar() ***/
-
- void SetCharWait(int Wait)
- {/* set GetChar() wait */
- GetCharWait = Wait;
- } /* end SetCharWait */
-
- /*** read character from serial port ***/
-
- int GetChar(int Port)
- {int rc;
- int i;
- /* make at most 2 attempts */
- for(i=1;i<2;i++)
- {rc = SioGetc(Port);
- if(rc>=0) break;
- if(rc<-1)
- {/* error */
- SioError(rc,"SioGetc");
- SioDone(Port);
- }
- /* rc == -1 (timeout) */
- if(GetCharWait>0) Delay(GetCharWait);
- else break;
- }
- #if CAPTURE
- if(rc>-1)
- {/* capture receive (if not t/o) */
- CaptureBuffer[Tail] = (WORD)(rc<<8) | (WORD)('R');
- if(Tail<CAPTURESIZE) Tail++;
- }
- #endif
- return(rc);
- } /* end GetChar */
-
- /*** display the error text ***/
-
- void SayError(int Port, char *ptr)
- {char temp[81];
- sprintf(temp,"ERROR! COM%d : %s",1+Port,ptr);
- DisplayLine(temp);
- /* cancel remote */
- PutChar(Port,CAN);
- PutChar(Port,CAN);
- PutChar(Port,CAN);
- } /* end SayError */
-
- /*** display line on TERM client area ***/
-
- char CRLF[2] = {CR,LF};
-
- void DisplayLine(char *Ptr)
- {
- WriteTheString("<",1);
- WriteTheString(Ptr,strlen(Ptr));
- WriteTheString(">",1);
- WriteTheString(CRLF,2);
- #if DEBUG
- _lwrite(DebugHandle,Ptr,strlen(Ptr));
- _lwrite(DebugHandle,CRLF,2);
- #endif
- } /* end DisplayLine */
-
- void Delay(int Wait)
- {long TimeMark;
- TimeMark = GetTickCount() + (long)Wait;
- while(GetTickCount()<TimeMark);
- }