创新can调试器Linux版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
7.3 KiB

4 years ago
  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, ControlCAN;
  6. type
  7. TForm1 = class(TForm)
  8. ComboBox1: TComboBox;
  9. ComboBox2: TComboBox;
  10. Button1: TButton;
  11. Button2: TButton;
  12. GroupBox1: TGroupBox;
  13. Label4: TLabel;
  14. Label5: TLabel;
  15. Label6: TLabel;
  16. Label7: TLabel;
  17. Button5: TButton;
  18. GroupBox2: TGroupBox;
  19. Label8: TLabel;
  20. Label9: TLabel;
  21. Button6: TButton;
  22. ComboBox6: TComboBox;
  23. ComboBox7: TComboBox;
  24. Edit1: TEdit;
  25. Edit4: TEdit;
  26. GroupBox6: TGroupBox;
  27. ListBox1: TListBox;
  28. GroupBox3: TGroupBox;
  29. Label1: TLabel;
  30. Edit2: TEdit;
  31. Edit3: TEdit;
  32. Label2: TLabel;
  33. Edit5: TEdit;
  34. Label10: TLabel;
  35. Edit6: TEdit;
  36. Label11: TLabel;
  37. Label12: TLabel;
  38. Label13: TLabel;
  39. ComboBox3: TComboBox;
  40. ComboBox4: TComboBox;
  41. Button3: TButton;
  42. procedure Button1Click(Sender: TObject);
  43. procedure Button2Click(Sender: TObject);
  44. procedure Button5Click(Sender: TObject);
  45. procedure FormCreate(Sender: TObject);
  46. procedure Button6Click(Sender: TObject);
  47. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  48. procedure ComboBox1Click(Sender: TObject);
  49. procedure ComboBox2Click(Sender: TObject);
  50. procedure Button3Click(Sender: TObject);
  51. procedure EnableUI(bEnable: BOOL);
  52. private
  53. { Private declarations }
  54. public
  55. end;
  56. PTListBox=^TListBox;
  57. var
  58. Form1: TForm1;
  59. var
  60. m_devtype : DWORD;
  61. m_devind : DWORD;
  62. m_cannum : DWORD;
  63. m_connect : DWORD;
  64. m_threadhandle : integer;
  65. implementation
  66. {$R *.dfm}
  67. function ReceiveThread(param : Pointer): integer;
  68. var
  69. receivedata : array[0..49] of VCI_CAN_OBJ;
  70. len : integer;
  71. j : integer;
  72. i : integer;
  73. str : AnsiString;
  74. tmpstr :AnsiString;
  75. box : PTListBox;
  76. errinfo : VCI_ERR_INFO;
  77. begin
  78. box:=param;
  79. while TRUE do
  80. begin
  81. if m_connect=0 then
  82. break;
  83. Sleep(1);
  84. len:=VCI_Receive(m_devtype,m_devind,m_cannum,@receivedata[0],50,200);
  85. if len<=0 then
  86. begin
  87. continue;
  88. end;
  89. for i:=0 to len-1 do
  90. begin
  91. str:='���յ�����֡: ';
  92. if receivedata[i].TimeFlag=0 then
  93. tmpstr:='ʱ����ʶ:�� '
  94. else
  95. tmpstr:='ʱ����ʶ:0x'+IntToHex(receivedata[i].TimeStamp,1)+' ';
  96. str:=str+tmpstr;
  97. tmpstr:='֡ID:0x'+IntToHex(receivedata[i].ID,1)+' ';
  98. str:=str+tmpstr;
  99. str:=str+'֡��ʽ:';
  100. if receivedata[i].RemoteFlag=0 then
  101. tmpstr:='����֡ '
  102. else
  103. tmpstr:='Զ��֡ ';
  104. str:=str+tmpstr;
  105. str:=str+'֡����:';
  106. if receivedata[i].ExternFlag=0 then
  107. tmpstr:='��׼֡ '
  108. else
  109. tmpstr:='��չ֡ ';
  110. str:=str+tmpstr;
  111. box.Items.Add(str);
  112. if receivedata[i].RemoteFlag=0 then
  113. begin
  114. str:='����:';
  115. if receivedata[i].DataLen>8 then
  116. receivedata[i].DataLen:=8;
  117. for j:=0 to receivedata[i].DataLen-1 do
  118. begin
  119. tmpstr:=IntToHex(receivedata[i].Data[j],2)+' ';
  120. str:=str+tmpstr;
  121. end;
  122. box.Items.Add(str);
  123. end;
  124. end;
  125. box.ItemIndex:=box.Items.Count-1;
  126. end;
  127. EndThread(0);
  128. ReceiveThread:=0;
  129. end;
  130. procedure TForm1.Button1Click(Sender: TObject);
  131. var
  132. index: integer;
  133. cannum: integer;
  134. threadid: LongWord;
  135. initconfig : VCI_INIT_CONFIG;
  136. begin
  137. index:=m_devind;
  138. cannum:=ComboBox2.ItemIndex;
  139. if m_connect=1 then
  140. begin
  141. m_connect:=0;
  142. Button1.Caption:='����';
  143. VCI_CloseDevice(m_devtype,m_devind);
  144. EnableUI(True);
  145. Exit;
  146. end;
  147. if (index>=0) and (cannum>=0) then
  148. begin
  149. if VCI_OpenDevice(m_devtype,index,0)<>1 then
  150. ShowMessage('�˿ڴ���ʧ��')
  151. else
  152. begin
  153. initconfig.AccCode:=StrToInt('0x'+Edit2.Text);
  154. initconfig.AccMask:=StrToInt('0x'+Edit3.Text);
  155. initconfig.Timing0:=StrToInt('0x'+Edit5.Text);
  156. initconfig.Timing1:=StrToInt('0x'+Edit6.Text);
  157. initconfig.Filter:=ComboBox3.ItemIndex;
  158. initconfig.Mode:=ComboBox4.ItemIndex;
  159. if VCI_InitCAN(m_devtype,m_devind,cannum,@initconfig)<>1 then
  160. begin
  161. Showmessage('��ʼ��CANʧ��');
  162. Exit;
  163. end;
  164. m_devind:=index;
  165. m_cannum:=cannum;
  166. m_connect:=1;
  167. Button1.Caption:='�Ͽ�';
  168. threadid:=111;
  169. m_threadhandle:=BeginThread(0,0,ReceiveThread,@ListBox1,0,threadid);
  170. EnableUI(False);
  171. end;
  172. end
  173. else
  174. ShowMessage('ѡ���Ϊ��');
  175. end;
  176. procedure TForm1.Button2Click(Sender: TObject);
  177. begin
  178. if m_connect=0 then
  179. Exit;
  180. if VCI_ResetCAN(m_devtype,m_devind,m_cannum)<>1 then
  181. ShowMessage('��λCANʧ��')
  182. else
  183. begin
  184. ListBox1.Items.Add('��λCAN�ɹ�');
  185. Button5.Enabled := False;
  186. end
  187. end;
  188. procedure TForm1.FormCreate(Sender: TObject);
  189. begin
  190. m_devtype := 4;//USBCAN2���ͺ�
  191. m_devind := 0;
  192. m_cannum :=0;
  193. m_connect:= 0;
  194. ComboBox1.ItemIndex:=1;
  195. ComboBox2.ItemIndex:=0;
  196. ComboBox3.ItemIndex:=0;
  197. ComboBox4.ItemIndex:=0;
  198. ComboBox6.ItemIndex:=0;
  199. ComboBox7.ItemIndex:=0;
  200. Button5.Enabled := False;
  201. end;
  202. procedure TForm1.Button5Click(Sender: TObject);
  203. var
  204. sendtype,frametype,frameformat : BYTE;
  205. id : DWORD;
  206. data : array[0..7] of BYTE;
  207. str : AnsiString;
  208. strdata : AnsiString;
  209. senddata : VCI_CAN_OBJ;
  210. i : integer;
  211. begin
  212. if m_connect=0 then
  213. Exit;
  214. sendtype:=0;
  215. frametype:=ComboBox6.ItemIndex;
  216. frameformat:=ComboBox7.ItemIndex;
  217. id:=StrToInt('0x'+Edit1.Text);
  218. str:=Edit4.Text;
  219. for i:=0 to 7 do
  220. begin
  221. strdata:=Copy(str,3*i+1,2);
  222. strdata:=Trim(strdata);
  223. if Length(strdata)=0 then
  224. break;
  225. data[i]:=StrToInt('0x'+strdata);
  226. end;
  227. senddata.SendType:=sendtype;
  228. senddata.ExternFlag:=frametype;
  229. senddata.RemoteFlag:=frameformat;
  230. senddata.ID:=id;
  231. senddata.DataLen:=i;
  232. Move(data,senddata.Data,i);
  233. if VCI_Transmit(m_devtype,m_devind,m_cannum,@senddata,1)=1 then
  234. ListBox1.Items.Add('���ͳɹ�')
  235. else
  236. ListBox1.Items.Add('����ʧ��');
  237. end;
  238. procedure TForm1.Button6Click(Sender: TObject);
  239. begin
  240. if m_connect=0 then
  241. Exit;
  242. if VCI_StartCAN(m_devtype,m_devind,m_cannum)<>1 then
  243. ShowMessage('����CANʧ��')
  244. else
  245. begin
  246. ListBox1.Items.Add('����CAN�ɹ�');
  247. Button5.Enabled := True;
  248. end
  249. end;
  250. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  251. begin
  252. if m_connect=1 then
  253. begin
  254. m_connect:=0;
  255. WaitForSingleObject(m_threadhandle,2000);
  256. m_threadhandle:=0;
  257. VCI_CloseDevice(m_devtype,m_devind);
  258. end
  259. end;
  260. procedure TForm1.ComboBox1Click(Sender: TObject);
  261. begin
  262. m_devtype := ComboBox1.ItemIndex + 3;
  263. if m_devtype = 3 then
  264. begin
  265. m_cannum := 0;
  266. ComboBox2.ItemIndex := 0;
  267. end
  268. end;
  269. procedure TForm1.ComboBox2Click(Sender: TObject);
  270. begin
  271. m_cannum := ComboBox2.ItemIndex;
  272. if m_devtype = 3 then
  273. begin
  274. ComboBox2.ItemIndex := 0;
  275. m_cannum := 0;
  276. end
  277. end;
  278. procedure TForm1.Button3Click(Sender: TObject);
  279. begin
  280. ListBox1.Clear();
  281. end;
  282. procedure TForm1.EnableUI(bEnable: BOOL);
  283. begin
  284. Label8.Enabled := bEnable;
  285. ComboBox1.Enabled := bEnable;
  286. Label9.Enabled := bEnable;
  287. ComboBox2.Enabled := bEnable;
  288. Label1.Enabled := bEnable;
  289. Edit2.Enabled := bEnable;
  290. Label2.Enabled := bEnable;
  291. Edit3.Enabled := bEnable;
  292. Label12.Enabled := bEnable;
  293. ComboBox3.Enabled := bEnable;
  294. Label10.Enabled := bEnable;
  295. Edit5.Enabled := bEnable;
  296. Label11.Enabled := bEnable;
  297. Edit6.Enabled := bEnable;
  298. Label13.Enabled := bEnable;
  299. ComboBox4.Enabled := bEnable;
  300. end;
  301. end.