在好例子网,分享、交流、成长!
您当前所在位置:首页Pascal 开发实例Delphi数据库编程 → delphi串口通讯

delphi串口通讯

Delphi数据库编程

下载此实例
  • 开发语言:Pascal
  • 实例大小:21.85M
  • 下载次数:44
  • 浏览次数:149
  • 发布时间:2021-03-29
  • 实例类别:Delphi数据库编程
  • 发 布 人:gingger
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 串口通讯 串口 通讯

实例介绍

【实例简介】

进行串口接收数据进行解析

【实例截图】

from clipboard

【核心代码】

var
  len,i,offset,recvdlen:Integer;
  reqbuf:array[1..12]of Byte;
  recbuf:array[1..176]of Byte;
  recmem:TMemoryStream;
  chk:Byte;
  tmps:string;
  tmps1:string;
  recsl:TStringList;
  recfile:string;
  sendtimes:integer;
begin
//  pnlRecTimes.Caption:=Format('Download:%2f%% (%d-%d)',[100*recvdlen/len ,recvdlen ,len]) ;
  pnlRecTimes.Caption:='' ;
  Application.ProcessMessages;
  if not rs1.Connected then Exit;
    for i := 0 to 50 do //等待500ms
  begin
    TThread.Sleep(10);
    Application.ProcessMessages;
  end;
 {  if False then
  begin
    btn8.Enabled:=True;
    Application.MessageBox('It is forbidden to read the fault record during operation, please stop the fault record after reading it! '#13#10' (运行时禁止读取故障记录,请停机后再读取故障记录!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
    Exit;
  end;   }
  btn2.Enabled:=False;
  btn8.Enabled:=False;
  tmr1.Enabled:=False;
  reqbuf[ 1]:=$5A;
  reqbuf[ 2]:=$00;
  reqbuf[ 3]:=$40;
  reqbuf[ 4]:=$00;
  reqbuf[ 5]:=$00;
  reqbuf[ 6]:=$00;
  reqbuf[ 7]:=$00;
  reqbuf[ 8]:=$00;
  reqbuf[ 9]:=$00;
  reqbuf[10]:=$00;
  reqbuf[11]:=$00;
  reqbuf[12]:=Byte(0-($5A $40));
  rs1.WriteCommData(PAnsiChar(@reqbuf[1]),12);
  reqType := 2;
  RecBufNew:=False;
  i:=0;
  len:=0;
  while True do
  begin
    i:=i 1;
    if RecBufNew=True then
    begin
      len:=RecdBuf[5] RecdBuf[6]*$100 RecdBuf[7]*$10000;
      break;
    end;
    TThread.Sleep(10);
    Application.ProcessMessages;
    if(i>300)then
    begin
      btn8.Enabled:=True;
      btn2.Enabled:=True;
//      Application.MessageBox('Data extraction failed, please extract again! '#13#10' (数据提取失败,请再次提取!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
      break;
    end;
  end;
  if(i<300)then
  begin
    if len>0 then
    begin
      recmem:=TMemoryStream.Create;
      offset:=0;
      recvdlen:=0;
      //recmem.SetSize(len);
      while(recvdlen<len)do
      begin
        reqbuf[ 1]:=$5A;
        reqbuf[ 2]:=$00;
        reqbuf[ 3]:=$41;
        reqbuf[ 4]:=offset and $FF;
        reqbuf[ 5]:=(offset shr 8) and $FF;
        reqbuf[ 6]:=(offset shr 16) and $FF;
        reqbuf[ 7]:=(offset shr 24) and $FF;
        reqbuf[ 8]:=$B0;
        reqbuf[ 9]:=$00;
        reqbuf[10]:=$00;
        reqbuf[11]:=$00;
        chk:=0;
        for i := 1 to 11 do chk:=chk reqbuf[i];
        reqbuf[12]:=Byte(0-chk);
        rs1.WriteCommData(PAnsiChar(@reqbuf[1]),12);
        reqType := 3;
        RecBufNew:=False;
        i:=0;
        while True do
        begin
          i:=i 1;
          if RecBufNew=True then
          begin
            recmem.Write(@RecdBuf[5],RecLen);
            break;
          end;
          TThread.Sleep(10);  //100改为10
          Application.ProcessMessages;
          if(i>300)then
          begin
//            btn8.Enabled:=True;
//            btn2.Enabled:=True;
//            Application.MessageBox('Data extraction failed, please extract again! '#13#10' (数据提取失败,请再次提取!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
            break;
          end;
        end;
        if(i<300)then
        begin
          recvdlen:=recvdlen RecLen;
          offset:=recvdlen;
          pnlRecTimes.Caption:=Format('Download:%2f%% (%d-%d)',[100*recvdlen/len ,recvdlen ,len]) ;
          Application.ProcessMessages;
          sendtimes:=0;
        end
        else
        begin
          sendtimes:=sendtimes 1;
          if sendtimes>5 then
          begin
            ShowMessage('发送超时,CPU未响应');
            break;
          end;
        end;
      end;
      if(recmem.Size>0)then
      begin
        recsl:=TStringList.Create;
//        recsl.Add('时间,事件,交流输入,直流输入,输出电压,输出电流,充电电流,散热器温度,蓄电池温度,控制板温度,风扇输出');
//        recsl.Add('Time,Event,Input voltage,Boost input voltage,Boost input current 1,Boost input current 2,Boost output voltage,LLC output voltage,LLC output current,Inverter output voltage,Inverter output current,Inverter output frequency,Heat sink temperature,Inverter temperature,Fan duty cycle,SysStatusSignal,CPU Version,Inverter Version');
        tmps:='Time,Event,Input voltage,Boost input voltage,Boost input current 1,Boost input current 2,Boost output voltage,LLC output voltage,LLC output current,';
            tmps:=tmps 'Inverter output voltage,Inverter output current,Inverter output frequency,Heat sink temperature,Inverter temperature,Fan duty cycle,SysStatusSignal,CPU Version,Inverter Version';
        recsl.Add(tmps);
        recmem.Position:=0;
        while recmem.Position<recmem.Size do
        begin
          recmem.Read(recbuf[1],44);
          tmps:='';
          case recbuf[43] of
          $80: begin tmps:='DC input undervoltage' ; end;
            $B0: begin tmps:='DC input undervoltage recovery' ; end;
          $81: begin tmps:='DC input overvoltage' ; end;
            $B1: begin tmps:='DC input overvoltage recovery' ; end;
          $82: begin tmps:='Boost inductor L1 overcurrent' ; end;
            $B2: begin tmps:='Boost inductor L1 overcurrent recovery' ; end;
          $83: begin tmps:='Boost inductor L2 overcurrent' ; end;
            $B3: begin tmps:='Boost inductor L2 overcurrent recovery'   ; end;
          $84: begin tmps:='LLC output overvoltage'   ; end;
            $B4: begin tmps:='LLC output overvoltage recovery'     ; end;
          $85: begin tmps:='LLC primary side overcurrent'  ; end;
            $B5: begin tmps:='LLC primary side overcurrent recovery'; end;
          $86: begin tmps:='LLC output overcurrent' ; end;
            $B6: begin tmps:='LLC output overcurrent recovery'; end;
          $87: begin tmps:='AC output undervoltage'  ; end;
            $B7: begin tmps:='AC output undervoltage recovery'     ; end;
          $88: begin tmps:='AC output overvoltage' ; end;
            $B8: begin tmps:='AC output overvoltage recovery' ; end;
          $89: begin tmps:='AC output overcurrent' ; end;
            $B9: begin tmps:='AC output overcurrent recovery' ; end;
          $8A: begin tmps:='AC output short circuit' ; end;
            $BA: begin tmps:='AC output short circuit recovery' ; end;
          $8B: begin tmps:='Total hardware protection' ; end;
            $BB: begin tmps:='Total hardware protection recovery'   ; end;
          $8C: begin tmps:='Fan failure'   ; end;
            $BC: begin tmps:='Fan failure recovery'     ; end;
          $8D: begin tmps:='RS485 communication failure'  ; end;
           $BD: begin tmps:='RS485 communication failure recovery'; end;
          $8E: begin tmps:='Output failure' ; end;
            $BE: begin tmps:='Output failure recovery'; end;
          $8F: begin tmps:='Precharge failure'  ; end;
            $BF: begin tmps:='Precharge failure recovery'     ; end;

          $90: begin tmps:='DC/DC over temperature' ; end;
            $C0: begin tmps:='DC/DC over temperature recovery'   ; end;
          $91: begin tmps:='Accelerating overcurrent'   ; end;
            $C1: begin tmps:='Accelerating overcurrent recovery'     ; end;
          $92: begin tmps:='Accelerating overvoltage'  ; end;
            $C2: begin tmps:='Accelerating overvoltage recovery'; end;
          $93: begin tmps:='Inverter over temperature' ; end;
            $C3: begin tmps:='Inverter over temperature recovery'; end;
          $94: begin tmps:='Inverter overload'  ; end;
            $C4: begin tmps:='Inverter overload recovery'     ; end;
          $95: begin tmps:='Current detection fault' ; end;
            $C5: begin tmps:='Current detection fault recovery'   ; end;
          $96: begin tmps:='Short circuit to ground'   ; end;
            $C6: begin tmps:='Short circuit to ground recovery'     ; end;
          $97: begin tmps:='Wave-by-wave current limiting fault'  ; end;
            $C7: begin tmps:='Wave-by-wave current limiting fault recovery'; end;
          $98: begin tmps:='Discharge relay failure' ; end;
            $C8: begin tmps:='Discharge relay failure recovery'; end;
          $99: begin tmps:='Main relay failure'  ; end;
            $C9: begin tmps:='Main relay failure recovery'     ; end;
          end;
          with recsl do
          begin
            tmps1:=IntToStr(2000 (recbuf[4] shr 2)) '-';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[4] shl 2)or(recbuf[3] shr 6))and $0F)]), ' ', '0') '-';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[((recbuf[3] shr 1)and $1F)]),' ','0') ' ';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[3] shl 4)or(recbuf[2] shr 4))and $1F)]),' ','0') ':';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[2] shl 2)or(recbuf[1] shr 6))and $3F)]),' ','0') ':';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(recbuf[1] and $3F)]),' ','0');
            tmps:=#39 tmps1 ',' tmps;
            tmps1:=Format('%4.1fV',[(recbuf[9] recbuf[10]*$100)/10]); //Vin
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[11] recbuf[12]*$100)/10]); //Boost_Vin
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[13] recbuf[14]*$100)/10]);//Boost_Iin1
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[15] recbuf[16]*$100)/10]);//Boost_Iin2
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[17] recbuf[18]*$100)/10]);//Boost_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[19] recbuf[20]*$100)/10]); //LLC_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[21] recbuf[22]*$100)/10]); //LLc_Iout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[23] recbuf[24]*$100)/10]);//Inverter_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[25] recbuf[26]*$100)/10]);//Inverter_Iout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fHz',[(recbuf[27] recbuf[28]*$100)/10]);//Inverter_Hz
            tmps:=tmps ',' tmps1;
            tmps1:=IntToStr(Smallint(recbuf[29] recbuf[30]*$100)) '℃';  //散热器
            tmps:=tmps ',' tmps1;
            tmps1:=IntToStr(Smallint(recbuf[31] recbuf[32]*$100)) '℃'; //逆变器
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%d%%',[(recbuf[33] recbuf[34]*$100)]);//风扇
            tmps:=tmps ',' tmps1;
            tmps1:=UIntToStr(recbuf[35]);    //SysStatusSignal
            tmps:=tmps ',' tmps1;
//            tmps1:='V' UIntToStr(recbuf[37] /10);    //0.1 DC/DC software version
            tmps1:='V' Format('%4.1f',[(recbuf[37] recbuf[38]*$100)/10]);//
            tmps:=tmps ',' tmps1;
//            tmps1:='V' UIntToStr(recbuf[39] /10);    //0.1 DC/AC software version
            tmps1:='V' Format('%4.1f',[(recbuf[39] recbuf[40]*$100)/10]);//
            tmps:=tmps ',' tmps1;
            Add(tmps);  //换行(注意:换行用Add,换列用,)
          end;
        end;
        recfile:=ExtractFilePath(Application.ExeName) 'Rec' FormatDateTime('YYYYMMDDHHNNSS',Now()) '.csv';
        recsl.SaveToFile(recfile);
        recsl.Free;
//        Application.MessageBox(PChar('记录文件下载成功,保存为' recfile ',请用Excel打开查看。'), '下载成功', MB_OK
//          MB_ICONINFORMATION);
        Application.MessageBox(PChar('The log file was successfully downloaded and saved as' recfile ',Please open it in Excel.'), 'download successful', MB_OK
          MB_ICONINFORMATION);

      end;
      recmem.SaveToFile(ExtractFilePath(Application.ExeName) 'recfile.bin');
      recmem.Free;
    end
//    else Application.MessageBox('没有故障记录!', '提示', MB_OK MB_ICONINFORMATION);
    else Application.MessageBox('No fault record!', 'Prompt', MB_OK MB_ICONINFORMATION);

实例下载地址

delphi串口通讯

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警