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;
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;