Jeden z našich zákazníků potřeboval před nedávnem pracovat s převodníkem USB2CAN Triple z Pascalu. Abychom mu dokázali že není problém používat X2CAN API napsané v C/C++, vytvořil jsem tento ukázkový kód. Ač jsem programoval v Pascalu naposledy na VŠ před rokem 2000, podařilo se tento funkční kód vytvořit během 3 hodin včetně instalace FreePascalu. Jelikož jsou moje znalosti Pascalu nulové, na případné chyby mne prosím upozorněte.
Ukázkový kód inicializuje převodník USB2CAN Triple a odešle 1 zprávu na CAN sběrnici. Ve funkci TRIPLE_PrepareAdapter je případně třeba upravit číslo COM portu.
David Španěl
program test_usb2can_triple; uses DynLibs,Crt; type CANMessageTime = record Hour: UInt16; Minute: UInt16; Second: UInt16; Milliseconds: UInt16; TimeStampUS: UInt32; end; type CAN_MESSAGE = record Id1: UInt16; Id2: UInt32; Id: UInt32; length: UInt8; (*to je vzdy delka, ne DLC*) rtr: Boolean; st_ext: Boolean; fd_can: Boolean; fd_esi: Boolean; fd_br_switch: Boolean; (*baud rate switch*) fd_sid11: Boolean; data: array [0..Pred(8)] of UInt8; fd_data: ^UInt8; time: CANMessageTime; port: UInt32; dont_delete: Boolean; end; var Hbar_X2CAN: Thandle; var Hbar_Base: Thandle; var adapter: IntPtr; var IsOK: Boolean; var msg: CAN_MESSAGE; var ret: Boolean; var RXLen: UInt32; function USB2CAN_TRIPLE_PrepareAdapter(port_number : UInt32; combo: Boolean ) :IntPtr; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_PrepareAdapter'; function USB2CAN_TRIPLE_Open(speed : Integer; errFnc : IntPtr) : Boolean; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_Open'; procedure USB2CAN_TRIPLE_Close () ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_Close'; procedure USB2CAN_TRIPLE_SelectActualAdapter (adapter : IntPtr) ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_SelectActualAdapter'; function USB2CAN_TRIPLE_SendCANMessage(var message: CAN_MESSAGE): Boolean ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_SendCANMessage'; procedure CANMsgUpdateFrom29 (message : Pointer) ; cdecl; external 'X2CAN_3th_dll.dll' name 'CANMsgUpdateFrom29'; function USB2CAN_TRIPLE_GetCANMessage(message: Pointer): Boolean ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_GetCANMessage'; procedure USB2CAN_TRIPLE_PrepareAdapterSpeedC1 (adapter : IntPtr; speed: UInt32; liso: Boolean) ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_PrepareAdapterSpeedC1'; procedure USB2CAN_TRIPLE_PrepareAdapterSpeedC2 (adapter : IntPtr; speed: UInt32; liso: Boolean) ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_PrepareAdapterSpeedC2'; procedure USB2CAN_TRIPLE_PrepareAdapterSpeedC3 (adapter : IntPtr; speed: Uint32; liso: Boolean; esi: Boolean; iso_crt: Boolean) ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_PrepareAdapterSpeedC3'; function USB2CAN_TRIPLE_GetRXBufferLength(): UInt32 ; cdecl; external 'X2CAN_3th_dll.dll' name 'USB2CAN_TRIPLE_GetRXBufferLength'; begin IsOk := false; Hbar_Base := LoadLibrary('BaseW32_dll.dll'); Hbar_X2CAN := LoadLibrary('X2CAN_3th_dll.dll'); if(Hbar_Base >=32) then writeln('Hbar_Base: OK') else writeln('Hbar_Base: ERROR'); if(Hbar_X2CAN >=32) then writeln('Hbar_X2CAN: OK') else writeln('Hbar_X2CAN: ERROR'); adapter := USB2CAN_TRIPLE_PrepareAdapter(12,false); USB2CAN_TRIPLE_SelectActualAdapter(adapter); USB2CAN_TRIPLE_PrepareAdapterSpeedC1(adapter,250,false); USB2CAN_TRIPLE_PrepareAdapterSpeedC2(adapter,250,false); USB2CAN_TRIPLE_PrepareAdapterSpeedC3(adapter,2501000,false,false,true); IsOK := USB2CAN_TRIPLE_Open(250,0); if(IsOK=true) then writeln('USB2CAN Open: OK') else writeln('USB2CAN Open: ERROR'); msg.Id := $0CF00401; msg.Id1 := 10; msg.Id2 := 0; msg.port:= 1; msg.fd_can := false; msg.rtr := false; msg.st_ext := true; msg.fd_data := nil; msg.length:=8; msg.data[1] := 123; CANMsgUpdateFrom29(@msg); ret := USB2CAN_TRIPLE_SendCANMessage(msg); if(ret=true) then writeln('USB2CAN Send: OK') else writeln('USB2CAN Send: ERROR'); while true do begin RXLen := USB2CAN_TRIPLE_GetRXBufferLength(); writeln(RXLen); if(RXLen>0) then begin ret := USB2CAN_TRIPLE_GetCANMessage(@msg); writeln(msg.Id); end; Delay(100); end; USB2CAN_TRIPLE_Close(); FreeLibrary(Hbar_X2CAN); FreeLibrary(Hbar_Base); end.