//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PACSDK.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; const int DI_MAX_TOTAL_CH = 8; const int DO_MAX_TOTAL_CH = 8; const int ORIGIN_X = 10; const int ORIGIN_Y = 10; const int DIAMETER = 60; const int GAP_CIRCLE_LABEL = 5; const int LABEL_WIDTH = 60; const int LABEL_HEIGHT = 20; const int GAP_DI_DO = 10; const int CHECKBOX_WIDTH = 60; const int CHECKBOX_HEIGHT = 20; const int GAP_HORIZONTAL = 10; unsigned long diValue; unsigned long doValue; TCheckBox * cbArr[DO_MAX_TOTAL_CH]; TLabel * lbArr[DI_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; } for (int i = 0; i < DI_MAX_TOTAL_CH; i++) { lbArr[i] = new TLabel(this); lbArr[i]->Parent = Form1; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { cbbSlot->ItemIndex = 0; cbbDICh->ItemIndex = 0; cbbDOCh->ItemIndex = 0; int total_DI_ch = StrToInt(cbbDICh->Items->Strings[cbbDICh->ItemIndex]); int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); printCheckBoxs(Sender, total_DO_ch); printLabels(Sender, total_DI_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::printCheckBoxs(TObject *Sender, int channel) { int x = PaintBox1->Left + ORIGIN_X; int y = PaintBox1->Top + ORIGIN_Y + DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO; int vertical_shift = DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; 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; for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if ((cbArr[i]->Visible == true) && (cbArr[i]->Checked == true)) { DO_value += one << i; } } Edit3->Text = IntToHex((int)DO_value, DO_MAX_TOTAL_CH/4); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) //Read DI { char temp[32]; int slot = cbbSlot->ItemIndex + 1; int total_DI_ch = StrToInt(cbbDICh->Items->Strings[cbbDICh->ItemIndex]); int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); HANDLE h = uart_Open(""); diValue = 0; doValue = 0; pac_ReadDIO(h, slot, total_DI_ch, total_DO_ch, &diValue, &doValue); uart_Close(h); Edit1->Text = IntToHex((int)diValue, total_DI_ch/4); Edit2->Text = IntToHex((int)doValue, total_DO_ch/4); PaintBox1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::printLabels(TObject * Sender, int channel) { int x = PaintBox1->Left + ORIGIN_X; int y = PaintBox1->Top + ORIGIN_Y + DIAMETER + GAP_CIRCLE_LABEL; int vertical_shift = DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; for (int i = 0; i < channel; i++) { lbArr[i]->Left = x + horizontal_shift * (i % 8); lbArr[i]->Top = y + vertical_shift * (i / 8); lbArr[i]->Width = LABEL_WIDTH; lbArr[i]->Height = LABEL_HEIGHT; lbArr[i]->Caption = "DI_" + IntToStr(i); lbArr[i]->Visible = true; } for (int j = channel; j < DO_MAX_TOTAL_CH; j++) { lbArr[j]->Visible = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::cbbDIChChange(TObject *Sender) { int total_DI_ch = StrToInt(cbbDICh->Items->Strings[cbbDICh->ItemIndex]); PaintBox1->Refresh(); printLabels(Sender, total_DI_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { int x = ORIGIN_X; int y = ORIGIN_Y; int vertical_shift = DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; int total_DI_ch = StrToInt(cbbDICh->Items->Strings[cbbDICh->ItemIndex]); int x1, y1, x2, y2; for (int i = 0; i < total_DI_ch; i++) { if (pac_GetBit(diValue, i)) PaintBox1->Canvas->Brush->Color = clRed; else PaintBox1->Canvas->Brush->Color = clBlack; x1 = x + horizontal_shift * (i % 8); y1 = y + vertical_shift * (i / 8); x2 = x1 + DIAMETER; y2 = y1 + DIAMETER; PaintBox1->Canvas->Ellipse(x1, y1, x2, y2); } } //--------------------------------------------------------------------------- void __fastcall TForm1::cbbDOChChange(TObject *Sender) { int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); printCheckBoxs(Sender, total_DO_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit3KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == 0x0D) { unsigned int DO_value = StrToInt("$" + Edit3->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; } } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) //Write DO { int slot = cbbSlot->ItemIndex + 1; int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); unsigned int DO_Value = StrToInt("$" + Edit3->Text); //HexToInt HANDLE h = uart_Open(""); pac_WriteDO(h, slot, total_DO_ch, DO_Value); uart_Close(h); } //---------------------------------------------------------------------------