W
wendy8657
Unregistered / Unconfirmed
GUEST, unregistred user!
我用串口输出方波,下面是源代码,在form中加一个timer来控制每隔一段时间触发一次,输出接一个电阻后接在示波器上,但是输出的却不是方波,为什么呀?
var fdata:array[1..8] of byte;
procedure TForm1.OpenComm;
var
cc:TCOMMCONFIG;
begin
hcomm:=CreateFile('com1',GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,0,0);//打开com
if(hcomm=INVALID_HANDLE_VALUE) then
//如果com未打开
begin
MessageBox(0,'打开通讯端口错误!!','',MB_OK);
exit;
end;
//设置com的状态
GetCommState(hcomm,cc.dcb);//得知目前com状态
cc.dcb.BaudRate:=CBR_9600;//设置波特率为9600
cc.dcb.ByteSize:=8;//字节为8位
cc.dcb.Parity:=NOPARITY;//Parity为None
cc.dcb.StopBits:=ONESTOPBIT;//1个stop bit
if not SetCommState(hcomm,cc.dcb) then
begin
MessageBox(0,'通讯端口设置错误!!','',MB_OK);
CloseHandle(hcomm);
exit;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);//打开串口
begin
OpenComm;
end;
procedure TForm1.Button2Click(Sender: TObject);//关闭串口
begin
SetCommMask(hcomm,$0);
CloseHandle(hcomm);
close;
end;
//准确延时,延时间隔是微秒
procedure delay(delay_time:integer;c:TLargeInteger);
var t,t1:TLargeInteger;
begin
QueryPerformanceCounter(t);//得到当前计算机运行时钟数目
repeat
QueryPerformanceCounter(t1);//得到当前计算机运行时钟数目
until(t1-t)*1000000/c>=delay_time;
end;
procedure TForm1.Button3Click(Sender: TObject);//发送方波
begin
timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var lrc:Longword;
c:TLargeInteger;
i:integer;
begin
if(hcomm=0)then exit;//检查Handle 值
QueryPerformanceFrequency(c);
//WriteFile(hcomm,'01010101',8,lrc,nil);//送出数据
fdata[1]:=$00;
fdata[2]:=$ff;
fdata[3]:=$00;
fdata[4]:=$ff;
fdata[5]:=$00;
fdata[6]:=$ff;
fdata[7]:=$00;
fdata[8]:=$ff;
for i:=1 to 8 do
begin
WriteFile(hcomm,fdata,length(fdata),lrc,nil);//送出数据
delay(100,c);//方波的宽度,微秒为单位
end;
end;
end.
var fdata:array[1..8] of byte;
procedure TForm1.OpenComm;
var
cc:TCOMMCONFIG;
begin
hcomm:=CreateFile('com1',GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,0,0);//打开com
if(hcomm=INVALID_HANDLE_VALUE) then
//如果com未打开
begin
MessageBox(0,'打开通讯端口错误!!','',MB_OK);
exit;
end;
//设置com的状态
GetCommState(hcomm,cc.dcb);//得知目前com状态
cc.dcb.BaudRate:=CBR_9600;//设置波特率为9600
cc.dcb.ByteSize:=8;//字节为8位
cc.dcb.Parity:=NOPARITY;//Parity为None
cc.dcb.StopBits:=ONESTOPBIT;//1个stop bit
if not SetCommState(hcomm,cc.dcb) then
begin
MessageBox(0,'通讯端口设置错误!!','',MB_OK);
CloseHandle(hcomm);
exit;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);//打开串口
begin
OpenComm;
end;
procedure TForm1.Button2Click(Sender: TObject);//关闭串口
begin
SetCommMask(hcomm,$0);
CloseHandle(hcomm);
close;
end;
//准确延时,延时间隔是微秒
procedure delay(delay_time:integer;c:TLargeInteger);
var t,t1:TLargeInteger;
begin
QueryPerformanceCounter(t);//得到当前计算机运行时钟数目
repeat
QueryPerformanceCounter(t1);//得到当前计算机运行时钟数目
until(t1-t)*1000000/c>=delay_time;
end;
procedure TForm1.Button3Click(Sender: TObject);//发送方波
begin
timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var lrc:Longword;
c:TLargeInteger;
i:integer;
begin
if(hcomm=0)then exit;//检查Handle 值
QueryPerformanceFrequency(c);
//WriteFile(hcomm,'01010101',8,lrc,nil);//送出数据
fdata[1]:=$00;
fdata[2]:=$ff;
fdata[3]:=$00;
fdata[4]:=$ff;
fdata[5]:=$00;
fdata[6]:=$ff;
fdata[7]:=$00;
fdata[8]:=$ff;
for i:=1 to 8 do
begin
WriteFile(hcomm,fdata,length(fdata),lrc,nil);//送出数据
delay(100,c);//方波的宽度,微秒为单位
end;
end;
end.