//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PISOCANDNMAPI.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //Port 0 WORD TotalDevices_00,InputDataLenList_00[128],OutputDataLenList_00[128],EPR_List_00[128]; BYTE DesMACIDList_00[128],ConnectionTypeList_00[128]; //Port 1 WORD TotalDevices_01,InputDataLenList_01[128],OutputDataLenList_01[128],EPR_List_01[128]; BYTE DesMACIDList_01[128],ConnectionTypeList_01[128]; //--------------------------------------------------------------------------- __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_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_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,*ComboBox_IOSlaveID; AnsiString ComboBoxName; GetScanListButton = (TButton*)Sender; Port = GetScanListButton->Name[GetScanListButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_SlaveID_0%d",Port); ComboBox_SlaveID = ((TComboBox*)FindComponent(ComboBoxName)); ComboBoxName.printf("ComboBox_IOSlaveID_0%d",Port); ComboBox_IOSlaveID = ((TComboBox*)FindComponent(ComboBoxName)); if(Port == 0) Ret = CANDNM_GetScanList(0,Port,&TotalDevices_00,DesMACIDList_00,ConnectionTypeList_00,InputDataLenList_00,OutputDataLenList_00,EPR_List_00); else Ret = CANDNM_GetScanList(0,Port,&TotalDevices_01,DesMACIDList_01,ConnectionTypeList_01,InputDataLenList_01,OutputDataLenList_01,EPR_List_01); ComboBox_SlaveID->Items->Clear(); ComboBox_IOSlaveID->Items->Clear(); if(Ret) Str.printf("CANDNM_GetScanList Error(%d)",Ret); else Str.printf("CANDNM_GetScanList OK"); ShowMessage(Str); if(Ret) return; if(Port == 0) { for(i = 0;i < TotalDevices_00;i++) { if(ConnectionTypeList_00[i] == ConType_Explicit) { ComboBox_SlaveID->Items->Add(DesMACIDList_00[i]); ComboBox_IOSlaveID->Items->Add(DesMACIDList_00[i]); } } if(TotalDevices_00 >= 1) ComboBox_SlaveID->ItemIndex = 0; } else { for(i = 0;i < TotalDevices_01;i++) { if(ConnectionTypeList_01[i] == ConType_Explicit) { ComboBox_SlaveID->Items->Add(DesMACIDList_01[i]); ComboBox_IOSlaveID->Items->Add(DesMACIDList_01[i]); } } if(TotalDevices_01 >= 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; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_ReadInput_00Click(TObject *Sender) { BYTE Port,DesMACID,ConType = 0; short Ret; WORD IOLen,i; BYTE IODATA[512]; TButton *ReadInputButton; TComboBox *ComboBox_IOSlaveID; TEdit *Edit_ConType,*Edit_InputData; AnsiString Str,IDName,EditName,IOStr; ReadInputButton = (TButton*)Sender; Port = ReadInputButton->Name[ReadInputButton->Name.Length()] - '0'; IDName.printf("ComboBox_IOSlaveID_0%d",Port); ComboBox_IOSlaveID = (TComboBox*)FindComponent(IDName); EditName.printf("Edit_ConType_0%d",Port); Edit_ConType = (TEdit*)FindComponent(EditName); EditName.printf("Edit_InputData_0%d",Port); Edit_InputData = (TEdit*)FindComponent(EditName); DesMACID = ComboBox_IOSlaveID->Items->Strings[ComboBox_IOSlaveID->ItemIndex].ToInt(); if(Edit_ConType->Text == "Poll") ConType = ConType_Poll; if(Edit_ConType->Text == "Strobe") ConType = ConType_BitStrobe; if(Edit_ConType->Text == "COS") ConType = ConType_COS; if(Edit_ConType->Text == "Cyclic") ConType = ConType_Cyclic; Ret = CANDNM_ReadInputData(0,Port,DesMACID,ConType,&IOLen,IODATA); if(Ret) { Str.printf("ReadInputData Error(%d)",Ret); ShowMessage(Str); return; } IOStr = ""; for(i = 0;i < IOLen;i++) { Str.printf("%02X,",IODATA[i]); IOStr += Str; } Edit_InputData->Text = IOStr; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox_IOSlaveID_00Change(TObject *Sender) { BYTE SlaveID,Port,i,ConType; WORD OutputLen; AnsiString ComboBoxName; TComboBox *ComboBox_IOSlaveID; TEdit *Edit_ConType,*Edit_InputData,*Edit_OutputData; AnsiString Str,IDName,EditName,IOStr; ComboBox_IOSlaveID = (TComboBox*)Sender; Port = ComboBox_IOSlaveID->Name[ComboBox_IOSlaveID->Name.Length()] - '0'; SlaveID = ComboBox_IOSlaveID->Items->Strings[ComboBox_IOSlaveID->ItemIndex].ToInt(); EditName.printf("Edit_ConType_0%d",Port); Edit_ConType = (TEdit*)FindComponent(EditName); EditName.printf("Edit_OutputData_0%d",Port); Edit_OutputData = (TEdit*)FindComponent(EditName); for(i = 0;i < 128;i++) { if(Port == 0) { if((DesMACIDList_00[i] == SlaveID) && (ConnectionTypeList_00[i] != ConType_Explicit)) { ConType = ConnectionTypeList_00[i]; OutputLen = OutputDataLenList_00[i]; break; } } else { if((DesMACIDList_00[i] == SlaveID) && (ConnectionTypeList_01[i] != ConType_Explicit)) { ConType = ConnectionTypeList_01[i]; OutputLen = OutputDataLenList_01[i]; break; } } } switch(ConType) { case ConType_Poll: Edit_ConType->Text = "Poll"; break; case ConType_BitStrobe: Edit_ConType->Text = "Strobe"; break; case ConType_COS: Edit_ConType->Text = "COS"; break; case ConType_Cyclic: Edit_ConType->Text = "Cyclic"; break; default: Edit_ConType->Text = ConType; break; } if(OutputLen != 0) { IOStr = ""; for(i = 0;i < OutputLen;i++) { Str.printf("00,"); IOStr += Str; } IOStr[IOStr.Length()] = ';'; } else IOStr = ""; Edit_OutputData->Text = IOStr; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button_WriteOutput_00Click(TObject *Sender) { BYTE Data[512]; WORD DataLen; int j; WORD Ret; AnsiString str1,str2; AnsiString Str; BYTE Port,DesMACID,ConType; TButton *WriteOutputButton; TComboBox *ComboBox_IOSlaveID; AnsiString ComboBoxName,EditName; WriteOutputButton = (TButton*)Sender; Port = WriteOutputButton->Name[WriteOutputButton->Name.Length()] - '0'; ComboBoxName.printf("ComboBox_IOSlaveID_0%d",Port); ComboBox_IOSlaveID = (TComboBox*)FindComponent(ComboBoxName); DesMACID = ComboBox_IOSlaveID->Items->Strings[ComboBox_IOSlaveID->ItemIndex].ToInt(); EditName.printf("Edit_ConType_0%d",Port); if(((TEdit*)FindComponent(EditName))->Text == "Poll")ConType = ConType_Poll; if(((TEdit*)FindComponent(EditName))->Text == "Strobe")ConType = ConType_BitStrobe; if(((TEdit*)FindComponent(EditName))->Text == "COS")ConType = ConType_COS; if(((TEdit*)FindComponent(EditName))->Text == "Cyclic")ConType = ConType_Cyclic; EditName.printf("Edit_OutputData_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++; Ret = CANDNM_WriteOutputData(0,Port,DesMACID,ConType,DataLen,Data); if(Ret) Str.printf("Write Output Data Error(%d",Ret); else Str.printf("Write Output Data OK"); ShowMessage(Str); } //---------------------------------------------------------------------------