Nov 17

In order to develop the higher level RoboCAN protocol I will need two devices that are connected to the CAN bus. One is the ATDVK90CAN1 board and the other one is a PC using the CANUSB interface. When the drivers are installed a series of demo programs is installed as well including the source code. These demo programs show how to interface with a DLL called canusbdrv.dll which provides the basic functions to send and receive frames using the CANUSB interface.

I’m using Delphi 2005 as my development environment for Windows applications. Opening and compiling the SDIAPP stored in the C:\Program Files\LAWICEL\CANUSB\examples\Delphi2005 folder works, however when executing the code it crashes when I press the openbutton. I wonder if it truly is so difficult to build code that works…

Some debugging later I find that the string used for receiving the version is too small for storing the version. Below is the modified code (the bold parts):

procedure TSDIAppForm.BtnOpenClick(Sender: TObject);
Var VerText : String[255];
begin
  CANUSBOpen(”, CAN_BAUD_500K, CANUSB_ACCEPTANCE_CODE_ALL, CANUSB_ACCEPTANCE_MASK_ALL, CANUSB_FLAG_TIMESTAMP);
  CANUSBVersionInfo(@VerText[0]);
  Label1.Caption := VerText;
  BtnOpen.Enabled := False;
  BtnClose.Enabled := True;
  BtnStatus.Enabled := True;
  BtnWrite.Enabled := True;
  Timer1.Enabled := True;
end;

Next to this, I changed the address used for sending frames to $310 so it works with the code running so far on the ATDVK90CAN1 board. Now at least it does not crash, however when pressing the write button I see that a frame is received with 1 data byte (correct) but it shows two… Some more debugging shows that the loop to show the received data contains another bug as well… Below the modified code (the bold parts). 

procedure TSDIAppForm.Timer1Timer(Sender: TObject);
Var
  CANMessage: CANmsg;
  DummyStr: String;
  tmp: Integer;
begin
  if (CANUSBRead(CANMessage) = ERROR_CANUSB_OK) then
   begin
     DummyStr := Format(‘ID = %x,’, [CANMessage.id]);
     DummyStr := DummyStr + Format(‘ LEN = %d, DATA = ‘, [CANMessage.len]);

     tmp := 0;
     while (tmp < CANMessage.len) do
       begin
         DummyStr := DummyStr + Format(‘ %2x’, [CANMessage.data[tmp]]);
         Inc (tmp);
       end;

     Label3.Caption := DummyStr;
   end
end;

procedure TSDIAppForm.BtnWriteClick(Sender: TObject);
Var CANMessage: CANmsg;
begin
  CANMessage.id := $310;
  CANMessage.flags := 0;
  CANMessage.len := 2;
  CANMessage.data[0] := $55;
  CANMessage.data[1] := $AA;
  CANUSBWrite(CANMessage);
end;

At least it seems the ddl is working so I will use this dll to develop a RoboCAN monitor application.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.