adtcommimpl.~pas
来自「完成Linux下的串口程序,串口发送延时程序」· ~PAS 代码 · 共 1,443 行 · 第 1/4 页
~PAS
1,443 行
Success := True;
Result := True;
end;
except
ShowMessage('写模式打开新文件“' + FileName + '”时出错!');
end;
end;
{
//连续写文件数据
function WriteFlie(Addr, FileName: string; var Success: Boolean; ComPort: TComPort): Boolean;
var
i, j, intHandle, intNumRead, intBytes, intFileSize: Integer;
buf: array[0..50] of Char; //缓冲区:50
strAnswer, strTemp: string;
F: file of Byte;
begin
intHandle := 0;
Result := False;
Success := False;
intBytes := High(buf) - Low(buf) + 1;
try
try
if (UpperCase(ExtractFileExt(FileName)) = '.BMP') then
begin
AssignFile(F, FileName);
ReSet(F);
intFileSize := FileSize(F);
for i := 1 to ((intFileSize - 1) div intBytes) + 1 do
begin
if ((intFileSize mod intBytes) = 0) or (i <> ((intFileSize - 1) div intBytes) + 1) then
intNumRead := intBytes
else
intNumRead := (intFileSize mod intBytes);
buf := '';
BlockRead(F, buf, intNumRead);
strTemp := '';
for j := 0 to intNumRead - 1 do
strTemp := strTemp + buf[j];
if CommSend(Addr, '14', CmdFormat(Addr, '14', strTemp, intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else begin
Result := False;
Success := False;
end;
end;
end
else begin
intHandle := FileOpen(FileName, fmOpenRead);//打开文件,获取文件句柄
while True do
begin
buf := '';
intNumRead := FileRead(intHandle, buf, High(buf) - Low(buf) + 1);//读取数据,获取读取数据的字节数
if intNumRead = 0 then//如果读取数据的字节数为0,则退出循环
Break;
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(buf)), intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else begin
Result := False;
Success := False;
end;
end;
end;
finally
if (UpperCase(ExtractFileExt(FileName)) = '.BMP') then
CloseFile(F)
else
FileClose(intHandle);//关闭文件
end;
except
ShowMessage('连续写文件数据“' + FileName + '”时出错!');
end;
end;
}
//连续写文件数据
function WriteFlie(Addr, FileName: string; var Success: Boolean; ComPort: TComPort): Boolean;
var
i, j,k,m,n,intHandle, intNumRead, intBytes, intFileSize: Integer;
buf:array[0..30] of Char; //51
bufEven: array[0..51] of Char; //缓冲区:53 xtm
bufOdd: array[0..52] of Char;//缓冲区:53 奇数数列
buf1:array[1..9] of Char;
strAnswer, strTemp: string;
strTail:integer;
strNewTail :char;
F: file of Byte;
debugstr:string;
begin
intHandle := 0;
Result := False;
Success := False;
intBytes := High(buf) - Low(buf) + 1;
strTail := 2;
strNewTail :=#0;
try
try
if (UpperCase(ExtractFileExt(FileName)) = '.BMP') then
begin
AssignFile(F, FileName);
ReSet(F);
intFileSize := FileSize(F);
for i := 1 to ((intFileSize - 1) div intBytes) + 1 do
begin
if ((intFileSize mod intBytes) = 0) or (i <> ((intFileSize - 1) div intBytes) + 1) then
intNumRead := intBytes
else
intNumRead := (intFileSize mod intBytes);
buf := '';
BlockRead(F, buf, intNumRead);
strTemp := '';
for j := 0 to intNumRead - 1 do
strTemp := strTemp + buf[j];
if CommSend(Addr, '14', CmdFormat(Addr, '14', strTemp, intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else begin
Result := False;
Success := False;
end;
end;
end
else
begin
intHandle := FileOpen(FileName, fmOpenRead);//打开文件,获取文件句柄
while True do
begin
if(SelChangeLine =1) then
begin
bufEven := '';
bufOdd := '';
if(CallTimeSum=0) then
begin
CallTimeSum := 1;
end
else
begin
if(WireSel=1) then //假如选择无线
intNumRead := FileRead(intHandle, buf1, High(buf1) - Low(buf1) + 1)//读取数据,获取读取数据的字节数
else
intNumRead := FileRead(intHandle, bufEven, High(bufEven) - Low(bufEven) + 1);
if intNumRead = 0 then//如果读取数据的字节数为0,则退出循环
Break;
m := 0;
n := 0; //判断是否为偶数或者是奇数
while m<High(bufEven) do
begin
if (ord(bufEven[m])>128) then
Inc(m)
else
Inc(n);
Inc(m);
end;
if (n MOD 2 = 0) then
strTail := 2 //偶数
else
strTail := 1; //奇数
if(strTail=1) then
begin
FileRead(intHandle, strNewTail,1); //则再读取下面的一个,凑为整数个
end;
if(WireSel=1) then //假如选择无线
begin
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(buf1)), intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else
begin
Result := False;
Success := False;
end;
end
else
begin
if(strTail<>1) then //偶数
begin
debugstr := StrToValid(string(bufEven));
bufOdd[intNumRead] := ' ';
for k:=0 to intNumRead-1 do
bufOdd[k] := bufEven[k];
debugstr := StrToValid(string(bufOdd));
// if(LocateLine(Addr,StrToValid(string(bufOdd)),ComPort)=false) then
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(bufOdd)), intNumRead+1), strAnswer, ComPort) then
begin
if(boolSendLine=true) then
if(SelChangeLine =1) then
//if(CallTimeSum<>0) then //不是第一次也不是第一行
// CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(CmdOdd)), 2), strAnswer, ComPort);
// CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(CmdChange)), 3), strAnswer, ComPort);
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
CallTimeSum := 1;
continue;
end;
end
else
begin
Result := False;
Success := False;
end
else
begin
Success := True;
CallTimeSum := 1;
continue;
end;
end
else //以下为奇数情况
begin
bufOdd[intNumRead] := strNewTail;
for k:=0 to intNumRead-1 do
bufOdd[k] := bufEven[k];
debugstr := StrToValid(string(bufOdd));
if(LocateLine(Addr,StrToValid(string(bufOdd)),ComPort)=false) then
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(bufOdd)), intNumRead+1), strAnswer, ComPort) then
begin
if(boolSendLine=true) then
if(SelChangeLine =1) then
// if(CallTimeSum<>0) then //不是第一次也不是第一行
CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(CmdOdd)), 2), strAnswer, ComPort);
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
CallTimeSum := 1;
continue;
end;
end
else
begin
Result := False;
Success := False;
end
else
begin
Success := True;
CallTimeSum := 1;
continue;
end;
end;
end;
end;
end
else
begin
buf := '';
if(WireSel=1) then //假如选择无线
intNumRead := FileRead(intHandle, buf1, High(buf1) - Low(buf1) + 1)//读取数据,获取读取数据的字节数
else
intNumRead := FileRead(intHandle, buf, High(buf) - Low(buf) + 1);//读取数据,获取读取数据的字节数
if intNumRead = 0 then//如果读取数据的字节数为0,则退出循环
Break;
if(WireSel=1) then //假如选择无线
begin
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(buf1)), intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else
begin
Result := False;
Success := False;
end;
end
else
begin
if CommSend(Addr, '14', CmdFormat(Addr, '14', StrToValid(string(buf)), intNumRead), strAnswer, ComPort) then
begin
if Copy(strAnswer, 1, 2) = '01' then//指令执行成功
begin
Result := True;
Success := True;
continue;
end;
end
else
begin
Result := False;
Success := False;
end;
end;
end;
end;
end;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?