unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Button_Exit: TButton;
    Button_Send: TButton;
    Button_Active: TButton;
    RadioButton_11Bit: TRadioButton;
    RadioButton_29Bit: TRadioButton;
    CheckBox_RTR: TCheckBox;
    Edit_D0: TEdit;
    Edit_D1: TEdit;
    Edit_D2: TEdit;
    Edit_D3: TEdit;
    Edit_D4: TEdit;
    Edit_D5: TEdit;
    Edit_D6: TEdit;
    Edit_D7: TEdit;
    ComboBox_BoardNo: TComboBox;
    ComboBox_BaudRate: TComboBox;
    ComboBox_DataLen: TComboBox;
    Edit_MsgID: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button_ActiveClick(Sender: TObject);
    procedure Button_SendClick(Sender: TObject);
    procedure Button_ExitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses CM100;

var
 SelectedBoardNo:BYTE;
 IsActiveFlag:bool=false;
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
   ComboBox_BoardNo.ItemIndex := 0;
   ComboBox_BaudRate.ItemIndex := 0;
   ComboBox_DataLen.ItemIndex := 8;
end;

procedure TForm1.Button_ActiveClick(Sender: TObject);
var
   BoardNo,Baud: BYTE;
   Ret: Integer;
   Para: ConfigStruct;
   TitleStr: AnsiString;
   BaudMap: array[0..7] of WORD;
begin
   BaudMap[0] := 10;
   BaudMap[1] := 20;
   BaudMap[2] := 50;
   BaudMap[3] := 125;
   BaudMap[4] := 250;
   BaudMap[5] := 500;
   BaudMap[6] := 800;
   BaudMap[7] := 1000;

   BoardNo:=ComboBox_BoardNo.ItemIndex;
   Baud:=ComboBox_BaudRate.ItemIndex;
   if (IsActiveFlag) then //If board is active before, close it first.
     begin
	CM100_CloseBoard(SelectedBoardNo);
	IsActiveFlag:=false;
     end;

   Ret := CM100_ActiveBoard(BoardNo); //Active board
   if (Ret <> 0) then
     begin
        ShowMessage('Active Board Error!');
        Form1.Caption := 'TransmitMsg --- No board is active';
        exit;
     end;

   Ret := CM100_Init(BoardNo,1);  //Init board
   if (Ret <> 0) then
     begin
        ShowMessage('Init Board Error!');
        CM100_CloseBoard(BoardNo);
        Form1.Caption := 'TransmitMsg --- No board is active';
        exit;
     end;

   Para.AccMask[0]:=$ff; //Set AccMask to 0xffffffff to pass all CAN messages from CAN bus
   Para.AccMask[1]:=$ff;
   Para.AccMask[2]:=$ff;
   Para.AccMask[3]:=$ff;
   Para.AccCode[0]:=$00; //AccCode is useless when AccMask is 0xffffffff
   Para.AccCode[1]:=$00;
   Para.AccCode[2]:=$00;
   Para.AccCode[3]:=$00;
   Para.BaudRate:=Baud+1; //Baud=0 for user-defined baud, 1:10K, 2:20K, 3:50K, 4:125K, 5:250K, 6:500K, 7:800K, 8:1M bps
   Para.BT0:=0; //BT0 and BT1 are only useful when user-defined baud is used.
   Para.BT1:=0;
   Ret := CM100_Config(BoardNo,1,Para); //Configure board parameters
   if (Ret <> 0) then
     begin
        ShowMessage('Configure Board Error!');
        CM100_CloseBoard(BoardNo);
        Form1.Caption := 'TransmitMsg --- No board is active';
        exit;
     end;

   SelectedBoardNo := BoardNo;
   TitleStr := 'TransmitMsg --- Board ' + IntToStr(BoardNo) + ' is active with baud ' + IntToStr(BaudMap[Baud]) + ' K pbs';
   Form1.Caption := TitleStr;
   IsActiveFlag := true;
end;

procedure TForm1.Button_SendClick(Sender: TObject);
var
   i,Ret:integer;
   TempValue:DWORD;
   TempStr,Str,ErrStr:AnsiString;
   CANMsg:PacketStruct;
begin
   TempStr := Edit_MsgID.Text; //Get message id parameter
   TempValue := StrToInt('$'+ TempStr);
   if (RadioButton_11Bit.Checked) then  //Check the CAN message mode
     begin
        CANMsg.mode := 0;
        CANMsg.id := TempValue and $7ff;
     end
   else
     begin
        CANMsg.mode := 1;
        CANMsg.id := TempValue and $1fffffff;
     end;

   TempStr := IntToHex(CANMsg.id,0);
   Edit_MsgID.Text := TempStr; //Set message id parameter back to Edit box

   if (CheckBox_RTR.Checked) then CANMsg.rtr := 1 //Check if the RTR is used or not
   else CANMsg.rtr := 0;

   CANMsg.len := ComboBox_DataLen.ItemIndex;  //Get data length parameter

   for i:=0 to 7 do
     begin
      Str := 'Edit_D' + IntToStr(i);
      TempStr := (TEdit(FindComponent(Str))).Text; //Get data parameters
      TempValue := StrToInt('$'+ TempStr);
      CANMsg.data[i] := BYTE((TempValue and $ff));
      TempStr := IntToHex(CANMsg.data[i],2);
      (TEdit(FindComponent(Str))).Text := TempStr; //Set data parameters back to Edit box
     end;

   Ret := CM100_SendMsg(SelectedBoardNo,1,CANMsg); //Send CAN message to CAN bus
   if (Ret <> 0) then
     begin
        ErrStr := 'Send Message Error, Error Code ' + IntToStr(Ret);
        ShowMessage(ErrStr);
     end;
end;

procedure TForm1.Button_ExitClick(Sender: TObject);
begin
   if (IsActiveFlag) then CM100_CloseBoard(SelectedBoardNo);
   Close();
end;

end.
