spcomm串口通讯WriteCommData问题,偶尔不好用啊,相当急啊(100分)

  • 主题发起人 WangZhaoHui
  • 开始时间
W

WangZhaoHui

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我在Timer下使用writecommdata每隔一秒钟会象下位机发送七个字节,否则下位机就会关掉,现在接收一切都正常,但软件运行一段时间后我在Timer下使用writecommdata明明发送成功了,但硬件设备就是没接到数据,用信号源检测也确实没有数据发送出来,这是为什么呢,总是运行一段时间才有这种现象,关了软件重新启动又好用了。太可怕了,是我什么地方没设置明白吗,我在百度里也查了一下,也有网友遇到过类似的问题,但也没找到解决问题的办法。附源码,主要不是总不好用,我也怀疑过发送缓冲区的问题,实在搞不明白了,大家帮帮我
procedure TfrmMain.TimerSendCommandTimer(Sender: TObject);
begin
if uart_flag and (not SendCommanding) then
begin
Inc(SendCommandTimer);
if (SendCommandTimer > 50) then
begin
SendCommandTimer := 0;
PurgeComm(CommMain.Handle, PURGE_TXCLEAR);
Memo1.Lines.Add(Format('%d %d', [ch_temp[0], ch_temp[1]]));
Exit;
end;
Send_Comm_Signal($5C);
end;
end;

procedure TfrmMain.Send_Comm_Signal(signal: Byte);
//UCHAR
var
i: Integer;
checksum: BYTE;
aBoolean: Boolean;
arryCommandByte: array[0..6] of Byte;
begin
FillChar(arryCommandByte, SizeOf(arryCommandByte), 0);
arryCommandByte[0] := $FF;
arryCommandByte[1] := $05;
arryCommandByte[2] := $32;
arryCommandByte[3] := $05;
arryCommandByte[4] := $00;
arryCommandByte[5] := signal;
purgecomm(CommMain.handle, purge_txclear);
checksum := 0;
for i := 1 to 5do
checksum := checksum + arryCommandByte;
if (checksum = $FF) then
Dec(checksum);
arryCommandByte[6] := checksum;
aBoolean := CommMain.WriteCommData(@arryCommandByte, 7);
end;
 
缓冲区满了
 
我清暖冲区了啊。我发送周期是一秒的,发送缓冲区也不可能满呀
 
发送周期不要太快,如果太快有时会将几个包一起收过来了~~
 
也不太快啊。唉
 
用spcomm比较费时间,不过应该没那么夸张,大概100多毫秒的样子。
最好做个flag,没有收到再发送一次
 
mscomm速度相对快点,大概能节约个50毫秒
 

Similar threads

D
回复
0
查看
686
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部