Fadshop.net
A website for better software and better life in Internet!
   MY WEB

   Chinese URLs

   Greeting Creator

   Input Tips Everyday

   Source Code
Java Applet Games
Visit webpage in VC
M3U Music List Editor
Input Tips everyday
Combox in HTML
Self-made Screen Saver
Model of Neural Network
Display Chinese Characters
Mouse in Maze, a classic game in math
Serial Port Communication

Source Code -> Serial Port Communication in DOS
 

Serial Port Communication



Serial Port Communication is an important part of computer service, because computer must communicate with others, by Network Adapter, or Parallel Port, or by Serial Port.
Do you want to control Serial Port in Dos? in WIN95/98?


You can use BASIC (BASICA), C (TC,BC) if you want to run in DOS.
100 REM SERIAL CONTROL. WRITTEN BY TBLIN@163.NET
150 TXD$="ABCDEFG"
200 D$="":'DATA RECEIVED.
210 OPEN "COM2:9600,E,7,1,CD,RS,CS,DS" AS #1
220 GOSUB 630
250 CLOSE #1:END
630 PRINT "SENDING DATA=";TXD$
640 PRINT #1,TXD$:'SENDING
650 T3 = VAL(MID$(TIME$,7,2)):'WAITING 2 SECONDS
660 IF EOF(1)=0 THEN GOSUB 710
670 T4 = VAL(MID$(TIME$,7,2)) 
680 IFABS(T4-T2)<2 THEN 660 
690 PRINT:PRINT "OVER 2S!" 
700 RETURN 
710 A$="INPUT$(1,#1)" 
720 D$="D$+A$" 
730 PRINT A$; 
760 RETURN 

^Top

You can use Borland C or Turbo C to compile this source code. here is head file:
/*TLINK.H FOR TLINK.C tblin@163.net 97/04/29 */
#define SER_RBF		0 /*the read buffer*/
#define SER_THR		0 /*the write buffer*/
#define SER_IER		1 /*the int. enable register*/
#define SER_IIR		2 /*the int. identiffication register*/
#define SER_LCR		3 /*control data config. and divisor latch*/
#define SER_MCR		4 /*modem control register*/
#define SER_LSR		5 /*line status register*/
#define SER_MSR		6 /*modem status of cts,ring etc.*/
#define SER_DLL		0 /*the low byte of baud rate divisor*/
#define SER_DLH		1 /*the hi byte of divisor latch*/
#define SER_GP02	8 /*enable interrupt*/
#define COM_1		0x3F8
#define COM_2		0x2F8
#define COM485		0x1E8
#define SER_STOP_1	0 /*1stop bit per character*/
#define SER_STOP_2	4
#define SER_BITS_5	0 /*send 5 bit characters*/
#define SER_BITS_6	1 /*send 6 bit characters*/
#define SER_BITS_7	2 /*send 7 bit characters*/
#define SER_BITS_8	3 /*send 8 bit characters*/
#define SER_PARITY_NONE	0 /*no parity*/
#define SER_PARITY_ODD  8
#define SER_PARITY_EVEN	24
#define SER_DIV_LATCH_ON 128 /*used to turn reg 0,1 into divisor latch*/
#define PIC_IMR		0x21 /*pic's interrupt mask reg.*/
#define PIC_ICR		0x20 /*pic/s interrupt control reg.*/
#define INT_SER_PORT_0	0x0C /*port 0 interrupt com 1&3*/
#define INT_SER_PORT_1	0x0B
#define Int_Mode 	0
#define Ask_Mode	1
#define SERIAL_BUFF_SIZE 4096 /*current size of circulating receive buffer*/
#define SER_BAUD_1200	96
#define SER_BAUD_2400	48
#define SER_BAUD_9600	12

Here is source code
/*Serial Port control. tblin@163.net*/
#include "d:\ltb\tlink.h"
#include 
#include 
#include 
#ifdef __cplusplus
    #define __CPPARGS ...
#else
    #define __CPPARGS
#endif

void interrupt  (*Old_Isr)(__CPPARGS); /*holds old com port interrupt handler*/

char ser_buffer[SERIAL_BUFF_SIZE]; /*the receive buffer*/
int ser_end=-1,ser_start=-1;
int ser_ch,char_ready=0;
int old_int_mask;
unsigned open_port;
int serial_lock=0;
int mode=0;
void interrupt far Read_Ser(void) /*ISR program.*/
{
 char ch;
 serial_lock=1;
 ser_ch=inp(open_port+SER_RBF);
 if(++ser_end>SERIAL_BUFF_SIZE-1)
	ser_end=0;
 ser_buffer[ser_end]=ser_ch;
 ++char_ready;
 outp(PIC_ICR,0x20);
 serial_lock=0;
 }/* End Read_Ser*/

int Read_Queue()
{
 int ch;
 while(serial_lock){}
 if(ser_end!=ser_start)
	{if(++ser_start>SERIAL_BUFF_SIZE-1)
		ser_start=0;
	 ch=ser_buffer[ser_start];
	 if(char_ready>0)	--char_ready;
	 return(ch);
	 }
	else return(0);		/*return NULL*/
 }/* End Read_Queue*/

Write_Ser(char ch)
{
 while(!(inp(open_port+SER_LSR)&0x20)){} /*Waiting for Sent Buffer empty.*/
 /* asm cli*/                   /*These two lines must be compiled by TCC.EXE*/
 disable();			/*Instead the latest line.*/
 outp(open_port+SER_THR,ch);
 enable();			/*Instead the next line.*/
/* asm sti			WARN!!!*/
 }

Open_Serial(int port_base,int baud,int configuration)
{
 int value;
 open_port=port_base;
 outp(open_port+SER_LCR,SER_DIV_LATCH_ON);	/*Initating*/
 outp(open_port+SER_DLL,baud);
 outp(open_port+SER_DLH,0);
 outp(open_port+SER_LCR,configuration);
 outp(open_port+SER_MCR,SER_GP02);
 outp(open_port+SER_IER,1);
 old_int_mask=inp(PIC_IMR);
 if(open_port==COM_1)
	{Old_Isr=getvect(INT_SER_PORT_0);
	 setvect(INT_SER_PORT_0,Read_Ser);
	 printf("\nOpening Communications Channel Com Port #1...\n");
	 outp(PIC_IMR,old_int_mask&0xEF);
	 }
	else
	{Old_Isr=getvect(INT_SER_PORT_1);
	 setvect(INT_SER_PORT_1,Read_Ser);
	 printf("\nOpening Communications Channel Com Port #2...\n");
	 outp(PIC_IMR,old_int_mask&0xF7);
	 }
 }/*End Open_Serial*/

Close_Serial(void)
{
 outp(open_port+SER_MCR,0);
 outp(open_port+SER_IER,0);
 outp(PIC_IMR,old_int_mask);
 if(open_port==COM_1)
	{setvect(INT_SER_PORT_0,Old_Isr);
	 printf("\nClosing Communications Channel Com Port #1.\n");
	 }
	else
	{setvect(INT_SER_PORT_1,Old_Isr);
	 printf("\nClosing Communications Channel Com Port #2.\n");
	 }
 }/*End Close_Serial*/

int menu()
{int baud;char ch;
 printf("\nCom1 or Com2(1/2)?");
 ch=getche();
 if(ch=='1') open_port=COM_1;
	else
		if(ch=='2')	open_port=COM_2;
			else	exit(1);
 printf("\nChoose the BaudRate:\n\n");
 printf("\t 2400----0\t 4800----1\t 9600----2\n");
 printf("\t14400----3\t19200----4\t28800----5\n");
 printf("\t38400----6\t57600----7\t115200---8\n");
/* scanf("%d",&baud);printf("\n");*/
 ch=getche();
 switch(ch)
	{
	case	'0':	return(SER_BAUD_2400);
	case	'1':	return(SER_BAUD_4800);
	case	'2':	return(SER_BAUD_9600);
	case	'3':	return(SER_BAUD_14400);
	case	'4':	return(SER_BAUD_19200);
	case	'5':	return(SER_BAUD_28800);
	case	'6':	return(SER_BAUD_38400);
	case	'7':	return(SER_BAUD_57600);
	case	'8':	return(SER_BAUD_115200);
	default:	printf("Wrong!");
			exit(1);
	 }
 }

main()
{char get_ch,send_ch;int baud,done=1;
 printf("\nNull Modem Terminal Communications Program.\n\n");
 baud=menu();
 Open_Serial(open_port,baud,SER_PARITY_NONE|SER_BITS_8|SER_STOP_1);
 while(done)
	{if(kbhit())
		{scanf("%d",&send_ch);
		 printf("Writing...%c\t%d\n",send_ch,send_ch);
		 Write_Ser(send_ch);
		 if(send_ch==27) done=0;
		 if(send_ch==13)
			{printf("\n");
			 Write_Ser(10);
			 }
		 } /*end of kbhit*/
	 /*try and get a character from remote*/
	 if (char_ready>0)
			{get_ch=Read_Queue();
			 printf("Read....%c\t%d\n",get_ch,get_ch);
			 }
	 if(get_ch==27)
		{done=0;
		 printf("\nThe remote machine closing connection.");
		 }
	 }/*end while*/
 Close_Serial();
 }/*End main*/

Control Serial Port in DOS Control Serial Port in Windows98/NT

   Contact Info
  User Support Sales Question Webmaster

Copyright 1998-2002 Fadshop.net, Inc. All rights reserved.