//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "PACSDK.h"

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
HANDLE hPort ;
void __fastcall TForm1::btnOpenComClick(TObject *Sender)
{
        char strErr[32] ;
        hPort=uart_Open(etComFormat->Text.c_str() );
        if(hPort == INVALID_HANDLE_VALUE)
        {
                pac_GetErrorMessage(pac_GetLastError(), strErr);
                MessageBox(NULL,strErr,"Open COM error",MB_OK);
        }
        else
        {
                btnOpenCom->Enabled =false;
                btnCloseCom->Enabled =true;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCloseComClick(TObject *Sender)
{
        uart_Close(hPort);
        btnOpenCom->Enabled =true;
        btnCloseCom->Enabled =false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnReadDIClick(TObject *Sender)
{
        char strErr[32] ;
        unsigned long DO_Value,DI_Value;
        bool bRet=pac_ReadDIO(hPort,PAC_REMOTE_IO(StrToInt(etAddr->Text)),StrToInt(etDITotalChs->Text ),StrToInt(etDOTotalChs->Text ),&DI_Value,&DO_Value);
        if(bRet!=false)
        {
                etDIValue->Text=IntToHex((int)DI_Value,StrToInt(etDITotalChs->Text )/4);
        }
        else
        {
                pac_GetErrorMessage(pac_GetLastError(), strErr);
                MessageBox(NULL,strErr,"Read DI error",MB_OK);
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnWriteDOClick(TObject *Sender)
{
        char strErr[32] ;
        unsigned long DO_Value=StrToInt("$"+etDOValue->Text );
        bool bRet=pac_WriteDO(hPort,PAC_REMOTE_IO(StrToInt(etAddr->Text)),StrToInt(etDOTotalChs->Text ), DO_Value);
        if(bRet==false)
        {
                pac_GetErrorMessage(pac_GetLastError(), strErr);
                MessageBox(NULL,strErr,"Write DO error",MB_OK);
        }
}
//---------------------------------------------------------------------------
