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

📄 mainformunit.pas

📁 USB技术大全-54.8M.zip
💻 PAS
📖 第 1 页 / 共 2 页
字号:


procedure TMainForm.DataInTimerTimer(Sender: TObject);
var
  InByte,InByteB,InByteC,InByteD:byte;
  UsedPorts:byte;
  i:integer;
  MyComponent:TComponent;
begin
  DataInTimer.Tag:=1;
  DataInIntervalLabel.Caption:=IntToStr(DataInTimer.Interval)+'ms';
  if DoGetInDataPorts(InByteB,InByteC,InByteD,UsedPorts)<>NO_ERROR then
    begin
      DeviceNotPresentLabel.Visible:=true;
      DataInTimer.Tag:=0;
      Exit;
    end;
  if UsedPorts<=1 then PortChoiceRadioGroup.ItemIndex:=0;
  case PortChoiceRadioGroup.ItemIndex of
    1:InByte:=InByteC;
    2:InByte:=InByteD;
  else
    InByte:=InByteB;
  end;
  DeviceNotPresentLabel.Visible:=false;
  for i:= 0 to 7 do
    begin
      MyComponent:=FindComponent('DataInCheckBox'+IntToStr(i));
      (MyComponent as TCheckBox).Checked:=((MyComponent as TCheckBox).Tag and InByte)<>0;
    end;

  if DoGetOutDataPorts(InByteB,InByteC,InByteD,UsedPorts)<>NO_ERROR then
    begin
      DeviceNotPresentLabel.Visible:=true;
      DataInTimer.Tag:=0;
      Exit;
    end;
  if UsedPorts<=1 then PortChoiceRadioGroup.ItemIndex:=0;
  case PortChoiceRadioGroup.ItemIndex of
    1:InByte:=InByteC;
    2:InByte:=InByteD;
  else
    InByte:=InByteB;
  end;
  DeviceNotPresentLabel.Visible:=false;
  for i:= 0 to 7 do
    begin
      MyComponent:=FindComponent('DataOutCheckBox'+IntToStr(i));
      (MyComponent as TCheckBox).Checked:=((MyComponent as TCheckBox).Tag and InByte)<>0;
    end;

  if DoGetDataPortDirections(InByteB,InByteC,InByteD,UsedPorts)<>NO_ERROR then
    begin
      DeviceNotPresentLabel.Visible:=true;
      DataInTimer.Tag:=0;
      Exit;
    end;
  if UsedPorts<=1 then PortChoiceRadioGroup.ItemIndex:=0;
  case PortChoiceRadioGroup.ItemIndex of
    1:InByte:=InByteC;
    2:InByte:=InByteD;
  else
    InByte:=InByteB;
  end;
  DeviceNotPresentLabel.Visible:=false;
  for i:= 0 to 7 do
    begin
      MyComponent:=FindComponent('DataDirectionCheckBox'+IntToStr(i));
      (MyComponent as TCheckBox).Checked:=((MyComponent as TCheckBox).Tag and InByte)<>0;
    end;
  DataInTimer.Tag:=0;
end;


procedure TMainForm.EEPROMReadButtonClick(Sender: TObject);
var
  i:integer;
  DataByte:byte;
begin
  EEPROMStringGrid.Col:=1;
  for i:=0 to EEPROMStringGrid.RowCount-1 do
    begin
      if DoEEPROMRead(i,DataByte)=0 then
        begin
          EEPROMStringGrid.Cells[1,i]:=IntToStr(DataByte);
          EEPROMStringGrid.Row:=i;
        end
      else
        begin
          raise Exception.Create('Unable to read from EEPROM!'+#13+#10+'(maybe device is not present)');
        end;
    end;
end;

procedure TMainForm.EEPROMWriteButtonClick(Sender: TObject);
var
  i:integer;
  DataByte:integer;
begin
  EEPROMStringGrid.Col:=1;
  try
    for i:=0 to EEPROMStringGrid.RowCount-1 do
      begin
        EEPROMStringGrid.Row:=i;
        DataByte:=StrToInt(EEPROMStringGrid.Cells[1,i]);
        if (DataByte>255)or(DataByte<0) then
          raise Exception.Create('Number is not in BYTE range: 0-255 !');
        if DoEEPROMWrite(i,DataByte)<>0 then
          begin
            raise Exception.Create('Error to write to EEPROM!'+#13+#10+'(maybe device is not present)');
          end;
      end;
  except on EConvertError do
    begin
      ActiveControl:=EEPROMStringGrid;
      MessageBox(Handle,'Invalid BYTE number'+#13+#10+'(unable to convert to numeric value)',PChar(Caption),MB_ICONERROR);
    end
  else
    begin
      ActiveControl:=EEPROMStringGrid;
      raise;
    end;
  end;
end;


procedure TMainForm.RS232SendButtonClick(Sender: TObject);
type
  MyBuffer=array [0..100000] of byte;
var
  j,i:integer;
  P:MyBuffer;
  MyStr:string;
begin
  MyStr:=TerminalMemo.Text;
  i:=Length(MyStr);
  Screen.Cursor:=crHourGlass;
  TransmittingLabel.Visible:=true;
  TransmittingLabel.Repaint;
  try
    for j:=1 to i do
      begin
        P[j-1]:=byte(MyStr[j]);
      end;
    DoRS232BufferSend(P, i);
  finally
    Screen.Cursor:=crArrow;
    TransmittingLabel.Visible:=false;
    TransmittingLabel.Repaint;
  end;
end;


procedure TMainForm.RS232BaudrateSpinEditChange(Sender: TObject);
var
  RetVal:integer;
begin
  try
    RetVal:=DoSetRS232Baud(RS232BaudrateSpinEdit.Value);
    RS232BaudrateSpinEdit.Color:=clWindow;
    RS232BaudrateSpinEdit.Font.Style:=[];
    case RetVal of
      DEVICE_NOT_PRESENT:RS232BaudrateSpinEdit.Color:=clRed;
      INVALID_BAUDRATE: RS232BaudrateSpinEdit.Font.Style:=[fsStrikeOut];
    end;
  except
  end;
end;

procedure TMainForm.RS232ReadTimerTimer(Sender: TObject);
var
  BaudRate:integer;
  Databits:byte;
  Parity:byte;
  Stopbits:byte;
begin
  RS232ReadIntervalLabel.Caption:=IntToStr(RS232ReadTimer.Interval)+'ms';
  if DoGetRS232Baud(BaudRate)=NO_ERROR then
    begin
      RS232ReadBaudLabel.Caption:='='+IntToStr(BaudRate);
      DeviceNotPresentLabel.Visible:=false;
    end
  else
    begin
      DeviceNotPresentLabel.Visible:=true;
    end;
  if not DataBitsComboBox.DroppedDown then
    if DoGetRS232DataBits(Databits)=NO_ERROR then
      begin
        DataBitsComboBox.ItemIndex:=Databits-5;
        DeviceNotPresentLabel.Visible:=false;
      end
    else
      begin
        //DeviceNotPresentLabel.Visible:=true;
      end;
  if not ParityComboBox.DroppedDown then
    if DoGetRS232Parity(Parity)=NO_ERROR then
      begin
        ParityComboBox.ItemIndex:=Parity;
        DeviceNotPresentLabel.Visible:=false;
      end
    else
      begin
        //DeviceNotPresentLabel.Visible:=true;
      end;
  if not StopBitsComboBox.DroppedDown then
    if DoGetRS232StopBits(Stopbits)=NO_ERROR then
      begin
        StopBitsComboBox.ItemIndex:=Stopbits;
        DeviceNotPresentLabel.Visible:=false;
      end
    else
      begin
        //DeviceNotPresentLabel.Visible:=true;
      end;
end;

procedure TMainForm.CopyRightLabelClick(Sender: TObject);
begin
  ShellExecute(Handle,'open','http://www.cesko.host.sk',nil,'.',0);
  ShellExecute(Handle,'open','http://www.atmel.com',nil,'.',0);
end;

procedure TMainForm.RS232BufferTimerTimer(Sender: TObject);
var
  DataLength:integer;
  i:integer;
begin
  DataLength:=SizeOf(InputRS232Data);
  if (DoGetRS232Buffer(InputRS232Data,DataLength)<>NO_ERROR) then
    begin
      //DeviceNotPresentLabel.Visible:=true;
      Exit;
    end;
  if (DataLength=0) then Exit;
  Screen.Cursor:=crAppStart;
  ReceivingLabel.Visible:=true;
  ReceivingLabel.Repaint;
  try
    TerminalMemo.SelStart:=Length(TerminalMemo.Text);
    TerminalMemo.SelLength:=0;
    for i:=0 to DataLength-1 do
      begin
        RS232BufferMemo.Lines.Add(IntToStr(InputRS232Data[i])+' -> '+IntToHex(InputRS232Data[i],2)+' -> '+chr(InputRS232Data[i]));
        TerminalMemo.SelText:=chr(InputRS232Data[i]);
      end;
  finally
    Screen.Cursor:=crArrow;
    ReceivingLabel.Visible:=false;
    ReceivingLabel.Repaint;
  end;
end;

procedure TMainForm.RS232SendEditKeyPress(Sender: TObject; var Key: Char);
begin
  DoRS232Send(byte(Key));
end;

procedure TMainForm.DataBitsComboBoxChange(Sender: TObject);
var
  RetVal:integer;
begin
  RetVal:=DoSetRS232DataBits(StrToInt(DataBitsComboBox.Text));
  DataBitsComboBox.Color:=clWindow;
  DataBitsComboBox.Font.Style:=[];
  case RetVal of
    DEVICE_NOT_PRESENT: DataBitsComboBox.Color:=clRed;
    INVALID_DATABITS: DataBitsComboBox.Font.Style:=[fsStrikeOut];
  end;
end;

procedure TMainForm.ParityComboBoxChange(Sender: TObject);
var
  RetVal:integer;
begin
  RetVal:=DoSetRS232Parity(ParityComboBox.ItemIndex);
  ParityComboBox.Color:=clWindow;
  ParityComboBox.Font.Style:=[];
  case RetVal of
    DEVICE_NOT_PRESENT: ParityComboBox.Color:=clRed;
    INVALID_PARITY: ParityComboBox.Font.Style:=[fsStrikeOut];
  end;
end;

procedure TMainForm.StopBitsComboBoxChange(Sender: TObject);
var
  RetVal:integer;
begin
  RetVal:=DoSetRS232StopBits(StopBitsComboBox.ItemIndex);
  StopBitsComboBox.Color:=clWindow;
  StopBitsComboBox.Font.Style:=[];
  case RetVal of
    DEVICE_NOT_PRESENT: StopBitsComboBox.Color:=clRed;
    INVALID_STOPBITS: StopBitsComboBox.Font.Style:=[fsStrikeOut];
  end;
end;


procedure TMainForm.FormResize(Sender: TObject);
begin
  TerminalMemo.Width:= ClientWidth-EEPROMStringGrid.Width-TerminalMemo.Left;
  EEPROMStringGrid.Left:=ClientWidth-EEPROMStringGrid.Width;
  TerminalMemo.Width:= EEPROMStringGrid.Left-TerminalMemo.Left;
  TerminalMemo.Height:= ClientHeight-TerminalMemo.Top;
  RS232BufferMemo.Height:= ClientHeight-RS232BufferMemo.Top;
end;





procedure TMainForm.EEPROMSizeSpinEditChange(Sender: TObject);
var
  i:integer;
begin
  EEPROMStringGrid.RowCount:=EEPROMSizeSpinEdit.Value;
  for i:=0 to EEPROMStringGrid.RowCount-1 do
    EEPROMStringGrid.Cells[0,i]:=IntToStr(i);  
end;


procedure TMainForm.PortChoiceRadioGroupClick(Sender: TObject);
begin
  DataInTimerTimer(self);
end;


end.

⌨️ 快捷键说明

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