?? unit1.pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ControlCAN;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Button1: TButton;
Button2: TButton;
GroupBox1: TGroupBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Button5: TButton;
GroupBox2: TGroupBox;
Label8: TLabel;
Label9: TLabel;
Button6: TButton;
ComboBox5: TComboBox;
ComboBox6: TComboBox;
ComboBox7: TComboBox;
Edit1: TEdit;
Edit4: TEdit;
GroupBox6: TGroupBox;
ListBox1: TListBox;
GroupBox3: TGroupBox;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Label2: TLabel;
Edit5: TEdit;
Label10: TLabel;
Edit6: TEdit;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
end;
PTListBox=^TListBox;
var
Form1: TForm1;
var
m_devtype : DWORD;
m_devind : DWORD;
m_cannum : DWORD;
m_connect : DWORD;
m_threadhandle : integer;
implementation
{$R *.dfm}
function ReceiveThread(param : Pointer): integer;
var
receivedata : array[0..49] of VCI_CAN_OBJ;
len : integer;
j : integer;
i : integer;
str : AnsiString;
tmpstr :AnsiString;
box : PTListBox;
errinfo : VCI_ERR_INFO;
begin
box:=param;
while TRUE do
begin
if m_connect=0 then
break;
Sleep(1);
len:=VCI_Receive(m_devtype,m_devind,m_cannum,@receivedata[0],50,200);
if len<=0 then
begin
//注意:如果沒有讀到數(shù)據(jù)則必須調(diào)用此函數(shù)來讀取出當前的錯誤碼,
//千萬不能省略這一步(即使你可能不想知道錯誤碼是什么)
VCI_ReadErrInfo(m_devtype,m_devind,m_cannum,@errinfo);
continue;
end;
for i:=0 to len-1 do
begin
str:='接收到數(shù)據(jù)幀: ';
if receivedata[i].TimeFlag=0 then
tmpstr:='時間標識:無 '
else
tmpstr:='時間標識:0x'+IntToHex(receivedata[i].TimeStamp,8)+' ';
str:=str+tmpstr;
tmpstr:='幀ID:0x'+IntToHex(receivedata[i].ID,8)+' ';
str:=str+tmpstr;
str:=str+'幀格式:';
if receivedata[i].RemoteFlag=0 then
tmpstr:='數(shù)據(jù)幀 '
else
tmpstr:='遠程幀 ';
str:=str+tmpstr;
str:=str+'幀類型:';
if receivedata[i].ExternFlag=0 then
tmpstr:='標準幀 '
else
tmpstr:='擴展幀 ';
str:=str+tmpstr;
box.Items.Add(str);
if receivedata[i].RemoteFlag=0 then
begin
str:='數(shù)據(jù):';
if receivedata[i].DataLen>8 then
receivedata[i].DataLen:=8;
for j:=0 to receivedata[i].DataLen-1 do
begin
tmpstr:=IntToHex(receivedata[i].Data[j],2)+' ';
str:=str+tmpstr;
end;
box.Items.Add(str);
end;
end;
box.ItemIndex:=box.Items.Count-1;
end;
EndThread(0);
ReceiveThread:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
index: integer;
cannum: integer;
threadid: LongWord;
initconfig : VCI_INIT_CONFIG;
begin
index:=ComboBox1.ItemIndex;
cannum:=ComboBox2.ItemIndex;
if m_connect=1 then
begin
m_connect:=0;
Button1.Caption:='連接';
VCI_CloseDevice(m_devtype,m_devind);
Exit;
end;
if (index>=0) and (cannum>=0) then
begin
if VCI_OpenDevice(m_devtype,index,0)<>1 then
ShowMessage('端口打開失敗')
else
begin
initconfig.AccCode:=StrToInt('0x'+Edit2.Text);
initconfig.AccMask:=StrToInt('0x'+Edit3.Text);
initconfig.Timing0:=StrToInt('0x'+Edit5.Text);
initconfig.Timing1:=StrToInt('0x'+Edit6.Text);
initconfig.Filter:=ComboBox3.ItemIndex;
initconfig.Mode:=ComboBox4.ItemIndex;
if VCI_InitCAN(m_devtype,m_devind,cannum,@initconfig)<>1 then
begin
Showmessage('初始化CAN失敗');
Exit;
end;
m_devind:=index;
m_cannum:=cannum;
m_connect:=1;
Button1.Caption:='斷開';
threadid:=111;
m_threadhandle:=BeginThread(0,0,ReceiveThread,@ListBox1,0,threadid);
end;
end
else
ShowMessage('選項不能為空');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if m_connect=0 then
Exit;
if VCI_ResetCAN(m_devtype,m_devind,m_cannum)<>1 then
ShowMessage('復位CAN失敗')
else
ListBox1.Items.Add('復位CAN成功');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m_devtype := 1;//PCI5121類型號
m_devind := 0;
m_cannum :=0;
m_connect:= 0;
ComboBox1.ItemIndex:=0;
ComboBox2.ItemIndex:=0;
ComboBox3.ItemIndex:=0;
ComboBox4.ItemIndex:=0;
ComboBox5.ItemIndex:=2;
ComboBox6.ItemIndex:=0;
ComboBox7.ItemIndex:=0;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
sendtype,frametype,frameformat : BYTE;
id : DWORD;
data : array[0..7] of BYTE;
str : AnsiString;
strdata : AnsiString;
senddata : VCI_CAN_OBJ;
i : integer;
begin
if m_connect=0 then
Exit;
sendtype:=ComboBox5.ItemIndex;
frametype:=ComboBox6.ItemIndex;
frameformat:=ComboBox7.ItemIndex;
id:=StrToInt('0x'+Edit1.Text);
str:=Edit4.Text;
for i:=0 to 7 do
begin
strdata:=Copy(str,3*i+1,2);
strdata:=Trim(strdata);
if Length(strdata)=0 then
break;
data[i]:=StrToInt('0x'+strdata);
end;
senddata.SendType:=sendtype;
senddata.ExternFlag:=frametype;
senddata.RemoteFlag:=frameformat;
senddata.ID:=id;
senddata.DataLen:=i;
Move(data,senddata.Data,i);
if VCI_Transmit(m_devtype,m_devind,m_cannum,@senddata,1)=1 then
ListBox1.Items.Add('發(fā)送成功')
else
ListBox1.Items.Add('發(fā)送失敗');
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if m_connect=0 then
Exit;
if VCI_StartCAN(m_devtype,m_devind,m_cannum)<>1 then
ShowMessage('啟動CAN失敗')
else
ListBox1.Items.Add('啟動CAN成功');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if m_connect=1 then
begin
m_connect:=0;
WaitForSingleObject(m_threadhandle,2000);
m_threadhandle:=0;
VCI_CloseDevice(m_devtype,m_devind);
end
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -