//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PISOCANDNMAPI.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_AddDevice_00Click(TObject *Sender) { WORD Ret,NewEPR,InputLen,OutputLen; BYTE NewMACID,ConType; BYTE Port; AnsiString Str; TButton *AddDeviceButton; TEdit *Edit_NewMACID,*Edit_NewEPR,*Edit_InputLen,*Edit_OutputLen; TRadioButton *RadioButton_Poll,*RadioButton_Strobe,*RadioButton_COS,*RadioButton_Cyclic; AnsiString EditName,RadioButtonName; AddDeviceButton = (TButton*)Sender; Port = AddDeviceButton->Name[AddDeviceButton->Name.Length()] - '0'; EditName.printf("Edit_NewMACID_0%d",Port); Edit_NewMACID = (TEdit*)FindComponent(EditName); EditName.printf("Edit_NewEPR_0%d",Port); Edit_NewEPR = (TEdit*)FindComponent(EditName); EditName.printf("Edit_InputLen_0%d",Port); Edit_InputLen = (TEdit*)FindComponent(EditName); EditName.printf("Edit_OutputLen_0%d",Port); Edit_OutputLen = (TEdit*)FindComponent(EditName); RadioButtonName.printf("RadioButton_Poll_0%d",Port); RadioButton_Poll = (TRadioButton*)FindComponent(RadioButtonName); RadioButtonName.printf("RadioButton_Strobe_0%d",Port); RadioButton_Strobe = (TRadioButton*)FindComponent(RadioButtonName); RadioButtonName.printf("RadioButton_COS_0%d",Port); RadioButton_COS = (TRadioButton*)FindComponent(RadioButtonName); RadioButtonName.printf("RadioButton_Cyclic_0%d",Port); RadioButton_Cyclic = (TRadioButton*)FindComponent(RadioButtonName); if(Edit_NewMACID->Text.Length() < 1) { ShowMessage("Please input New Slave ID"); return;} if(Edit_NewEPR->Text.Length() < 1) { ShowMessage("Please input EPR value"); return; } if(Edit_InputLen->Text.Length() < 1) { ShowMessage("Please input the input length"); return; } if(Edit_OutputLen->Text.Length() < 1) { ShowMessage("Please input the output length"); return;} NewMACID = Edit_NewMACID->Text.ToInt(); NewEPR = Edit_NewEPR->Text.ToInt(); InputLen = Edit_InputLen->Text.ToInt(); OutputLen = Edit_OutputLen->Text.ToInt(); if(RadioButton_Poll->Checked) ConType = ConType_Poll; if(RadioButton_Strobe->Checked) ConType = ConType_BitStrobe; if(RadioButton_COS->Checked) ConType = ConType_COS; if(RadioButton_Cyclic->Checked) ConType = ConType_Cyclic; //AddDevice Ret = CANDNM_AddDevice(0,Port,NewMACID,1000); if(Ret != 0) { Str.printf("AddDevice Error(%d)",Ret); ShowMessage(Str); return; } //AddIOConnection Ret = CANDNM_AddIOConnection(0,Port,NewMACID,ConType,InputLen,OutputLen,NewEPR); if(Ret != 0) { Str.printf("AddIOConnection Error(%d)",Ret); ShowMessage(Str); return; } ShowMessage("Add Device OK"); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_GetAttr_00Click(TObject *Sender) { WORD Ret; BYTE Data[512],DesMACID; WORD Len; AnsiString Str1,Str2; AnsiString temp; BYTE Port; TButton *GetAttrButton; TComboBox *ComboBox_SlaveID,*ComboBox_ClassID,*ComboBox_InstanceID,*ComboBox_AttributeID; AnsiString ComboBoxName,EditName; GetAttrButton = (TButton*)Sender; Port = GetAttrButton->Name[GetAttrButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_ClassID_0%d",Port); ComboBox_ClassID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_InstanceID_0%d",Port); ComboBox_InstanceID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_AttributeID_0%d",Port); ComboBox_AttributeID = ((TComboBox*)FindComponent(ComboBoxName)); DesMACID = ComboBox_SlaveID->Items->Strings[ComboBox_SlaveID->ItemIndex].ToInt(); Ret = CANDNM_GetAttribute(0,Port,DesMACID,ComboBox_ClassID->ItemIndex,ComboBox_InstanceID->ItemIndex,ComboBox_AttributeID->ItemIndex); Sleep(1000); Ret = CANDNM_GetAttributeValue(0,Port,DesMACID,Data,&Len); for(int i=0;iText = Str1; EditName.printf("GetAttrAsc_0%d",Port); ((TEdit*)FindComponent(EditName))->Text = Str2; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_StartDevice_00Click(TObject *Sender) { WORD Ret; AnsiString Str; BYTE Port; TButton *StartDeviceButton; TComboBox *ComboBox_SlaveID; AnsiString ComboBoxName; StartDeviceButton = (TButton*)Sender; Port = StartDeviceButton->Name[StartDeviceButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); BYTE DesMACID = ComboBox_SlaveID->Items->Strings[ComboBox_SlaveID->ItemIndex].ToInt(); Ret = CANDNM_StartDevice(0,Port,DesMACID); if(Ret == 0) Str.printf("Slave %d Started",DesMACID); else Str.printf("StartDevice Error(%d)",Ret); ShowMessage(Str); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_StopDevice_00Click(TObject *Sender) { WORD Ret; AnsiString Str; BYTE Port; TButton *StopDeviceButton; TComboBox *ComboBox_SlaveID; AnsiString ComboBoxName; StopDeviceButton = (TButton*)Sender; Port = StopDeviceButton->Name[StopDeviceButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); BYTE DesMACID = ComboBox_SlaveID->Items->Strings[ComboBox_SlaveID->ItemIndex].ToInt(); Ret = CANDNM_StopDevice(0,Port,DesMACID); if(Ret == 0) Str.printf("Slave %d Stopped",DesMACID); else Str.printf("StopDevice Error(%d)",Ret); ShowMessage(Str); } //--------------------------------------------------------------------------- BYTE StrToHex(AnsiString &str) { BYTE A; if(str.Length() == 1) { switch(str[1]) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'a': return 0x0A; case 'A': return 0x0A; case 'b': return 0x0B; case 'B': return 0x0B; case 'c': return 0x0C; case 'C': return 0x0C; case 'd': return 0x0D; case 'D': return 0x0D; case 'e': return 0x0E; case 'E': return 0x0E; case 'f': return 0x0F; case 'F': return 0x0F; } } else { switch(str[1]) { case '0': A = 0; break; case '1': A = 1*16; break; case '2': A = 2*16; break; case '3': A = 3*16; break; case '4': A = 4*16; break; case '5': A = 5*16; break; case '6': A = 6*16; break; case '7': A = 7*16; break; case '8': A = 8*16; break; case '9': A = 9*16; break; case 'a': A = 10*16; break; case 'A': A = 10*16; break; case 'b': A = 11*16; break; case 'B': A = 11*16; break; case 'c': A = 12*16; break; case 'C': A = 12*16; break; case 'd': A = 13*16; break; case 'D': A = 13*16; break; case 'e': A = 14*16; break; case 'E': A = 14*16; break; case 'f': A = 15*16; break; case 'F': A = 15*16; break; } switch(str[2]) { case '0': return A; case '1': return A+1; case '2': return A+2; case '3': return A+3; case '4': return A+4; case '5': return A+5; case '6': return A+6; case '7': return A+7; case '8': return A+8; case '9': return A+9; case 'a': return A+10; case 'A': return A+10; case 'b': return A+11; case 'B': return A+11; case 'c': return A+12; case 'C': return A+12; case 'd': return A+13; case 'D': return A+13; case 'e': return A+14; case 'E': return A+14; case 'f': return A+15; case 'F': return A+15; } } } void __fastcall TForm1::Button_SetAttr_00Click(TObject *Sender) { BYTE Data[64]; int DataLen; int j; WORD Ret; AnsiString str1,str2; AnsiString Str; BYTE Port; TButton *SetAttrButton; TComboBox *ComboBox_SlaveID,*ComboBox_ClassID,*ComboBox_InstanceID,*ComboBox_AttributeID; AnsiString ComboBoxName,EditName; SetAttrButton = (TButton*)Sender; Port = SetAttrButton->Name[SetAttrButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_ClassID_0%d",Port); ComboBox_ClassID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_InstanceID_0%d",Port); ComboBox_InstanceID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_AttributeID_0%d",Port); ComboBox_AttributeID = ((TComboBox*)FindComponent(ComboBoxName)); BYTE DesMACID = ComboBox_SlaveID->Items->Strings[ComboBox_SlaveID->ItemIndex].ToInt(); EditName.printf("Edit_SetAttr_0%d",Port); str1 = ((TEdit*)FindComponent(EditName))->Text; if(str1.Length() < 1) return; DataLen = 0; while(1) { str2 = str1.SubString(1,(str1.Pos(",")-1)); if(str2.Length() < 1) break; Data[DataLen] = StrToHex(str2); DataLen++; str1 = str1.Delete(1,str1.Pos(",")); if(str1.Pos(",") == 0) break; } Data[DataLen] = StrToHex(str1); DataLen++; /* for(j = 0;jItemIndex,ComboBox_InstanceID->ItemIndex,ComboBox_AttributeID->ItemIndex,DataLen,Data); EditName.printf("Edit_SetAttr_0%d",Port); if(Ret == DNM_NoError) ((TEdit*)FindComponent(EditName))->Text = "OK !"; else ((TEdit*)FindComponent(EditName))->Text = "Fail............!"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_Active_00Click(TObject *Sender) { DWORD Ret; BYTE Port; TButton *ActiveButton; AnsiString ShapeName; ActiveButton = (TButton*)Sender; Port = ActiveButton->Name[ActiveButton->Name.Length()] - '0'; Ret = CANDNM_Active(0,Port); ShapeName.sprintf("Shape_0%d",Port); if(Ret == DNMXS_NoError) ((TShape*)FindComponent(ShapeName))->Brush->Color = clLime; else ((TShape*)FindComponent(ShapeName))->Brush->Color = clRed; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_GetMasterMACID_00Click(TObject *Sender) { DWORD Ret; BYTE Port; TButton *GetMasterMACIDButton; AnsiString EditName; GetMasterMACIDButton = (TButton*)Sender; Port = GetMasterMACIDButton->Name[GetMasterMACIDButton->Name.Length()] - '0'; Ret = CANDNM_GetMasterMACID(0,Port); EditName.printf("Edit_MasterMACID_0%d",Port); ((TEdit*)FindComponent(EditName))->Text = Ret; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_SetMasterMACID_00Click(TObject *Sender) { DWORD Ret; AnsiString Str; BYTE MasterMACID; BYTE Port; TButton *SetMasterMACIDButton; AnsiString EditName; SetMasterMACIDButton = (TButton*)Sender; Port = SetMasterMACIDButton->Name[SetMasterMACIDButton->Name.Length()] - '0'; EditName.printf("Edit_MasterMACID_0%d",Port); MasterMACID = ((TEdit*)FindComponent(EditName))->Text.ToInt(); Ret = CANDNM_SetMasterMACID(0,Port,MasterMACID); if(Ret) Str.printf("CANDNM_SetMasterMACID Error(%d)",Ret); else Str.printf("SetMasterMACID OK!"); ShowMessage(Str); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_GetBaudRate_00Click(TObject *Sender) { DWORD Ret; BYTE Port; TButton *GetBaudRateButton; AnsiString EditName; GetBaudRateButton = (TButton*)Sender; Port = GetBaudRateButton->Name[GetBaudRateButton->Name.Length()] - '0'; Ret = CANDNM_GetBaudRate(0,Port); EditName.printf("Edit_BaudRate_0%d",Port); ((TEdit*)FindComponent(EditName))->Text = Ret; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_SetBaudRate_00Click(TObject *Sender) { AnsiString Str; DWORD Ret; BYTE BR; BYTE Port; TButton *SetBaudRateButton; AnsiString EditName; SetBaudRateButton = (TButton*)Sender; Port = SetBaudRateButton->Name[SetBaudRateButton->Name.Length()] - '0'; EditName.printf("Edit_BaudRate%d",Port+1); BR = ((TEdit*)FindComponent(EditName))->Text.ToInt(); Ret = CANDNM_SetBaudRate(0,Port,BR); if(Ret) Str.printf("CANDNM_SetBaudRate Error(%d)",Ret); else Str.printf("CANDNM_SetBaudRate OK!"); ShowMessage(Str); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { DWORD Ret; BYTE TotalBoards,BoardIDList[20],PortNumList[20]; Ret = CANDNM_TotalPISOCANDNMBoard(&TotalBoards,BoardIDList,PortNumList); Edit_TotalBoards->Text = TotalBoards; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_GetScanList_00Click(TObject *Sender) { short Ret; AnsiString Str; WORD i,TotalDevices,InputDataLenList[128],OutputDataLenList[128],EPR_List[128]; BYTE DesMACIDList[128],ConnectionTypeList[128]; BYTE Port; TButton *GetScanListButton; TComboBox *ComboBox_SlaveID; AnsiString ComboBoxName; GetScanListButton = (TButton*)Sender; Port = GetScanListButton->Name[GetScanListButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); Ret = CANDNM_GetScanList(0,Port,&TotalDevices,DesMACIDList,ConnectionTypeList,InputDataLenList,OutputDataLenList,EPR_List); ComboBox_SlaveID->Items->Clear(); if(Ret) Str.printf("CANDNM_GetScanList Error(%d)",Ret); else Str.printf("CANDNM_GetScanList OK"); ShowMessage(Str); if(Ret) return; for(i = 0;i < TotalDevices;i++) { if(ConnectionTypeList[i] == ConType_Explicit) ComboBox_SlaveID->Items->Add(DesMACIDList[i]); } if(TotalDevices >= 1) ComboBox_SlaveID->ItemIndex = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_ClosePort_00Click(TObject *Sender) { BYTE Port; short Ret; TButton *ClosePortButton; AnsiString Str; ClosePortButton = (TButton*)Sender; Port = ClosePortButton->Name[ClosePortButton->Name.Length()] - '0'; Ret = CANDNM_Close(0,Port); if(Ret) Str.printf("Close Error(%d)",Ret); else Str.printf("Close OK"); ShowMessage(Str); Str.printf("Shape_0%d",Port); ((TShape*)FindComponent(Str))->Brush->Color = clRed; } //---------------------------------------------------------------------------