⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comdemo.c

📁 C语言开发串行通讯程序库
💻 C
字号:
/* 
// COMDEMO.C
//
// Serial communications demo program for the Cport communications library.
// 
// Copyright (c) 1993 Bri Productions
//
*/

#include "cport.h"
#include "xmodem.h"
#include <conio.h>
#include <stdlib.h>      
#include <dos.h>
#include <bios.h>
#include <ctype.h>
#include <stdio.h>


/*
//-------------------------------------
//
// Microsoft portability
//
//-------------------------------------
*/
#if M_I86

#include <stdarg.h>
#include <time.h>
#include <graph.h>

#define	BLUE		 1
#define  LIGHTGRAY 7

#define 	bioskey(a)		 _bios_keybrd(a)
#define	gotoxy(x,y)		 _settextposition((short)(y), (short)(x))
#define	clrscr()			 _clearscreen(_GWINDOW)
#define	textattr(a)		 _settextcolor((short)((a)&0xf)); _setbkcolor((short)((a)>>4))
#define	window(a,b,c,d) _settextwindow((short)(b),(short)(a),(short)(d),(short)(c))
#define  cputs(s)			 _outtext(s)

int  wherex (void);
int  wherey (void);
void delay  (clock_t milliseconds);
void clreol (void);

#endif

/* 
//-------------------------------------
//
// Queue sizes and thresholds
//
//-------------------------------------
*/
#define  TXQ      4096
#define  RCVQ     4096
#define  THRESH   (RCVQ * 3 / 4)


/*
//-------------------------------------
// 
// numbers of parameters
//
//-------------------------------------
*/
#define  NUM_COM     4             
#define  NUM_BAUD    9
#define  NUM_MODE    2
#define  NUM_HND     4


/*
//-------------------------------------
// 
// scan codes
//
//-------------------------------------
*/
#define  A     0x1E00
#define  B     0x3000
#define  C     0x2E00
#define  D     0x2000
#define  E     0x1200
#define  F     0x2100
#define  G     0x2200
#define  H     0x2300
#define  I     0x1700
#define  M     0x3200
#define  N     0x3100
#define  O     0x1800
#define  P     0x1900
#define  R     0x1300
#define  S     0x1F00
#define  W     0x1100
#define  X     0x2D00
#define  PGDN  0x5100
#define  PGUP  0x4900


#define COM3A  PORT2|IRQ5


/*
//-------------------------------------
//
// text attributes
//
//-------------------------------------
*/
#define  NORM  0x07
#define  BOLD  0x08
#define  FAINT 0xF7
#ifndef  BLINK
#define  BLINK 0x80
#endif
#define  REVRS 0x77
#define  ESC   0x1b


/*
//-------------------------------------
//
// parameter constants
//
//-------------------------------------
*/
const unsigned id[]    = { COM1, COM2, COM3A, COM4 };
const int      baud[]   = { B115200, B57600, B38400, B19200, 
                            B9600, B4800, B2400, B1200, B300 };
const byte     mode[]   = { W8|S1|NONE, W7|S1|EVEN };
const byte     hndshk[] = { OFF ,SOFT, HARD, HARD|SOFT };

/*
//-------------------------------------
//
// parameter indexes
//
//-------------------------------------
*/
struct indx{
   byte id;
   byte baud;
   byte mode;
   byte ansi;
   byte hndshk;
   byte lf;
 byte echo;
   }indx = { 0, 3, 0, 1, 0, 0, 0 };


/*
//-------------------------------------
//
// parameter messages
//
//-------------------------------------
*/
struct{
   char *id    [NUM_COM ];    
   char *baud  [NUM_BAUD];   
   char *mode  [NUM_MODE];     
   char *ansi  [2];
   char *hndshk[NUM_HND];
   char *lf    [2];
   }msg={   
            { "COM1", "COM2", "COM3", "COM4" },
            { "115k", "57600", "38400", "19200", "9600", 
              "4800", "2400", "1200", "300" },
            { "8-1-N", "7-1-E" }, 
            { "TTY", "ANSI" },
            { "NONE", "SOFT", "HARD", "BOTH" },
            { "  ", "LF" }
        };


/*
//-------------------------------------
//
// screen coordinates
//
//-------------------------------------
*/
static int x = 1;                      /* cursor location               */
static int y = 1;
static byte attrib = NORM;             /* present text attribute        */
#define  ERR_X    41                   /* error message x coordinate    */
#define  STAT_X   60                   /* status message x coordinate   */


/*
//-------------------------------------
//
// function prototypes
//
//-------------------------------------
*/
static void     Init        (void);
static void     Uninit      (void);
static void     NewParam    (void);
static void     Ansi        (void);
static void     CheckError  (void);
static void     CheckStatus (void);
static void     Upload      (void);
static void     Download    (void);
static int      callback   (int msg, unsigned param);
static void     put_ch  (char c);



char  *Xmsg[] = { "\r\ntransfer successful",
                  "\r\nfile error",
                  "\r\ntransfer canceled",
                  "\r\nmemory error"
               };

struct C_param param;
COM com;

/*
//-------------------------------------
//
// main()
//
//-------------------------------------
*/
void main(void)  
{     
char c;
unsigned key;
byte dtr = ON;


      /* initialize  */

   Init();   

   while(1)
   {
      
         /* Poll the keyboard buffer for available keystrokes.       */
         /* Meanwhile, the receive queue is checked for available    */
         /* characters, check for errors and check the modem status  */

      while(!bioskey(1))         
      {

            /* If a character(s) is available in the receive queue,  */
            /* fetch it. If it is and escape character, it must be   */
            /* check to see if it is the start of an ansi sequence.  */
            /* Otherwise the cursor position is updated, and the     */
            /* character is printed.                                 */
         
         if(ComLenRx(com))        
         {
            c = ComGetc(com);      

            if(c == ESC && indx.ansi)        
            {
               Ansi();  
               gotoxy(x, y);
               continue;
            }

          put_ch(c);            

         }


            /* Check for errors and changes in the modem status   */

         CheckError();       
         CheckStatus();      
      }
   

         /* When a key is pressed, the key is fetched, and the    */
         /* keyboard flags are checked to see if the alt key was  */
         /* also pressed. If the alt key was pressed, we must     */
         /* check if the key is a valid command, and if so        */
         /* process it accordingly                                */

      key = bioskey(0);                   
      if(!(key & 0x00ff))  
      {
         switch(key)                      
         {

               /* exit  */

            case X:                  
               Uninit();


               /* Next com port  */

            case C:                  
               ComParam(com, &param);
               ComClose(com);
               do
               {
                  indx.id++;             
                  indx.id %= NUM_COM;    
                  param.id = id[indx.id];
               }
               while((com = ComOpenS(&param)) == NULL);
               NewParam(); 
               break;


               /* Next baud rate */

            case B:                  
               indx.baud++;            
               indx.baud %= NUM_BAUD;  
               ComBaud(com, baud[indx.baud]);
               NewParam(); 
               break;


               /* Next word length  */

            case M:                    
               indx.mode++;            
               indx.mode %= NUM_MODE;  
               ComMode(com, mode[indx.mode]);
               NewParam(); 
               break;


               /* Toggle ansi terminal */

            case A:
               indx.ansi ^= 1;
               NewParam();
               break;


               /* Next handshake scheme   */

            case H:
               indx.hndshk++;
               indx.hndshk %= NUM_HND;
               ComHandshake(com, hndshk[indx.hndshk], THRESH);
               NewParam();
               break;


              /* Echo */

            case E:
               indx.echo ^= 1;
               break;


               /* Toggle LF append  */

            case N:
               indx.lf ^= 1;
               NewParam();
               break;

               /* Initialize hayes modem  */

            case I:                    
               ComPuts(com, "ATZ\r\n");         
               break;


               /* Hayes modem dial command   */

            case D:                    
               ComPuts(com, "ATDT");           
               break;


               /* Hayes modem hang up command   */

            case G:                    
               ComPuts(com, "+++");
               delay(3000);
               ComPuts(com, "ATH0\r\n");
               break;

            case PGUP:
               Upload();
               break;

            case PGDN:
               Download();
               break;

            case R:
               dtr ^= 1;
               ComDtr(com, dtr);
               break;

            default:                   
               continue;
         }
      }


         /* If the key was not a command, put the character in the   */
         /* transmit queue. If the character is a carriage return,   */
         /* append a line feed to it just in case.                   */

      else
      {
         ComPutc(com, (char)key);       
   if(indx.echo)
    put_ch((char)key);
         CheckStatus();      
         CheckError();       
      }
   }
}


/*
//-------------------------------------
//
// Modified putch()
//
//-------------------------------------
*/
void put_ch(char c)
{

 	gotoxy(x, y);
	putch(c);

   if(c == '\r' && indx.lf)
         putch('\n');

   if(c == '\b')
      printf(" \b");

   x = wherex();        
   y = wherey();

#ifdef M_I86
	if(x == 1 && y == 25)
		_scrolltextwindow(1);
#endif
}


/*
//-------------------------------------
//
// Initialize
//
//-------------------------------------
*/
void Init(void)
{
FILE *fp;



      /* If an initialization file exists, load it.   */

   fp = fopen("comdemo.ini", "rb");
   if(fp)
      fread(&indx, sizeof(struct indx), 1, fp);



      /* Set up the screen */

   clrscr();                        
   textattr(BLUE|(LIGHTGRAY<<4));   
   gotoxy(1,25);                    
   cprintf(" %4s 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -