//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "PACSDK.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define TBitmap Graphics::TBitmap //to avoid ambiguity of Windows::TBitmap const int startX = 20; const int startY = 30; const int UNIT_LENGTH = 55; const int GAP_SLOT = 5; const int GAP_MODULE_LABEL = 20; const int LABEL_HEIGHT = 20; int slotCount = 0; bool ModuleExist[7]; String ModuleName[7]; TLabel * lbSlot[7]; TLabel * lbmoduleName[7]; TImage * headImage; //image of CPU-power module TImage * moduleImage; //image of I/O slot module TImage * emptyImage; //image of a slot in empty status TImage * igHead; TImage * igModule[7]; //take place with picturebox //-------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { headImage = new TImage(this); moduleImage = new TImage(this); emptyImage = new TImage(this); ImageList1->GetBitmap(1, headImage->Picture->Bitmap); ImageList1->GetBitmap(2, moduleImage->Picture->Bitmap); ImageList1->GetBitmap(0, emptyImage->Picture->Bitmap); //initialize image of CPU-power module igHead = new TImage(this); igHead->Stretch = true; igHead->Picture->Assign(headImage->Picture); igHead->Width = UNIT_LENGTH * 3; igHead->Height = UNIT_LENGTH * 3; igHead->Left = startX; igHead->Top = startY; igHead->Parent = Form1; for (int i = 0; i < 7; i++) { //initialize Labels of Slot1, Slot2, ... lbSlot[i] = new TLabel(this); lbSlot[i]->Caption = "Slot " + IntToStr(i+1); lbSlot[i]->Width = UNIT_LENGTH; lbSlot[i]->Height = LABEL_HEIGHT; lbSlot[i]->Parent = Form1; //initialize TImage which hold image of I/O module status igModule[i] = new TImage(this); igModule[i]->Width = UNIT_LENGTH; igModule[i]->Height = UNIT_LENGTH * 3; igModule[i]->Left = startX + UNIT_LENGTH * 3 + GAP_SLOT + (UNIT_LENGTH + GAP_SLOT) * i; igModule[i]->Top = startY; igModule[i]->Picture->Assign(emptyImage->Picture); igModule[i]->Stretch = true; igModule[i]->Parent = Form1; //initialize Labels to hold module names lbmoduleName[i] = new TLabel(this); lbmoduleName[i]->Width = UNIT_LENGTH; lbmoduleName[i]->Height = LABEL_HEIGHT; lbmoduleName[i]->Parent = Form1; } } //--------------------------------------------------------------------------- void __fastcall TForm1::printSlotLabels(TObject *Sender, int x, int y) { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { lbSlot[i - 1]->Left = x + (UNIT_LENGTH + GAP_SLOT) * (i - 1); lbSlot[i - 1]->Top = y; } else { lbSlot[i - 1]->Visible = false; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { slotCount = pac_GetSlotCount(); printSlotLabels(Sender, startX + UNIT_LENGTH * 3 + GAP_SLOT, startY + UNIT_LENGTH * 3 + GAP_MODULE_LABEL); Button1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { HANDLE h = uart_Open(""); //COM1:,115200,N,8,1 int moduleType = 0; byte cmd[64]; byte result[64]; char temp[64]; AnsiString cmd_string = "$00M"; strcpy((char *)cmd, cmd_string.c_str()); for (byte i = 1; i <= slotCount; i++) { ModuleExist[i - 1] = pac_ModuleExists(0, i); //for 8K IO module only if (ModuleExist[i - 1]) { moduleType = pac_GetModuleName(i, temp); ModuleName[i - 1] = AnsiString(temp); if (255 == moduleType) //87K IO modules { pac_ChangeSlot(i); uart_SendCmd(h, cmd, result); // $00M: ask the device name AnsiString strResult = AnsiString((char *)result); ModuleName[i - 1] = strResult.SubString(4, 5); //return format for example: !0087055 } } else { ModuleName[i - 1] = ""; } lbmoduleName[i - 1]->Caption = ModuleName[i - 1]; } uart_Close(h); printModuleNameLabels(Sender, startX + UNIT_LENGTH * 3 + GAP_SLOT, startY - GAP_MODULE_LABEL); showModuleImages(); } //--------------------------------------------------------------------------- void __fastcall TForm1::printModuleNameLabels(TObject *Sender, int x, int y) { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { lbmoduleName[i - 1]->Left = x + (UNIT_LENGTH + GAP_SLOT) * (i - 1); lbmoduleName[i - 1]->Top = y; } else { lbmoduleName[i - 1]->Visible = false; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::showModuleImages() { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { if (ModuleExist[i-1]) { igModule[i-1]->Picture->Assign(moduleImage->Picture); } else { igModule[i-1]->Picture->Assign(emptyImage->Picture); } } else { igModule[i - 1]->Visible = false; } } } //---------------------------------------------------------------------------