//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PACSDK.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; const int DO_MAX_TOTAL_CH = 32; const int ORIGIN_X = 20; const int ORIGIN_Y = 120; const int HORIZONTAL_SHIFT = 80; const int VERTICAL_SHIFT = 40; const int CHECKBOX_WIDTH = 80; const int CHECKBOX_HEIGHT = 20; const int DIAMETER = 60; const int GAP_CIRCLE_LABEL = 5; const int GAP_DI_DO = 10; const int GAP_HORIZONTAL = 10; TCheckBox * cbArr[DO_MAX_TOTAL_CH]; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { cbArr[i] = new TCheckBox(this); cbArr[i]->OnClick = checkBox_CheckStateChanged; //BCB has no CheckStateChanged event cbArr[i]->Parent = Form1; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { cbbSlot->ItemIndex = 0; cbbChannel->ItemIndex = 0; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); printCheckBoxs(Sender, total_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::printCheckBoxs(TObject *Sender, int channel) { int x = ORIGIN_X; int y = ORIGIN_Y; for (int i = 0; i < channel; i++) { cbArr[i]->Left = x + HORIZONTAL_SHIFT * (i % 8); cbArr[i]->Top = y + VERTICAL_SHIFT * (i / 8); cbArr[i]->Width = CHECKBOX_WIDTH; cbArr[i]->Height = CHECKBOX_HEIGHT; cbArr[i]->Caption = "DO_" + IntToStr(i); cbArr[i]->Visible = true; } for (int j = channel; j < DO_MAX_TOTAL_CH; j++) { cbArr[j]->Visible = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::checkBox_CheckStateChanged(TObject * Sender) { unsigned int DO_value = 0; unsigned int one = 1; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if ((cbArr[i]->Visible == true) && (cbArr[i]->Checked == true)) { DO_value += one << i; } } Edit1->Text = IntToHex((int)DO_value, total_ch/4); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) //Write DO { int slot = cbbSlot->ItemIndex + 1; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); HANDLE h = uart_Open(""); unsigned int DO_Value = StrToInt("$" + Edit1->Text); pac_WriteDO(h, slot, total_ch, DO_Value); uart_Close(h); } //--------------------------------------------------------------------------- void __fastcall TForm1::cbbChannelChange(TObject *Sender) { int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); printCheckBoxs(Sender, total_ch); Button3Click(Sender); //clear all } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { //select all for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if (cbArr[i]->Visible == true) { cbArr[i]->Checked = true; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { //clear all for (int i = 0; i < 32; i++) { if (cbArr[i]->Visible == true) { cbArr[i]->Checked = false; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == 0x0D) { unsigned int DO_value = StrToInt("$" + Edit1->Text); //HexToInt for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if (1 == (1 & (DO_value >> i))) { cbArr[i]->Checked = true; } else { cbArr[i]->Checked = false; } } } } //---------------------------------------------------------------------------