
COMPUTERENGINEERING
1999 257Vol.25No.51999



TS-232ͨŵʵ̽

ժҪ ͨһӦ÷ǳ㷺ͨŷʽڼϵͳмҪáҪʵֿɿĴͨţ൱ѡĿǰͨдڵһЩģɵۣʵĽǶһЩ̽֡
ؼ ͨ ʵ RS-232
The Practical Research on RS-232 Communication
shen Hongwei
Electro-mechanic Dept.of Shaoxin Liberal Arts and Science Institute Shaoxin 312000
AbstractAs one kind of communication mode used in computer systemsRS-232 communication is very important.But it is quite difficult to bring about reliable RS-232 communication .This paper disucusses the problem of how to achieve RS-232 communication based on practical research which is directed against some theory on RS-232 communication.
Key wordsSerial communicationPractceRs-232
ͨΪһֱ׼ͨŽӿڣڼ(豸)֮Ϣзǳ㷺ʵӦüֵ
ʵʵӦÿУǷйشͨŵЩ⣬ֵһ֣̽ʵĽǶȼȷ
ʵʶӼԴ̸̸ͨԼһЩ
1Ŀǰйشͨŵļ
йشͨŵϣ˵ǳࡣ¼ϣձģ
1.1ͨŵľ
϶RS-232-Cͨű׼ͨž룬Ϊ޵ƽ£ЧΪ30סȻʵӦУǷԶԶֵ磬2400ʽPCĵԵ㴮ͨţ4оͨ£ͨžӵ100ʱܿɿͨţʵϣDEC˾ʵ飬ε»εʱ2400ʵͨžֱɴ300׼150סʱҲ
1.2ͨŵĲ
ڱ׼ĲʣPCϵĲʲѾΪ׼ֻҪֱʹö⡣豸ĲʲIntel 8031΢Ϊں˵ϵͳʲȷͱԴرǵԴĴPCONеSMODλá6MHzϵͳΪҪ2400ĲʣɶʱT1ڷʽ2(Զװطʽ)SMODȡ0ʱʲΪ
X2490F9H
SMODȡ1ʱʲΪ
X2430F3H
ʵʵĲʼֱΪ
SMOD0ʱ2232.17
SMOD1ʱ2403.80.16
ɴ˿ɼSMODѡ0ʱһΪͨ5Ҫܽͨˡ
1.3ͨŵַʽ
ڴͨУԲӲַʽĿǰϳõֵ3Ʒʽ"漼"TXDRXDźŵ3߽ͨš
ǣַʽУڲBIOSõĴͨųƣ BIOSԴͨŵصҪ߶ӡBIOSźŵҪǣ
óʼʱҪκźţ
ýպʱҪDTRRTSرգ
÷ͺʱҪDTRRTS򿪣ȴ豸DSRCTSδյڳʱ󷵻أִзͶ
ˣڲBIOSʱʹпڹַʽDSRDTR̽ӣRTSCTS̽ӣ򣬲ܽͨš
2ͨŵʵ
2.1DOSµĴͨʵ
DOSɼԦӲʺڲϵͳDOS£ûֱӶԴж˿ڲĳһȽʵ֡ȻڲTCBIOSʱԭ򣬶ʵ֡
ĴвѯͳTC2.0дͨ
#include"stdio.h"
#include"bios.h"
#include"dos.h"
main(int argc,char*argv[])
{FILE *fp;
char ch1,ljh=0,len=0,count=2;/*ͨʧʱһ*/
unsigned char ch;
unsigned int i,j,k;
int status;
if(argc<2)
{printf("\nUsage:send file.ext\n");
exit(0);
}
/*4佫ڳʼΪ2400,8λ,У,1ֹͣλ*/
outportb(0x2fb,0x80);
outportb(0x2f8,0x30);
outportb(0x2f9,0x00);
outportb(0x2fb,0x03);
/*4BIOSCOM(0,0XA3,1);BIOSʼ*/
ch=0xaa;/*ʼֽͨ*/
do ch1=inportb(0x2fd);
while((ch1&0x20)!=0x20);/*ѯͼĴΪշ*/
outportb(0x2f8,ch); /*Ϊ, */
/*ע:ҪBIOSCOM(1,ch,1)з,Ҫʹڹַʽ;,޷ͨ.Ȼ*/
do ch1=inportb(0x2fd);
while((ch1&0x01)!=0x01);/*ȴӻӦ*/
ch=inportb(0x2f8);
if(ch==0xaa)/*Ӧȷ*/
ll:{/*Ӧȷ,ָļе18ֽڷͳȥ */
if((fp=fopen(argv[1],"rb"))==NULL)
{printf("cannot open this file.\n");
exit();
}
fread(&ch,1,1,fp);
while(len<18)
{do ch1=inportb(0x2fd);
while((ch1&0x20)!=0x20);
outportb(0x2f8,ch);
ljh+=ch;/*ۼӺ(У)*/
len++;
fread(&ch,1,1,fp);
}
fclose(fp);
do ch1=inportb(0x2fd);
while((ch1&0x01)!=0x01);
ch=inportb(0x2f8);/*մӻۼӺ*/Sif(ch==ljh)
{/*ȷ,ȷ־ֽ0X00*/
for(j=0;j<1000;j++)
for(i=0;i<30000;i++) ;/*ʱԵȴӻ
ӷл*/
ch=0x00;
do ch1=inportb(0x2fd);
while((ch1&0x20)!=0x20);
outportb(0x2f8,ch);
for(j=0;j<30;j++)
for(i=0;i<30000;i++) ;
printf("\n");
printf("OK");
exit();
}
else/*ȷ,ʹ־ֽ0XFF*/
{for(j=0;j<1000;j++)
for(i=0;i<30000;i++) ;
ch=0xff;
do ch1=inportb(0x2fd);
while((ch1&0x20)!=0x20);
outportb(0x2f8,ch);
for(j=0;j<30;j++)
for(i=0;i<30000;i++) ;
ljh=0;
--count;
if(count)gotoll;/*һ*/
else printf("\nFailure in communication.\n");
}
}
}
2.2WindowsµĴͨʵ
Windows 95µĴͨʵһ3ַʽ
(1)WindowsAPIӿں
(2)RADϵͳ(VB)ActiveXؼ
(3)DLL̬ӿֱӲж˿ڡ
3ַʽص㣬ǲõַʽʵWindows 95¿ɿĴͨţַʽصӦ㡣
ǻDelphi 4.0ڴͨŵDLL
library Mycomdll;
uses
SysUtils,
Classes;
Const
COM1=$3f8;{崮ַ}
COM2=$2F8;
Var
COM:Word;{DLLȫֱ}
Procedure Outb(Const Port:Word;Const Dbyte:Byte);pascal;
Begin
Asm
MOVDX,Port
MOVAL,Dbyte
OUTDX,AL
End;
End;
Function Inb(Const Port:Word):Byte;pascal;
Begin
Asm
MOVDX,Port
INAL,DX
MOV@Result,AL
End;
End;
{пڳʼ,COM1,COM2ѡ,2400,4800ѡ}
Procedure CommInit(CONST Port:ByteCONST BaudString)Stdcall
Var
BAUDLBAUDH:Byte
Begin
if Port=1 then
COM:=COM1
else
COM:=COM2;
if BAUD='2400'then
Begin
BAUDL:=$30;
BAUDH:=$00;
End
else
Begin
BAUDL:=$18;
BAUDH:=$00;
End;
Outb(COM+3,$80);{ò}
Outb(COM,BAUDL);
Outb(COM+1,BAUDH);
Outb(COM+3,$03){8λݣ1ֹͣλУ}
End;
{һֽ}
Procedure SendByte(Const Dbyte:Byte);Stdcall;
Var
Status:Byte;
Begin
Repeat
Status:=Inb(COM+5);
until((Status and $20)=$20);
Outb(COM,Dbyte);
End;
{һֽ}
Function ReceiveByte:Byte;Stdcall;
Var
Status,Res:Byte;
Begin
Repeat
Status:=Inb(COM+5);
Until((Status and $01)=$01);
res:=Inb(COM);
ReceiveByte:=Res;
End;
{EXPORTS̻}
Exports CommInit;
Exports SendByte;
Exports ReceiveByte;
begin
end.
2.3ͨŵʵ
ͨһӷʽһΪΪӣȻٰͳһͨЭͨšͼ1һĿǰй·򻯸Ľʵýӿڷ

ͼ1Ľʵýӿ
ע⣬ÿӻķͽTXDͨض4148ٹҽӵĽϣ򣬻źųͻӰͨţܵ´𻵡
2.4ͨųļ򵥵
ͨųĵ˵ǱȽ鷳ġһԲ²裺
(1)ȷƷʽУҪע""⣻BIOSýӷݣ Ҫһעźŵȷӡ
(2)򵥵߼ʼ鷢źŵ(ע߼ֻܼTTLźţ ˣһΪMAX232ȽӿоƬתTXDRXD)
(3)ȷнӷźŵǰ£ӷݲصӦͨЭһ·磬ʵãżУֹͣλͨŵӦȡ
(4)ֻеµĴڳԣɲýڵTXDRXDֱİ취򵥷㡣
3
ϹܼǱڶĴͨʵеһЩᡣϣשãʹͬйشͨŵӦоУ""Ӷһ· 
߼飺 У33꣬ʦҪ¼ƼӦ÷Ľѧй
ߵλѧԺϵ  312000
ο
1».DELPHI 3ָ(ϱ).磬1998-06
2ȫ˹׹.Ƭʵü.ӳ磬1992-03
ոڣ1998-02-02
