unit uFrame_Dyp002; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, System.ImageList, FMX.ImgList, FMX.Controls.Presentation, FMX.Edit, FMX.Objects,uFrameBase,uShowInfo,uKsoap,uPucFun, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, DateUtils,Winapi.Windows; type TDataPacket = record Line1: string; // 第一行数据 Line2: string; // 第二行数据 Line3: string; // 第三行数据 Line4: string; // 第四行数据 Line5: string; // 第五行数据 Field1: Integer; // 第六行的第一个值 Field2: Integer; // 第六行的第二个值 Field3: Integer; // 第六行的第三个值 Field4: Integer; // 第六行的第四个值 Field5: string; // 第六行的第五个值 end; TFrame_Dyp002 = class(TFrameBase) Rectangle88: TRectangle; Option1: TRectangle; Glyph9: TGlyph; OptionLabel1: TText; Text19: TText; Edit_CaiZhi: TEdit; Option2: TRectangle; Glyph2: TGlyph; OptionLabel3: TText; Text6: TText; Edit_banhou: TEdit; Option3: TRectangle; Glyph3: TGlyph; OptionLabel5: TText; Text23: TText; Edit_jiexi: TEdit; Option4: TRectangle; Glyph12: TGlyph; OptionLabel7: TText; Text28: TText; Edit_xiankuang: TEdit; Option7: TRectangle; Glyph13: TGlyph; OptionLabel9: TText; Text30: TText; Edit_rongchadada: TEdit; ImageList1: TImageList; Option6: TRectangle; Glyph1: TGlyph; Text1: TText; Text2: TText; Edit_rongchaxiao: TEdit; Option5: TRectangle; Glyph4: TGlyph; Text3: TText; Text4: TText; Edit_jianju: TEdit; TxtError: TText; Timer1: TTimer; OpenDialog1: TOpenDialog; Memo1: TMemo; procedure Edit_CaiZhiClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); private iLine: integer; sLine1: string; LastPacket: TDataPacket; procedure TimerTimerAbc; procedure Timer3TimerA; { Private declarations } public procedure FillDefaultValue(sPartnumValue,sDefaultTxt:string);override; //填写默认值 function DoExec():boolean;override; //判断输入是否有效,执行插入主表,或打开窗体 function CheckValid(var sTxt:string):boolean;override; function ParseLastDataPacket(const Content: string): TDataPacket; end; implementation {$R *.fmx} uses uDM, json_webservice, uSafeLog; function TFrame_Dyp002.CheckValid(var sTxt:string):boolean; //返回面次数据 var aa:Extended; begin Result:=false; if Trim(Edit_CaiZhi.Text)='' then begin Error(Edit_CaiZhi,TxtError,'空气盲区不能为空');Exit;end; if Trim(Edit_banhou.Text)='' then begin Error(Edit_banhou,TxtError,'水下盲区不能为空');Exit;end; if Trim(Edit_jiexi.Text)='' then begin Error(Edit_jiexi,TxtError,'回波距离不能为空');Exit;end; if Trim(Edit_xiankuang.Text)='' then begin Error(Edit_xiankuang,TxtError,'盲区差不能为空');Exit;end; if Trim(Edit_jianju.Text)='' then begin Error(Edit_jianju,TxtError,'是否合格不能为空');Exit;end; // if Trim(Edit_rongchaxiao.Text)='' then begin Error(Edit_rongchaxiao,TxtError,'不能为空');Exit;end; // if Trim(Edit_rongchadada.Text)='' then begin Error(Edit_rongchadada,TxtError,'不能为空');Exit;end; if not TryStrToFloat(Edit_CaiZhi.Text,aa) then begin Error(Edit_CaiZhi,TxtError,'空气盲区类型错误,请重新输入');Exit;end; if not TryStrToFloat(Edit_banhou.Text,aa) then begin Error(Edit_banhou,TxtError,'水下盲区类型错误,请重新输入');Exit;end; if not TryStrToFloat(Edit_jiexi.Text,aa) then begin Error(Edit_jiexi,TxtError,'回波距离类型错误,请重新输入');Exit;end; if not TryStrToFloat(Edit_xiankuang.Text,aa) then begin Error(Edit_xiankuang,TxtError,'盲区差类型错误,请重新输入');Exit;end; // if not TryStrToFloat(Edit_rongchaxiao.Text,aa) then begin Error(Edit_rongchaxiao,TxtError,'类型错误,请重新输入');Exit;end; // if not TryStrToFloat(Edit_rongchadada.Text,aa) then begin Error(Edit_rongchadada,TxtError,'类型错误,请重新输入');Exit;end; Result:=true; if Result then Error(Edit_CaiZhi,TxtError,''); end; procedure TFrame_Dyp002.FillDefaultValue(sPartnumValue,sDefaultTxt:string); var tmpList:TStrings; begin if sDefaultTxt='' then Exit; Edit_CaiZhi.Text:=FloatToStr(LastPacket.Field1); Edit_banhou.Text:=FloatToStr(LastPacket.Field2); Edit_jiexi.Text:=FloatToStr(LastPacket.Field3); Edit_xiankuang.Text:=FloatToStr(LastPacket.Field4); Edit_jianju.Text:=LastPacket.Field5; fDevRecord.P_ORG_CODE:= LastPacket.Line1; fDevRecord.P_DIE_NAME:= Edit_jianju.Text; //是否合格 if Assigned(FOnButtonClick) then FOnButtonClick(Self); // 将Frame自身作为Sender传递 // tmpList:=TStringList.Create; // try // tmpList.DelimitedText:= ReplaceStrWork(sDefaultTxt); // if tmpList.Count>4 then // begin // Edit_CaiZhi.Text:=tmpList.Strings[tmpList.Count-5]; // Edit_banhou.Text:=tmpList.Strings[tmpList.Count-4]; // Edit_jiexi.Text:=tmpList.Strings[tmpList.Count-3]; // Edit_xiankuang.Text:=tmpList.Strings[tmpList.Count-2]; // Edit_jianju.Text:=tmpList.Strings[tmpList.Count-1]; // // Edit_rongchaxiao.Text:=tmpList.Strings[20]; // // Edit_rongchadada.Text:=tmpList.Strings[21]; // end; // // {tmpList.DelimitedText:=sPartnumValue; // if tmpList.Count>5 then //这里根据料号返回信息填写缺省值 // Edit_banhou.Text:=tmpList.Strings[5];} // // finally // tmpList.Free; // end; end; function GetLatestFileInDir(const DirPath: string): string; var SearchRec: TSearchRec; LatestTime: TDateTime; CurrentTime: TDateTime; FindResult: Integer; begin Result := ''; LatestTime := 0; // 使用 0 而不是 MinDateTime // 检查目录是否存在 if not DirectoryExists(DirPath) then Exit; try // 使用 FindFirst 开始搜索 FindResult := FindFirst(IncludeTrailingPathDelimiter(DirPath) + 'bd-*.txt', faAnyFile, SearchRec); if FindResult = 0 then begin try repeat // 排除目录和特殊文件 if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and ((SearchRec.Attr and faDirectory) = 0) then begin try // 转换文件时间 CurrentTime := FileDateToDateTime(SearchRec.Time); if (Result = '') or (CurrentTime > LatestTime) then begin LatestTime := CurrentTime; Result := IncludeTrailingPathDelimiter(DirPath) + SearchRec.Name; end; except on E: EConvertError do ; // 忽略时间转换错误,继续处理下一个文件 end; end; until FindNext(SearchRec) <> 0; finally System.SysUtils.FindClose(SearchRec); // 确保搜索句柄被释放 end; end; except on E: EInOutError do Result := ''; // 发生IO错误时返回空字符串 on E: Exception do raise; // 重新抛出其他未处理的异常 end; end; procedure TFrame_Dyp002.Timer1Timer(Sender: TObject); var sFPhat: String; fileContent: string; function TryReadLockedFile(const FileName: string; out Content: string): Boolean; var hFile: THandle; fileSize: Integer; bytesRead: DWORD; buffer: TBytes; begin Result := False; Content := ''; // 使用Windows API以最大共享模式打开文件 hFile := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if hFile = INVALID_HANDLE_VALUE then begin // 记录错误但不中断流程,因为定时器会再次尝试 WorkLog.Messageinfo('Cannot open file "%s": Error %d', [FileName, GetLastError]); Exit; end; try // 获取文件大小 fileSize := GetFileSize(hFile, nil); if fileSize = 0 then begin Result := True; // 空文件 Exit; end; // 读取文件内容 SetLength(buffer, fileSize); if not ReadFile(hFile, buffer[0], fileSize, bytesRead, nil) then begin WorkLog.Messageinfo('Cannot read file "%s": Error %d', [FileName, GetLastError]); Exit; end; // 转换为字符串 (假设UTF-8编码) Content := TEncoding.UTF8.GetString(buffer); Result := True; finally CloseHandle(hFile); end; end; begin inherited; Timer1.Enabled:= False; try try sFPhat := GetLatestFileInDir(dm.MemTableReadKeyValue('tv_DirData', 'locad_dir')); if (sFPhat = '') or not FileExists(sFPhat) then Exit; // 尝试读取文件(使用Windows API以最大共享模式) if not TryReadLockedFile(sFPhat, fileContent) then Exit; // 检查文件内容是否为空 if Trim(fileContent) = '' then begin WorkLog.MessageInfo('File %s is empty', [sFPhat]); Exit; end; // 处理内容 try if (LastPacket.Line1 = '') or (LastPacket.Line1 <> sLine1) then begin LastPacket := ParseLastDataPacket(fileContent); FillDefaultValue('', LastPacket.Line1); sLine1 := LastPacket.Line1; WorkLog.MessageInfo('Successfully processed file: %s', [sFPhat]); end; except on E: Exception do WorkLog.MessageError('Error processing file content: %s', [E.Message]); end; except on E: Exception do WorkLog.MessageError('Unexpected error in Timer1Timer: %s', [E.Message]); end; finally Timer1.Enabled:= true; end; end; procedure TFrame_Dyp002.Timer3TimerA; var sFPhat: String; tmpList: TStringList; fileContent: string; function TryReadFile(const FileName: string; out Content: string): Boolean; var fs: TFileStream; bytes: TBytes; encoding: TEncoding; begin Result := False; try // 尝试以共享读取模式打开文件 fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone); try SetLength(bytes, fs.Size); if fs.Size > 0 then fs.Read(bytes[0], fs.Size); // 尝试检测文件编码 encoding := nil; TEncoding.GetBufferEncoding(bytes, encoding, TEncoding.UTF8); // 转换为字符串 Content := encoding.GetString(bytes); Result := True; finally fs.Free; end; except on E: Exception do begin Content := ''; WorkLog.MessageError('Cannot open file "%s": %s', [FileName, E.Message]); end; end; end; procedure ListfillValue(sDefaultTxt: string); var tmpList: TStrings; ii: Integer; begin if sDefaultTxt = '' then Exit; tmpList := TStringList.Create; try try tmpList.Delimiter := ','; tmpList.DelimitedText := ReplaceStrWork(sDefaultTxt); Memo1.Lines.Clear; for ii := 0 to tmpList.Count - 1 do Memo1.Lines.Add(tmpList.Strings[ii]); except on E: Exception do WorkLog.MessageError('Error in ListfillValue: %s', [E.Message]); end; finally tmpList.Free; end; end; begin inherited; try sFPhat := GetLatestFileInDir(dm.MemTableReadKeyValue('tv_DirData', 'locad_dir')); if (sFPhat = '') or not FileExists(sFPhat) then Exit; // 尝试读取文件(仅一次) if not TryReadFile(sFPhat, fileContent) then Exit; // 检查文件内容是否为空 if Trim(fileContent) = '' then begin WorkLog.MessageInfo('File %s is empty', [sFPhat]); Exit; end; // 处理内容 try if (LastPacket.Line1 = '') or (LastPacket.Line1 <> sLine1) then begin LastPacket := ParseLastDataPacket(fileContent); FillDefaultValue('', LastPacket.Line1); sLine1 := LastPacket.Line1; WorkLog.MessageInfo('Successfully processed file: %s', [sFPhat]); end; except on E: Exception do WorkLog.MessageError('Error processing file content: %s', [E.Message]); end; except on E: Exception do WorkLog.MessageError('Unexpected error in Timer1Timer: %s', [E.Message]); end; end; procedure TFrame_Dyp002.TimerTimerAbc; var sFPhat: String; iLen,y: Integer; tmpList:TStrings; fileStream: TFileStream; // 用于以只读方式打开文件 procedure ListfillValue(sDefaultTxt:string); var tmpList:TStrings; ii: Integer; begin if sDefaultTxt='' then Exit; tmpList:=TStringList.Create; try tmpList.Delimiter:= ','; tmpList.DelimitedText:= ReplaceStrWork(sDefaultTxt); Memo1.Lines.Clear; for ii := 0 to tmpList.Count-1 do Memo1.Lines.Add(tmpList.Strings[ii]); finally tmpList.Free; end; end; begin inherited; sFPhat:= GetLatestFileInDir(dm.MemTableReadKeyValue('tv_DirData','locad_dir')); //本地文件目录 if FileExists(sFPhat) then begin tmpList:= TStringList.Create; fileStream := TFileStream.Create(sFPhat, fmOpenRead or fmShareDenyWrite); // 只读且禁止写入 with tmpList do with tmpList do try try // LoadFromFile(sFPhat, Encoding.UTF8); LoadFromStream(fileStream, Encoding.UTF8); // 从流加载内容 except iLen := 0; end; if (LastPacket.Line1='') or (LastPacket.Line1<>sLine1) then begin // 解析内容,只获取最后一个数据包 LastPacket := ParseLastDataPacket(tmpList.Text); FillDefaultValue('', LastPacket.Line1); sLine1:= LastPacket.Line1; WorkLog.MessageInfo('sFPhat_sLine1:%s,%s ' , [LastPacket.Line1,sLine1]); end; // JSonProcessSensorData:= FormatDataToJSON(tmpList.Text); // iLen := Count; // if iLen>iLine then // begin // // if OpenDialog1.Execute then // Memo1.BeginUpdate; // try // for y := tmpList.Count downto 1 do // if (tmpList.Strings[y-1]<>'') then // begin // // ListfillValue(tmpList.Strings[y-1]); // // Memo1.Text:= tmpList.Strings[y-1]; // // FillDefaultValue('', tmpList.Strings[y-1]); // JSonProcessSensorData:= FormatDataToJSON(tmpList.Strings[y-1]); // FillDefaultValue('', JSonProcessSensorData); // break; // end; // //Memo1.Lines.Assign(tmpList); // 关键修改:将当前TStringList内容复制到Memo // finally // Memo1.EndUpdate; // end; // iLine := tmpList.Count; // // end; finally fileStream.Free; // 释放文件流 Free; end; end; end; function TFrame_Dyp002.DoExec():boolean; var LotNoRecord:TLotNoRecord; vId:Integer; sTxt:string; sError:string; vP_ID:Double; begin Result:=false; if CheckValid(sTxt) then //上传数据 begin LotNoRecord:=TLotNoRecord.Create('-1'); //初始化记录 LotNoRecord.P_LOT:=ksoap.P_Lot; LotNoRecord.P_LOT_TYPE:='正常板'; LotNoRecord.P_Enable:='Y'; LotNoRecord.P_ID:=0; LotNoRecord.P_ORG_CODE:=ksoap.P_ORG_CODE; //廠區 LotNoRecord.P_NUM:=ksoap.P_WORK_NUM; //工號 LotNoRecord.P_PC:=ksoap.P_PC; //製程 LotNoRecord.P_LINE:=ksoap.P_LINE; //線別 LotNoRecord.P_LINE_NUM:=ksoap.P_LINE_NUM; //線別編號 LotNoRecord.P_LINE_SPEED:=StrToFloat(Edit_jiexi.Text); //回波距离 LotNoRecord.P_PRODUCT_PRESSURE:=StrToFloat(Edit_banhou.Text); //入水盲区 LotNoRecord.P_SIDE:=Edit_CaiZhi.Text; //出水盲区 LotNoRecord.P_SHORT_CIRCUIT:=Edit_xiankuang.Text; //盲区差值 LotNoRecord.P_DIE_NAME:=(Edit_jianju.Text); //是否合格 LotNoRecord.P_CREATION_DATE:=FormatDateTime('yyyymmdd hh:mm:ss',Now); sError:=''; // vP_ID:=KJSon.Thread_cf_traceability_seq_f(ksoap.P_ORG_CODE,sError); LotNoRecord.P_ID:=vP_ID; //直接调用API,出错再保存本地 if not KJSon.Thread_Insert_CM_WIP_PROCESS_LINE_HISTORY_NEW(LotNoRecord,sError) then //提交主表不成功 begin if sError='連接數據庫服務器失败' then begin dm.InsertMain(vId,LotNoRecord,true); ShowInfoOK('数据库连接失败,已暂存本地,可以继生产'); Result:=true; end else ShowError(sError); end else begin ShowInfoOK('数据上传成功, 可以继生产'); Result:=true; end; end; end; procedure TFrame_Dyp002.Edit_CaiZhiClick(Sender: TObject); begin inherited; ShowTouchKeyBoard(); end; function TFrame_Dyp002.ParseLastDataPacket(const Content: string): TDataPacket; var Lines: TStringList; i: Integer; DataParts: TArray; PacketFound: Boolean; ResString: string; begin // 初始化默认值 Result.Line1 := ''; Result.Line2 := ''; Result.Line3 := ''; Result.Line4 := ''; Result.Line5 := ''; Result.Field1 := 0; Result.Field2 := 0; Result.Field3 := 0; Result.Field4 := 0; Result.Field5 := ''; Lines := TStringList.Create; try Lines.Text := Content; PacketFound := False; i := 0; while i < Lines.Count do begin // 跳过空行 if Trim(Lines[i]) = '' then begin Inc(i); Continue; end; // 检查是否足够6行非空内容(一个数据包) if i + 5 < Lines.Count then begin // 存储前五行数据 Result.Line1 := Trim(Lines[i]); Result.Line2 := Trim(Lines[i+1]); Result.Line3 := Trim(Lines[i+2]); Result.Line4 := Trim(Lines[i+3]); Result.Line5 := Trim(Lines[i+4]); // 解析第六行(逗号分隔的数据) DataParts := Trim(Lines[i+5]).Split([',']); if Length(DataParts) >= 5 then begin Result.Field1 := StrToIntDef(DataParts[0], 0); Result.Field2 := StrToIntDef(DataParts[1], 0); Result.Field3 := StrToIntDef(DataParts[2], 0); Result.Field4 := StrToIntDef(DataParts[3], 0); Result.Field5 := DataParts[4]; end; PacketFound := True; // 跳过这个数据包的剩余行,继续寻找下一个数据包 Inc(i, 6); end else Break; // 不足6行,结束解析 end; // 如果没有找到任何数据包,重置结果 if not PacketFound then begin Result.Line1 := ''; Result.Line2 := ''; Result.Line3 := ''; Result.Line4 := ''; Result.Line5 := ''; Result.Field1 := 0; Result.Field2 := 0; Result.Field3 := 0; Result.Field4 := 0; Result.Field5 := ''; end; // 将数据包内容组成一个字符串 ResString := '最后一个数据包:' + sLineBreak + ' 第一行: ' + LastPacket.Line1 + sLineBreak + ' 第二行: ' + LastPacket.Line2 + sLineBreak + ' 第三行: ' + LastPacket.Line3 + sLineBreak + ' 第四行: ' + LastPacket.Line4 + sLineBreak + ' 第五行: ' + LastPacket.Line5 + sLineBreak + Format(' 第六行数据: %d, %d, %d, %d, %s', [LastPacket.Field1, LastPacket.Field2, LastPacket.Field3, LastPacket.Field4, LastPacket.Field5]); WorkLog.MessageInfo('sFPhat:%s ' , [ResString]); finally Lines.Free; end; end; end.