⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom_form.~pas

📁 AVR开发板电路图/PCB/示范程序
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
        for i := 1 to FBufIdx - 3 do
          if (Buf2[i - 1] < #32) and not (Buf2[i - 1] in [#10, #13]) then
            Buf2[i - 1] := #32;
        Buf2[FBufIdx - 3] := #0;
        Memo1.Lines.Text := StrPas(Buf2);
        FBufIdx := 0;
        FillMemory(FBuffer, 4096, 0);
      finally
        FreeMem(Buf2);
      end;
    end
  else if (FBuffer[1] = CMDREAD) then
    begin
      GetMem(Buf2, FBufIdx - 2);
      SetLength(s, 2);
      try
        CopyMemory(Buf2, @FBuffer[2], FBufIdx - 3);
        Buf2[FBufIdx - 3] := #0;
        //Memo1.Lines.Add(StrPas(Buf2));
        if cdsAVR.ChangeCount > 0 then cdsAVR.CancelUpdates;
        pos := 1;
        Pos2 := 0;
        prs.Max := 256;
        prs.Min := 0;
        prs.Position := 0;
        prs.Visible := True;
        cdsAVR.FieldByName('FI').ReadOnly := False;
        for i := 1 to FBufIdx - 3 do
          begin
            if (Buf2[i - 1] <> #32) then
              begin
                if (pos <= 2) then s[Pos] := Buf2[i - 1];
                Inc(Pos);
              end
            else
              begin
                Pos := 1;
                if (pos2 = 0) then
                  cdsAVR.Append;
                cdsAVR['F' + Convert[pos2]] := s;
                Inc(pos2);
                prs.StepIt;
                if pos2 >= 16 then
                  begin
                    if cdsAVR.RecordCount < 16 then
                      cdsAVR['FI'] := Convert[cdsAVR.RecordCount] + '0:' + Convert[cdsAVR.RecordCount] + Convert[pos2 - 1];
                    Pos2 := 0;
                    cdsAVR.Post;
                  end;
              end;
          end;
        if cdsAVR.Modified then
          begin
            if cdsAVR.RecordCount < 16 then
              cdsAVR['FI'] := Convert[cdsAVR.RecordCount] + '0:' + Convert[cdsAVR.RecordCount] + Convert[pos2 - 1];
            cdsAVR.Post;
          end;
        cdsAVR.First;
        cdsAVR.FieldByName('FI').ReadOnly := True;
      finally
        FreeMem(Buf2);
        prs.Visible := False;
      end;
    end
  else if (FBuffer[1] = CMDWRITE) then
    begin
      CopyMemory(VaildWrite, @FBuffer[2], 2);
      WriteResult := True;
    end;
end;

procedure TForm1.actExitExecute(Sender: TObject);
begin
  Close;
end;

{ TWriteCommThread }

procedure TWriteCommThread.Execute;
var
  Buffer: PChar;
  Buf2: array[1..2] of Byte;
  s: string;
  Pos: integer;
  wait: integer;
begin
  with Form1 do
    if cdsAVR.Active and (cdsAVR.RecordCount > 0) then
      begin
        if cdsAVR.Modified then cdsAVR.Post;
        cdsAVR.First;
        Pos := 0;
        GetMem(Buffer, 32);
        cdsAVR.DisableControls;
        try
          //SetLength(s, 2);
          prs.Max := cdsAVR.RecordCount * 16;
          prs.Min := 0;
          prs.Position := 0;
          prs.Visible := True;
          Buffer[0] := CMDBEGIN;
          Buffer[1] := CMDWRITE;
          if opnAddr1.Checked then
            Buf2[1] := 1
          else
            Buf2[1] := 2;
          Buf2[2] := 0;

          if wa0.Checked then Buf2[2] := Buf2[2] + 2;
          if wa1.Checked then Buf2[2] := Buf2[2] + 4;
          if wa2.Checked then Buf2[2] := Buf2[2] + 8;
          BinToHex(@Buf2[1], @Buffer[2], 2); //Memory Address Type and Chip Address

          while not cdsAVR.Eof and (cdsAVR.RecNo <= 16) and not Self.Terminated and not Application.Terminated do
            begin
              if not cdsAVR.FieldByName('F' + Convert[Pos]).IsNull and
                (Length(cdsAVR.FieldByName('F' + Convert[Pos]).AsString) = 2) then
                begin
                  s := UpperCase(cdsAVR.FieldByName('F' + Convert[Pos]).AsString);
                  if not (s[1] in ['0'..'9', 'A'..'F']) then s[1] := '0';
                  if not (s[2] in ['0'..'9', 'A'..'F']) then s[2] := '0';

                  //Memory Address
                  Buffer[6] := '0';
                  Buffer[7] := '0';
                  Buffer[8] := Convert[cdsAVR.Recno - 1];
                  Buffer[9] := Convert[Pos];

                  //Memory Data
                  Buffer[10] := s[1];
                  Buffer[11] := s[2];

                  Buffer[12] := CMDEND;

                  WriteResult := False;
                  FillMemory(VaildWrite, 3, 0);
                  wait := 0;
                  WritePort(Buffer, 13);
                  while (wait < 2000) and (not WriteResult) do
                    begin
                      Inc(wait);
                      Sleep(100);
                    end;
                  if wait >= 2000 then
                    begin
                      Memo1.Lines.Add('Write EEPROM Error(Time out).');
                      Break;
                    end;
                  if WriteResult and (s[1] <> VaildWrite[0]) and (s[2] <> VaildWrite[1]) then
                    begin
                      Buffer[12] := #0;
                      Memo1.Lines.Add('Write EEPROM Error(Vaild Check Data:' + StrPas(@Buffer[2]) + ',S:' + s + ',Result:' + StrPas(VaildWrite) + ')');
                    end;
                end;
              Inc(Pos);
              prs.StepIt;
              if Pos >= 16 then
                begin
                  Pos := 0;
                  cdsAVR.Next;
                end;
            end;
          cdsAVR.First;
        finally
          FreeMem(Buffer);
          cdsAVR.EnableControls;
          prs.Visible := False;
        end;
      end;
end;

procedure TForm1.actWriteExecute(Sender: TObject);
begin
  TWriteCommThread.Create(False);
end;

procedure TForm1.actOpenExecute(Sender: TObject);
var i: integer;
  Field: TField;
begin
  if OpenDialog1.Execute then
    begin
      cdsImport.LoadFromFile(OpenDialog1.FileName);
      cdsImport.Open;
      cdsImport.First;
      if (cdsAVR.ChangeCount > 0) then cdsAVR.CancelUpdates;
      cdsAVRFI.ReadOnly := False;
      while not cdsImport.Eof do
        begin
          cdsAVR.Append;
          for i := 1 to cdsAVR.FieldCount do
            begin
              Field := cdsImport.FindField(cdsAVR.Fields[i - 1].FieldName);
              if (Field <> nil) and cdsAVR.Fields[i - 1].CanModify then
                begin
                  cdsAVR.Fields[i - 1].Value := Field.Value;
                end;
            end;
          cdsAVR.Post;
          cdsImport.Next;
        end;
      cdsImport.Close;
      cdsAVRFI.ReadOnly := True;
      cdsAVR.First;
      //cdsImport.EmptyDataSet;
    end;
end;

procedure TForm1.actSaveExecute(Sender: TObject);
begin
  if cdsAVR.Active and SaveDialog1.Execute then
    begin
      cdsAVR.SaveToFile(SaveDialog1.FileName);
    end;
end;

procedure TWriteCommThread.SetAddrType(const Value: integer);
begin
  FAddrType := Value;
end;

procedure TForm1.cdsAVRBeforePost(DataSet: TDataSet);
var
  i: integer;
  s: string;
begin
  for i := 1 to 16 do
    begin
      s := cdsAVR.FieldByName('F' + Convert[i - 1]).AsString;
      if s = '' then s := '00';
      if (s[1] in ['a'..'f']) then s[1] := Char(Byte(s[1]) - 32);
      if (s[2] in ['a'..'f']) then s[2] := Char(Byte(s[2]) - 32);
      if not (s[1] in ['0'..'9', 'A'..'F']) then s[1] := '0';
      if not (s[2] in ['0'..'9', 'A'..'F']) then s[2] := '0';
      if s <> cdsAVR.FieldByName('F' + Convert[i - 1]).AsString then
        begin
          //if not (cdsAVR.State in [dsEdit, dsInsert]) then cdsAVR.Edit;
          cdsAVR.FieldByName('F' + Convert[i - 1]).AsString := s;
        end;
    end;

end;

procedure TForm1.WritePort(Buffer: Pointer; Count: Integer);
begin
  ComPort1.Write(Buffer^, Count);
end;

procedure TForm1.OpenPort;
begin
  ComPort1.Open;
end;

procedure TForm1.ClosePort;
begin
  ComPort1.Close;
end;

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var i: integer;
begin
  if Count <= 0 then exit;
  if Count < 4096 then
    begin
      ComPort1.Read(SBuffer^, Count);
      PressBuffer(SBuffer, Count);
    end
  else
    begin
      i := Count;
      while i > 0 do
        begin
          ComPort1.Read(SBuffer^, 4096);
          PressBuffer(SBuffer, 4096);
          i := i - 4096;
        end;
    end;
end;

procedure TForm1.btnUpdateClick(Sender: TObject);
begin
  cmbPort.ApplySettings;
  cmbPort.UpdateSettings;
  cmbRate.ApplySettings;
  cmbData.ApplySettings;
  cmbFlow.ApplySettings;
  cmbParity.ApplySettings;
  cmbStop.ApplySettings;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -