Nowadays, there are many kinds of popular programming software. They are easy to program and maintain, but they are helpless when dealing with hardware directly and compiling system software. So C language comes in handy. As a transitional language between assembly language and high-level language, C language has both the efficiency of assembly language and the convenience of high-level language.
In communication, in order to ensure safe and reliable operation, the standard serial port must have many handshake signals and status information. This is because the CPU speeds of the computers in communication are different (which will cause "wrong frames") and the transmitter sends data faster than the receiver receives it (which will cause "overshoot"). To solve this problem, we use a simple handshake signal, that is, the transmitter only sends half a byte (lower 4 bits)
of data each time, and the other half a byte (higher 4 bits) is used to transmit information. We can encode the information bits (higher 4 bits) as follows:
0H: Sending new half-byte data
1H: Resending the last transmitted error data
2H: End of file name
3H: End of file
In this way, every time the transmitter sends a byte, it waits for the receiver to send a return signal, which is the byte sent by the transmitter. After receiving the return signal, the transmitter compares it with the byte just sent. If they are the same, it sends a new half byte, otherwise it resends. The new data is distinguished from the old data by the information bit. The following is a program written in C language to control the serial port.
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char *fname); /* Send file*/
void Send(int s); /* Send a byte*/
void SendFileName(char *fname); /* Send file name*/
void ReceiveFile(); /* Receive file*/
void GetFileName(char *f); /* Receive file name*/
void InitPort(int port,unsigned char para); /* Initialize port*/
void SendPort(int port,char c); /* Port send*/
int ReadPort(int port); /* Read port byte*/
int CheckState(int port); /* Check port status*/
int Receive(int port,int *G); /* Receive a byte*/
main(int argc,char *argv[])
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:"); ReceiveFile();
else { printf("Error parament. Please input again."); exit (1);
} } void SendFile(char *fname) { FILE * fp ; int ch,s; if((fp=fopen(fname , " rb"))==NULL) { printf("Can' '''t open the file .\n"); exit(1); } SendFileName ( fname); do{ ch=(int)getc(fp); if(ferror(fp)){ printf ("Error reading file.\n"); break; } s=ch%16; /*Get the lower 4 bits of a byte in the file*/ Send(s); s=ch/16; /*Get the upper 4 bits of a byte in the file*/ Send(s); }while(!feof(fp)); s=46; /*Send the end of the file*/ Send(s); Send(s); fclose(fp); } void Send(s) int s; { int G; SendPort(PORT,s); G=ReadPort(PORT); /*Wait for handshake signal*/ if(s!=G) s=s+16; do{ SendPort(PORT,s); G=ReadPort(PORT);/*Wait for handshake signal*/ }while(s!=G); } void SendFileName(fname) char *fname; { int s,ch; printf("Now transmit the file. Please wait..."); while(*fname){ ch=(int)fname++; s=ch%16; /*Get the lower 4 bits of a byte in the file name*/ Send(s); s=ch/16; Send(s); /*Get the lower 4 bits of a byte in the file name*/ } s=32; /*Send the end mark of the file name*/ Send(s); Send(s); } void ReceiveFile(){ FILE *fp; char ch; int G1,G2,G3; char fname[15]; GetFileName(fname); printf("Receiving file %s.\n",fname); remove(fname); if((fp=fopen(fname,"wb"))==NULL) { printf("Can''''t open output file.\n"); exit(1); } /*The loop is to detect whether the data received each time is new data, if not, */
/*Then use the data received this time to overwrite the data received last time*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*Restore the separated data and combine the high 4 bits and the low 4 bits*/
putc(ch,fp);
if(ferror(fp)){
printf("\nError writing file.");
exit(1);
}
G2=Receive(PORT,&G3);
G1=G3;
}while(G1/16!=48);
printf("\nTransmit finished.");
fclose(fp);
}
int Receive(port,G)
int port,*G;
{
int GM;
SendPort(port,*G);
GM=ReadPort(port);
if(GM/16==0)
return GM;
else if(GM/16==1){
do{
*G=GM;
SendPort(port,GM);
GM=ReadPort(port);
}while(GM/16==1);
}
return GM;
}
void GetFileName(char *f)
{
int G1,G2,G3;
char ch; G1 =
ReadPort(PORT);
G2=ReadPort(PORT);
do{
G3=Receive(PORT,&G3); ch
=(char)(G1%16+G2/16);
*f=ch; ) ; G1=G3; }while(G1/16!=32); printf("File name transmit finished.\n"); } void InitPort (port,para) int port; unsigned char para; { union REGS reg ; reg . char c ; { union REGS reg; reg.x.dx=port; reg.h.al=c; reg.h.ah=1; int86(0x14,?,?); if(reg.h.ah&128){ printf("\nSend mistakes!"); exit(1); } } int ReadPort(port) int port; { union REGS reg; while(!(CheckState(port)&256)){ if(kbhit()){/*If the port has no data for a long time, you can terminate the waiting manually*/ printf("Press any key to exit."); getch(); exit(1); } } reg.x.dx=port; reg.h.ah=2; int86(0x14,?,?); if(reg.h.ah&128){ printf("\nRead mistake!"); exit(1); } return reg.h.al; } int CheckState(port) int port; { union REGS reg; reg.x.dx=port; reg.h.ah=3; int86(0x14,?,?); return reg.x.ax; } Nowadays, there are many kinds of popular programming software, which are easy to program and maintain, but they are helpless when dealing with hardware directly and compiling system software, so C language comes in handy. As a transitional language between assembly language and high-level language, C language has the high efficiency of assembly language and the convenience of high-level language. In communication, in order to ensure safe and reliable operation, the standard serial port must have many handshake signals and status information. This is because the CPU speeds of the communicating computers are different (which will cause "wrong frames") and the transmitter sends data faster than the receiver receives it (which will cause "overshoot"). To solve this problem
To solve the problem, we use a simple handshake signal, that is, the transmitter only sends half a byte (lower 4 bits)
of data each time, and the other half a byte (upper 4 bits) is used to transmit information. We can encode the information bits (upper 4 bits)
as follows:
0H: Sending new half-byte data
1H: Resending the last transmitted error data
2H: End of file name
3H: End of file
In this way, every time the transmitter sends a byte, it waits for the receiver to send a return signal, which
is the byte sent by the transmitter. After receiving the return signal, the transmitter
compares it with the byte just sent. If they are the same, it sends a new half byte, otherwise it resends. The new data is distinguished from the old data
by the information bit. The following is a program written in C language to control the serial port.
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char *fname); /* Send file*/
void Send(int s); /* Send a byte*/
void SendFileName(char *fname); /* Send file name*/
void ReceiveFile(); /* Receive file*/
void GetFileName(char *f); /* Receive file name*/
void InitPort(int port,unsigned char para); /* Initialize port*/
void SendPort(int port,char c); /* Port send*/
int ReadPort(int port); /* Read port byte*/
int CheckState(int port); /* Check port status*/
int Receive(int port,int *G); /* Receive a byte*/
main(int argc,char *argv[])
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:"); ReceiveFile();
else { printf("Error parament. Please input again."); exit (1);
} } void SendFile(char *fname) { FILE * fp ; int ch,s; if((fp=fopen(fname , " rb"))==NULL) { printf("Can' '''t open the file .\n"); exit(1); } SendFileName ( fname); do{ ch=(int)getc(fp); if(ferror(fp)){ printf ("Error reading file.\n"); break; } s=ch%16; /*Get the lower 4 bits of a byte in the file*/ Send(s); s=ch/16; /*Get the upper 4 bits of a byte in the file*/ Send(s); }while(!feof(fp)); s=46; /*Send the end of the file*/ Send(s); Send(s); fclose(fp); } void Send(s) int s; { int G; SendPort(PORT,s); G=ReadPort(PORT); /*Wait for handshake signal*/ if(s!=G) s=s+16; do{ SendPort(PORT,s); G=ReadPort(PORT);/*Wait for handshake signal*/ }while(s!=G); } void SendFileName(fname) char *fname; { int s,ch; printf("Now transmit the file. Please wait..."); while(*fname){ ch=(int)fname++; s=ch%16; /*Get the lower 4 bits of a byte in the file name*/ Send(s); s=ch/16; Send(s); /*Get the lower 4 bits of a byte in the file name*/ } s=32; /*Send the end mark of the file name*/ Send(s); Send(s); } void ReceiveFile(){ FILE *fp; char ch; int G1,G2,G3; char fname[15]; GetFileName(fname); printf("Receiving file %s.\n",fname); remove(fname); if((fp=fopen(fname,"wb"))==NULL) { printf("Can''''t open output file.\n"); exit(1); } /*The loop is to detect whether the data received each time is new data, if not, */
/*Then use the data received this time to overwrite the data received last time*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*Restore the separated data and combine the high 4 bits and the low 4 bits*/
putc(ch,fp);
if(ferror(fp)){
printf("\nError writing file.");
exit(1);
}
G2=Receive(PORT,&G3);
G1=G3;
}while(G1/16!=48);
printf("\nTransmit finished.");
fclose(fp);
}
int Receive(port,G)
int port,*G;
{
int GM;
SendPort(port,*G);
GM=ReadPort(port);
if(GM/16==0)
return GM;
else if(GM/16==1){
do{
*G=GM;
SendPort(port,GM);
GM=ReadPort(port);
}while(GM/16==1);
}
return GM;
}
void GetFileName(char *f)
{
int G1,G2,G3;
char ch;
G1=ReadPort(PORT);
G2=ReadPort(PORT);
do{
G3=Receive(PORT,&G3); ch
=(char)(G1%16+G2/16) ; G2=Receive(PORT,&G3); G1=G3; }while(G1/16!=32); printf("File name transmit finished.\n"); } void InitPort(port,para) int port; unsigned char para; { union REGS reg; reg.x.dx=port; reg.h.ah=0; reg.h.al=para; int86(0x14,?,?); } void SendPort(port,c) int port; char c; { union REGS reg; reg.x.dx=port; reg.h.al=c; reg.h.ah=1; int86(0x14,?,?); if(reg.h.ah&128){ printf("\nSend mistakes!"); exit(1); } } int ReadPort(port) int port; { union REGS reg; while(!(CheckState(port)&256)){ if(kbhit()){/*If the port has no data for a long time, you can terminate the waiting manually*/ printf("Press any key to exit."); getch(); exit(1); } } reg.x.dx=port; reg.h.ah=2; int86(0x14,?,?); if(reg.h.ah&128){ printf("\nRead mistake!"); exit(1); } return reg.h.al; } int CheckState(port) int port; { union REGS reg; reg.x.dx=port; reg.h.ah=3; int86(0x14,?,?); return reg.x.ax; }