unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label2: TLabel;
    Label1: TLabel;
    ComboBox_BoardNo: TComboBox;
    ComboBox_BaudRate: TComboBox;
    Label3: TLabel;
    Button_ClearStatus: TButton;
    Button_ClearList: TButton;
    Button_Receive: TButton;
    Button_Exit: TButton;
    Edit_Status: TEdit;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button_ClearStatusClick(Sender: TObject);
    procedure Button_ClearListClick(Sender: TObject);
    procedure Button_ExitClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button_ReceiveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses CM100;

var
 SelectedBoardNo:BYTE;
 IsActiveFlag:bool=false;
 MaxListCnt:BYTE;
{$R *.DFM}
//---------------------------------------------
function Int64ToUnInt64(InputValue:Int64):double;
var
  TempValue,TempScale:double;
begin
  TempScale:=$7FFFFFFFFFFFFFFF;
  TempValue:= InputValue;
  If (TempValue < 0) Then
      TempValue := 2 * (TempScale + 1) + TempValue;

  Int64ToUnInt64 := TempValue;
end;
//---------------------------------------------
procedure TForm1.Button1Click(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
	Timer1.Enabled := false; //Stop timer event
	CM100_CloseBoard(SelectedBoardNo);
	IsActiveFlag:=false;
     end;

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

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

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

procedure TForm1.Button_ClearStatusClick(Sender: TObject);
begin
   CM100_ClearDataOverrun(SelectedBoardNo,1);
end;

procedure TForm1.Button_ClearListClick(Sender: TObject);
begin
   Memo1.Clear();
   MaxListCnt := 0;
end;

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

procedure TForm1.Timer1Timer(Sender: TObject);
var
   CANStatus:BYTE;
   Str:AnsiString;
begin
   CM100_Status(SelectedBoardNo,1,CANStatus); //Get CAN status
   Str := '0x' + IntToHex(CANStatus,2);
   Edit_Status.Text := Str;
end;

procedure TForm1.Button_ReceiveClick(Sender: TObject);
var
   i,j,MsgCnt,Ret:Integer;
   Timestamp:double;
   ListStr,DataStr,ErrStr:AnsiString;
   CANMsg:PacketStruct;
begin
   MsgCnt:=CM100_RxMsgCount(SelectedBoardNo,1); //Check the numbers of received CAN messages
   for i:=1 to MsgCnt do
     begin
   	Ret:=CM100_ReceiveMsg(SelectedBoardNo,1,CANMsg); //Receive CAN message
   	if (Ret <> 0) then
          begin
		
		ErrStr := 'Get Message Error, Error code=' + IntToStr(Ret);
		ShowMessage(ErrStr);
                Break;
          end;
	Timestamp := Int64ToUnInt64(CANMsg.MsgTimeStamps);
	if (CANMsg.rtr <> 0) then // if rtr bit is set, data is useless
          begin
	     ListStr := IntToStr(MaxListCnt) + ' Time=' + FloatToStr(Timestamp/10) + 'ms, Mode:'
                        + IntToStr(CANMsg.mode) + ', ID:' + IntToHex(CANMsg.id,8) + ', RTR:'
                        + IntToStr(CANMsg.rtr) + ', DLen:' + IntToStr(CANMsg.len);

             Memo1.Lines.Add(ListStr); //Show CAN messages
          end
	else
          begin
	     ListStr := IntToStr(MaxListCnt) + ' Time=' + FloatToStr(Timestamp/10) + 'ms, Mode:'
                        + IntToStr(CANMsg.mode) + ', ID:' + IntToHex(CANMsg.id,8) + ', RTR:'
                        + IntToStr(CANMsg.rtr) + ', DLen:' + IntToStr(CANMsg.len);
             if (CANMsg.len  = 0) then ListStr := ListStr + ' [X]'
	     else
               begin
                  ListStr := ListStr + ' [';
                  for j:=0 to CANMsg.len-1 do
                    begin
	     	       if (j < CANMsg.len-1) then
                         begin
                            DataStr := IntToHex(CANMsg.data[j],2) + ',';
                            ListStr := ListStr + DataStr;
                         end
                       else
                         begin
                            DataStr := IntToHex(CANMsg.data[j],2);
                            ListStr := ListStr + DataStr;
                         end;
                    end;
                  ListStr := ListStr + ']';
               end;
             Memo1.Lines.Add(ListStr); //Show CAN messages
          end;
	MaxListCnt := MaxListCnt + 1;
	if (MaxListCnt = 0) then Memo1.Clear(); //If ListBox buffer is full, clean it
     end;
end;

end.
 