//--------------------------------------------------------------------------- #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_HL = 5; const int LENGTH_CUBE_HL_DIVIDED_3 = 9; const int GAP_H_L = DIAMETER - LENGTH_CUBE_HL_DIVIDED_3 * 3 * 2; const int GAP_HL_LABEL = 5; const int LABEL_WIDTH = DIAMETER; const int LABEL_HEIGHT = 20; const int GAP_DI_DO = 10; const int CHECKBOX_WIDTH = DIAMETER; const int CHECKBOX_HEIGHT = 20; const int GAP_CHECKBOX_HL = 5; const int GAP_HORIZONTAL = 10; unsigned long diValue; unsigned long doValue; unsigned long DILowLatchValue = 0; unsigned long DIHighLatchValue = 0; unsigned long DOLowLatchValue = 0; unsigned long DOHighLatchValue = 0; bool Hshape[] = { true, false, true, true, true, true, true, false, true }; //H shape: // T F T // T T T // T F T bool Lshape[] = { true, false, false, true, false, false, true, true, true }; //L shape: // T F F // T F F // T T T 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_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; 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 DIO... { 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(""); //Read DIO diValue = 0; doValue = 0; pac_ReadDIO(h, slot, total_DI_ch, total_DO_ch, &diValue, &doValue); Edit1->Text = IntToHex((int)diValue, total_DI_ch/4); Edit2->Text = IntToHex((int)doValue, total_DO_ch/4); //Read DIO Low Latch DILowLatchValue = 0; DOLowLatchValue = 0; pac_ReadDIOLatch(h, slot, total_DI_ch, total_DO_ch, 0, &DILowLatchValue, &DOLowLatchValue); Edit7->Text = IntToHex((int)DILowLatchValue, total_DI_ch/4); Edit6->Text = IntToHex((int)DOLowLatchValue, total_DO_ch/4); //Read DIO High Latch DOHighLatchValue = 0; DIHighLatchValue = 0; pac_ReadDIOLatch(h, slot, total_DI_ch, total_DO_ch, 1, &DIHighLatchValue, &DOHighLatchValue); Edit5->Text = IntToHex((int)DIHighLatchValue, total_DI_ch/4); Edit4->Text = IntToHex((int)DOHighLatchValue, total_DO_ch/4); uart_Close(h); 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_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; 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_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; int DI_vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL; int DI_horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int DO_vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int DO_horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int total_DI_ch = StrToInt(cbbDICh->Items->Strings[cbbDICh->ItemIndex]); int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); int x1, y1, x2, y2; for (int i = 0; i < total_DI_ch; i++) { //print DI value: 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); //print DI Low latch status: if (pac_GetBit(DILowLatchValue, i)) PaintBox1->Canvas->Brush->Color = clGreen; else PaintBox1->Canvas->Brush->Color = clBlack; printShape(Sender, x + DI_horizontal_shift_L + horizontal_shift * (i % 8), y + DI_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Lshape); //print DI High latch status: if (pac_GetBit(DIHighLatchValue, i)) PaintBox1->Canvas->Brush->Color = clBlue; else PaintBox1->Canvas->Brush->Color = clBlack; printShape(Sender, x + horizontal_shift * (i % 8), y + DI_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } for (int i = 0; i < total_DO_ch; i++) { //print DO Low latch status: if (pac_GetBit(DOLowLatchValue, i)) PaintBox1->Canvas->Brush->Color = clGreen; else PaintBox1->Canvas->Brush->Color = clBlack; printShape(Sender, x + DO_horizontal_shift_L + horizontal_shift * (i % 8), y + DO_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Lshape); //print DO High latch status: if (pac_GetBit(DOHighLatchValue, i)) PaintBox1->Canvas->Brush->Color = clBlue; else PaintBox1->Canvas->Brush->Color = clBlack; printShape(Sender, x + horizontal_shift * (i % 8), y + DO_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } } //--------------------------------------------------------------------------- //print shape of characters. (H or L) void __fastcall TForm1::printShape(TObject * Sender, int x, int y, int length, bool * shape) { TRect rect; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (shape[j + 3 * i]) { rect.Left = x + length * j; rect.Top = y + length * i; rect.Right = rect.Left + length; rect.Bottom = rect.Top + length; PaintBox1->Canvas->FillRect(rect); } } } } //--------------------------------------------------------------------------- void __fastcall TForm1::cbbDOChChange(TObject *Sender) { int total_DO_ch = StrToInt(cbbDOCh->Items->Strings[cbbDOCh->ItemIndex]); printCheckBoxs(Sender, total_DO_ch); PaintBox1->Refresh(); } //--------------------------------------------------------------------------- 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); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) //Clear DIO Latch Values { int slot = cbbSlot->ItemIndex + 1; HANDLE h = uart_Open(""); pac_ClearDIOLatch(h, slot); uart_Close(h); } //---------------------------------------------------------------------------