//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PACSDK.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //The features of Latch and Counter in DI modules are not implemented in 8K series. //Only 87K or 7K series support functions of Latch and Counter. const int ORIGIN_X = 10; const int ORIGIN_Y = 10; const int DIAMETER = 40; const int LENGTH_CUBE_HL_DIVIDED_3 = 6; const int GAP_CIRCLE_HL = 5; const int GAP_H_L = 4; const int GAP_HL_BUTTON = 5; const int BUTTON_WIDTH = 40; const int BUTTON_HEIGHT = 25; const int HORIZONTAL_GAP_WHOLE_SET = 10; const int VERTICAL_GAP_WHOLE_SET = 5; unsigned long diValue; unsigned long LowLatchValue; unsigned long HighLatchValue; 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 TButton * btn[32]; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { for (int i = 0; i < 32; i++) { btn[i] = new TButton(this); btn[i]->OnClick = btn_Click; btn[i]->Tag = i; //used for OnClick to identify which button clicked btn[i]->Parent = Form1; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { cbbSlot->ItemIndex = 0; cbbChannel->ItemIndex = 0; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); printButtons(Sender, total_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_Click(TObject *Sender) //clear DI count { int slot = cbbSlot->ItemIndex + 1; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); HANDLE h = uart_Open(""); for (int i = 0; i < 32; i++) { if (((TButton *)Sender)->Tag == i) //which button clicked { pac_ClearDICNT(h, slot, i, total_ch); btn[i]->Caption = "0"; } } uart_Close(h); } //--------------------------------------------------------------------------- void __fastcall TForm1::printButtons(TObject *Sender, int channel) //print buttons according to total channel { int x = PaintBox1->Left + 10; int y = PaintBox1->Top + 10 + DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON; int horizontal_shift = DIAMETER + HORIZONTAL_GAP_WHOLE_SET; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON + BUTTON_HEIGHT + VERTICAL_GAP_WHOLE_SET; for (int i = 0; i < channel; i++) { btn[i]->Left = x + (i % 8) * horizontal_shift; btn[i]->Top = y + (i / 8) * vertical_shift; btn[i]->Width = BUTTON_WIDTH; btn[i]->Height = BUTTON_HEIGHT; btn[i]->Visible = true; } for (int j = channel; j < 32; j++) { btn[j]->Visible = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) //Read DI... { int slot = cbbSlot->ItemIndex + 1; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); HANDLE h = uart_Open(""); //DI Value: diValue = 0; pac_ReadDI(h, slot, total_ch, &diValue); Edit1->Text = IntToStr(diValue); //DI High Latch Value: HighLatchValue = 0; pac_ReadDILatch(h, slot, total_ch, 1, &HighLatchValue); Edit2->Text = IntToStr(HighLatchValue); //DI Low Latch Value: LowLatchValue = 0; pac_ReadDILatch(h, slot, total_ch, 0, &LowLatchValue); Edit3->Text = IntToStr(LowLatchValue); //DI Counts unsigned long counterValue = 0; for (int i = 0; i < total_ch; i++) { pac_ReadDICNT(h, slot, i, total_ch, &counterValue); btn[i]->Caption = IntToStr(counterValue); } uart_Close(h); PaintBox1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::printShape(TObject *Sender, TColor brushColor, 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]) { PaintBox1->Canvas->Brush->Color = brushColor; 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::cbbChannelChange(TObject *Sender) { int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); PaintBox1->Refresh(); printButtons(Sender, total_ch); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { int horizontal_shift = DIAMETER + HORIZONTAL_GAP_WHOLE_SET; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON + BUTTON_HEIGHT + VERTICAL_GAP_WHOLE_SET; int vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL; int horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int total_ch = StrToInt(cbbChannel->Items->Strings[cbbChannel->ItemIndex]); int x1, y1, x2, y2; TColor brushColor; for (int i = 0; i < total_ch; i++) { //print DI status: if (pac_GetBit(diValue, i)) PaintBox1->Canvas->Brush->Color = clRed; else PaintBox1->Canvas->Brush->Color = clBlack; x1 = ORIGIN_X + horizontal_shift * (i % 8); y1 = ORIGIN_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(LowLatchValue, i)) brushColor = clGreen; else brushColor = clBlack; printShape(Sender, brushColor, ORIGIN_X + horizontal_shift_L + horizontal_shift * (i % 8), ORIGIN_Y + vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Lshape); //print DI High latch status: if (pac_GetBit(HighLatchValue, i)) brushColor = clBlue; else brushColor = clBlack; printShape(Sender, brushColor, ORIGIN_X + horizontal_shift * (i % 8), ORIGIN_Y + vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { int slot = cbbSlot->ItemIndex + 1; HANDLE h = uart_Open(""); pac_ClearDILatch(h, slot); uart_Close(h); Edit2->Text = "0"; Edit3->Text = "0"; PaintBox1->Refresh(); } //---------------------------------------------------------------------------