procedure TForm1.Button1Click(Sender: TObject); var CommTimeOuts:Tcommtimeouts; TreadId:Dword; begin CommHand:=CreateFile( 'com1', Generic_Read or Generic_write, 0, nil, Open_existing, file_attribute_normal, 0 ); Setupcomm(commhand,4096,4096); purgecomm(commhand,purge_txabort or purge_rxabort or purge_txclear or purge_rxclear); if not getcommstate(commhand,Tmpdcb) then begin showmessage('Get Comm State faile'); closehandle(commhand); exit; end; tmpdcb.DCBlength :=sizeof(Tdcb); tmpdcb.BaudRate := 9600; tmpdcb.ByteSize := 8; tmpdcb.Parity := 0; tmpdcb.StopBits := Onestopbit; //- commtimeouts.ReadIntervalTimeout :=MaxDword; Commtimeouts.ReadTotalTimeoutMultiplier :=0; commtimeouts.ReadTotalTimeoutConstant :=0; commtimeouts.WriteTotalTimeoutMultiplier :=round(2*9600 / tmpdcb.BaudRate); Commtimeouts.WriteTotalTimeoutConstant :=25; if not setcommtimeouts(commhand,commtimeouts) then showmessage('Set TimeOuts Faile!'); if setcommstate(commhand,tmpdcb) then begin button2.Enabled :=true; button3.Enabled :=true; end; if not getcommstate(commhand,Tmpdcb) then begin showmessage('Get Comm State faile'); closehandle(commhand); exit; end; purgecomm(commhand,purge_Rxclear or Purge_Txclear); Createthread(nil,0,@ReadComm,nil,0,treadid); end;Procedure ReadComm;stdcall var sendstr:string; tmpwstr:widestring; i:integer; x:dword; Comstat:Tcomstat; RChar:char; begin while true do begin sleep(1); clearcommerror(form1.commhand,x,@Comstat); i:=Comstat.cbInQue ; if (i = 0) then continue; readfile(form1.CommHand,Rchar,1,x,nil); sendstr:=sendstr+Rchar; if Rchar = #13 then begin form1.Memo1.Lines.Add(sendstr); sendstr:=''; end; end; end; 刚好我定一个用线程的,送你啦。w2k通过,在用
to leeicescri:第一台机器的com2联到第二台机器的com1,然后第二台机器通过com1接收数据 谢谢各位,特别是陈欣
串口通讯协议和操作系统有什么关系?
只要知道波特率、数据位、停止位、校验方式,你在Win2k的机器上装个串口通讯的软件收就是了,这很困难么?
var
CommTimeOuts:Tcommtimeouts;
TreadId:Dword;
begin
CommHand:=CreateFile(
'com1',
Generic_Read or Generic_write,
0,
nil,
Open_existing,
file_attribute_normal,
0
);
Setupcomm(commhand,4096,4096);
purgecomm(commhand,purge_txabort or purge_rxabort or purge_txclear or purge_rxclear);
if not getcommstate(commhand,Tmpdcb) then
begin
showmessage('Get Comm State faile');
closehandle(commhand);
exit;
end; tmpdcb.DCBlength :=sizeof(Tdcb);
tmpdcb.BaudRate := 9600;
tmpdcb.ByteSize := 8;
tmpdcb.Parity := 0;
tmpdcb.StopBits := Onestopbit; //-
commtimeouts.ReadIntervalTimeout :=MaxDword;
Commtimeouts.ReadTotalTimeoutMultiplier :=0;
commtimeouts.ReadTotalTimeoutConstant :=0;
commtimeouts.WriteTotalTimeoutMultiplier :=round(2*9600 / tmpdcb.BaudRate);
Commtimeouts.WriteTotalTimeoutConstant :=25;
if not setcommtimeouts(commhand,commtimeouts) then showmessage('Set TimeOuts Faile!'); if setcommstate(commhand,tmpdcb) then
begin
button2.Enabled :=true;
button3.Enabled :=true;
end;
if not getcommstate(commhand,Tmpdcb) then
begin
showmessage('Get Comm State faile');
closehandle(commhand);
exit;
end;
purgecomm(commhand,purge_Rxclear or Purge_Txclear);
Createthread(nil,0,@ReadComm,nil,0,treadid);
end;Procedure ReadComm;stdcall
var
sendstr:string;
tmpwstr:widestring;
i:integer;
x:dword;
Comstat:Tcomstat;
RChar:char;
begin
while true do
begin
sleep(1);
clearcommerror(form1.commhand,x,@Comstat);
i:=Comstat.cbInQue ;
if (i = 0) then continue;
readfile(form1.CommHand,Rchar,1,x,nil);
sendstr:=sendstr+Rchar;
if Rchar = #13 then
begin
form1.Memo1.Lines.Add(sendstr);
sendstr:='';
end;
end;
end;
刚好我定一个用线程的,送你啦。w2k通过,在用
谢谢各位,特别是陈欣
可以在一台机器上模拟两台机器之间的环境吗?
http://act.it.sohu.com/download/show.php?soft=5674如果你想在一台机器上模拟双机通讯的情况,可以利用机器上的2个串口呀!把串口1和2用通讯线联起来,9针插头的2对3、3对2、5对5。当然要知道波特率等通讯参数,如果不知道就只能用各种组合自己试了,看哪种设置收到的数据是正常的。至于如何分析ASCII文件,那别人就帮不了你了!
不通过操作系统基本不可能,现在NT的保护机制几乎不允许直接硬件操作了,死心吧.
“可能很简单,我是用第二台机器的com1监听第一台机器的com2”
你的线是怎么接的?
你在2号干活的话,跟1号的OS没关系的,也就是数据格式,波特率,协议等(就当那个OS2机器是个8051),问题是监听作何解,还有第3台机器么?
把OS/2发的数据当单片机发的就行了,还有串口数据线的发送针pin3和接收针pin2反接。