C一般用來底層開發,如操作系統,嵌入式開發,或者要求效率,高可移植性的地方。C對人要求很高,程序員要考慮的地方太多。他的特點就是每一個字節都可以精確控制,不象C++,編譯器為你自動加的東西太多,效率也就低了。
windows 就是用它來開發的,linux很多程序也用它來寫。
例如:用C語言編寫簡單的接口程序.在當今,流行的編程軟件種類繁多,它們編程方便、易于維護,但是在與硬件直接打交道和編制系統軟件時卻束手無策,于是C語言就有了用武之地。C語言作為匯編語言與高級語言之間的一種過渡語言,兼有匯編語言的高效和高級語言的方便。
在通訊中,為了保證行運安全可靠,標準的串行口必須具有許多握手信號和狀態信息。這是因為通訊的各個計算機CPU速度不一樣(這會導致“錯幀”)以及發送機發送數據速度比接收機接收速度快(這會導致“過沖”)。為解決這個問題,我們采用一個簡單的握手信號,即發送機每次僅發送半個字節(低4位)的數據,而另外半個字節(高4位)則用來傳送信息。我們可以對信息位(高4位)進行如下簡單的編碼:
0H:發送的是新的半個字節數據
1H:重新發送上次傳送錯誤的數據
2H:文件名結束
3H:文件結束
這樣,每當發送機發送一個字節以后,就等待接受機發回送信號,這回送信號就是發送機發送過來的那個字節。發送機接收到回送信號后,把它與剛發送的字節相比較,如果相同,就發送新的半個字節,否則就重新發送。新數據與舊數據通過信息位來區分。下面就是我用C語言編寫控制串行口的程序。
我們以一個發送文件的程序為例,介紹一下用C語言實現對接口的控制。用C語言編寫簡單的接口程序源代碼:
#include "dos.h"?????????? /*c的特點在于只要有頭文件,就能和任意文件接上*/
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char fname); /* 發送文件*/
void Send(int s); /*發送一個字節*/
void SendFileName(char fname); /*發送文件名*/
void ReceiveFile(); /*接收文件*/
void GetFileName(char f); /*接收文件名*/
void InitPort(int port,unsigned char para); /*初始化端口*/
void SendPort(int port,char c); /*端口發送*/
int ReadPort(int port); /*讀端口字節*/
int CheckState(int port); /*檢查端口狀態*/
int Receive(int port,int G); /*接收一個字節*/
main(argc,argv)
int argc;
char *argv[];
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);
if(argv[1]=='S') /*檢查選擇的有效性*/
SendFile(argv[2]);
else if(argv[1]=='R')
ReceiveFile();
else{
printf("Error parament.Please input again.");
exit(1);
}
}
void SendFile(fname)
char *fname;
{
FILE *fp;
int ch,s;
if(!(fp=fopen(fname,"rb"))){
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; /*取文件中一個字節的低4位*/
Send(s);
s=ch/16; /*取文件中一個字節的高4位*/
Send(s);
}while(!feof(fp));
s=46; /*發送文件結束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(s)
int s;
{
int G;
SendPort(PORT,s);
G=ReadPort(PORT); /*等待握手信號*/
if(s!=G)
s=s+16;
do{
SendPort(PORT,s);
G=ReadPort(PORT);/*等待握手信號*/
}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; /*取文件名中一個字節的低4位*/
Send(s);
s=ch/16;
Send(s); /*取文件名中一個字節的低4位*/
}
s=32; /*發送文件名結束標志*/
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"))){
printf("Can't open output file.\n");
exit(1);
}
/*循環為檢測每次接受的數據是否為新數據,如果不是,*/
/*則用此次接收的數據覆蓋上次接收的數據*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*恢復分開的數據,組合高4位和低4位*/
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(f)
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;
*f++;
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()){/*如端口長期無數據可人為終止等待*/
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;
}
以上程序可傳送各種格式的文件,也有一定的自動糾錯能力,但對于異常情況的處理能力比較弱,讀者可以自己改進。由于篇幅限制,對于中斷14H的功能、入口參數及返回參數的意義請讀者自己查有關資料。
**********************************
附錄:現在大多數串行口都遵循RS-232標準,以下是最常用的RS-232信號:
名稱 針號 含義
RTS 4 Request to send(請求發送)
CTS 5 Clear to send(清除發送)
DSR 6 Data set ready(數據設備準備好)
DTR 20 Data terminal ready(數據終端準備好)
TXD 2 Transmit data(發送數據)
RXD 3 Receive data(接收數據)
GRD 7 Ground(接地)
----------------------------------------------------------------
附:經站長調試后在TC2.0下通過的代碼
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char *fname); /* 發送文件*/
void Send(int s); /*發送一個字節*/
void SendFileName(char *fname); /*發送文件名*/
void ReceiveFile(); /*接收文件*/
void GetFileName(char *f); /*接收文件名*/
void InitPort(int port,unsigned char para); /*初始化端口*/
void SendPort(int port,char c); /*端口發送*/
int ReadPort(int port); /*讀端口字節*/
int CheckState(int port); /*檢查端口狀態*/
int Receive(int port,int *G); /*接收一個字節*/
main(int argc,char *argv[])
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);
if(*argv[1]=='S') /*檢查選擇的有效性*/
SendFile(argv[2]);
else if(*argv[1]=='R')
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; /*取文件中一個字節的低4位*/
Send(s);
s=ch/16; /*取文件中一個字節的高4位*/
Send(s);
}while(!feof(fp));
s=46; /*發送文件結束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(s)
int s;
{
int G;
SendPort(PORT,s);
G=ReadPort(PORT); /*等待握手信號*/
if(s!=G)
s=s+16;
do{
SendPort(PORT,s);
G=ReadPort(PORT);/*等待握手信號*/
}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; /*取文件名中一個字節的低4位*/
Send(s);
s=ch/16;
Send(s); /*取文件名中一個字節的低4位*/
}
s=32; /*發送文件名結束標志*/
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);
}
/*循環為檢測每次接受的數據是否為新數據,如果不是,*/
/*則用此次接收的數據覆蓋上次接收的數據*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*恢復分開的數據,組合高4位和低4位*/
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;
*f++;
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()){/*如端口長期無數據可人為終止等待*/
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;
}
windows 就是用它來開發的,linux很多程序也用它來寫。
例如:用C語言編寫簡單的接口程序.在當今,流行的編程軟件種類繁多,它們編程方便、易于維護,但是在與硬件直接打交道和編制系統軟件時卻束手無策,于是C語言就有了用武之地。C語言作為匯編語言與高級語言之間的一種過渡語言,兼有匯編語言的高效和高級語言的方便。
在通訊中,為了保證行運安全可靠,標準的串行口必須具有許多握手信號和狀態信息。這是因為通訊的各個計算機CPU速度不一樣(這會導致“錯幀”)以及發送機發送數據速度比接收機接收速度快(這會導致“過沖”)。為解決這個問題,我們采用一個簡單的握手信號,即發送機每次僅發送半個字節(低4位)的數據,而另外半個字節(高4位)則用來傳送信息。我們可以對信息位(高4位)進行如下簡單的編碼:
0H:發送的是新的半個字節數據
1H:重新發送上次傳送錯誤的數據
2H:文件名結束
3H:文件結束
這樣,每當發送機發送一個字節以后,就等待接受機發回送信號,這回送信號就是發送機發送過來的那個字節。發送機接收到回送信號后,把它與剛發送的字節相比較,如果相同,就發送新的半個字節,否則就重新發送。新數據與舊數據通過信息位來區分。下面就是我用C語言編寫控制串行口的程序。
我們以一個發送文件的程序為例,介紹一下用C語言實現對接口的控制。用C語言編寫簡單的接口程序源代碼:
#include "dos.h"?????????? /*c的特點在于只要有頭文件,就能和任意文件接上*/
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char fname); /* 發送文件*/
void Send(int s); /*發送一個字節*/
void SendFileName(char fname); /*發送文件名*/
void ReceiveFile(); /*接收文件*/
void GetFileName(char f); /*接收文件名*/
void InitPort(int port,unsigned char para); /*初始化端口*/
void SendPort(int port,char c); /*端口發送*/
int ReadPort(int port); /*讀端口字節*/
int CheckState(int port); /*檢查端口狀態*/
int Receive(int port,int G); /*接收一個字節*/
main(argc,argv)
int argc;
char *argv[];
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);
if(argv[1]=='S') /*檢查選擇的有效性*/
SendFile(argv[2]);
else if(argv[1]=='R')
ReceiveFile();
else{
printf("Error parament.Please input again.");
exit(1);
}
}
void SendFile(fname)
char *fname;
{
FILE *fp;
int ch,s;
if(!(fp=fopen(fname,"rb"))){
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; /*取文件中一個字節的低4位*/
Send(s);
s=ch/16; /*取文件中一個字節的高4位*/
Send(s);
}while(!feof(fp));
s=46; /*發送文件結束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(s)
int s;
{
int G;
SendPort(PORT,s);
G=ReadPort(PORT); /*等待握手信號*/
if(s!=G)
s=s+16;
do{
SendPort(PORT,s);
G=ReadPort(PORT);/*等待握手信號*/
}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; /*取文件名中一個字節的低4位*/
Send(s);
s=ch/16;
Send(s); /*取文件名中一個字節的低4位*/
}
s=32; /*發送文件名結束標志*/
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"))){
printf("Can't open output file.\n");
exit(1);
}
/*循環為檢測每次接受的數據是否為新數據,如果不是,*/
/*則用此次接收的數據覆蓋上次接收的數據*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*恢復分開的數據,組合高4位和低4位*/
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(f)
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;
*f++;
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()){/*如端口長期無數據可人為終止等待*/
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;
}
以上程序可傳送各種格式的文件,也有一定的自動糾錯能力,但對于異常情況的處理能力比較弱,讀者可以自己改進。由于篇幅限制,對于中斷14H的功能、入口參數及返回參數的意義請讀者自己查有關資料。
**********************************
附錄:現在大多數串行口都遵循RS-232標準,以下是最常用的RS-232信號:
名稱 針號 含義
RTS 4 Request to send(請求發送)
CTS 5 Clear to send(清除發送)
DSR 6 Data set ready(數據設備準備好)
DTR 20 Data terminal ready(數據終端準備好)
TXD 2 Transmit data(發送數據)
RXD 3 Receive data(接收數據)
GRD 7 Ground(接地)
----------------------------------------------------------------
附:經站長調試后在TC2.0下通過的代碼
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char *fname); /* 發送文件*/
void Send(int s); /*發送一個字節*/
void SendFileName(char *fname); /*發送文件名*/
void ReceiveFile(); /*接收文件*/
void GetFileName(char *f); /*接收文件名*/
void InitPort(int port,unsigned char para); /*初始化端口*/
void SendPort(int port,char c); /*端口發送*/
int ReadPort(int port); /*讀端口字節*/
int CheckState(int port); /*檢查端口狀態*/
int Receive(int port,int *G); /*接收一個字節*/
main(int argc,char *argv[])
{
if(argc<2){
printf("Please input R(receive) or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);
if(*argv[1]=='S') /*檢查選擇的有效性*/
SendFile(argv[2]);
else if(*argv[1]=='R')
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; /*取文件中一個字節的低4位*/
Send(s);
s=ch/16; /*取文件中一個字節的高4位*/
Send(s);
}while(!feof(fp));
s=46; /*發送文件結束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(s)
int s;
{
int G;
SendPort(PORT,s);
G=ReadPort(PORT); /*等待握手信號*/
if(s!=G)
s=s+16;
do{
SendPort(PORT,s);
G=ReadPort(PORT);/*等待握手信號*/
}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; /*取文件名中一個字節的低4位*/
Send(s);
s=ch/16;
Send(s); /*取文件名中一個字節的低4位*/
}
s=32; /*發送文件名結束標志*/
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);
}
/*循環為檢測每次接受的數據是否為新數據,如果不是,*/
/*則用此次接收的數據覆蓋上次接收的數據*/
G1=ReadPort(PORT);
G2=Receive(PORT,&G1);
do{
G3=Receive(PORT,&G2);
ch=(char)(G1%16+G2*16);/*恢復分開的數據,組合高4位和低4位*/
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;
*f++;
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()){/*如端口長期無數據可人為終止等待*/
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;
}