⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 PAS
📖 第 1 页 / 共 2 页
字号:
   {Secondly, get port count}
   strTemp := DIPortCnt.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortCount := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;
   if ( ( ( nPortStart + nPortCount ) > DIPortCount ) OR ( nPortCount = 0 ) ) then
   begin
      Application.MessageBox('Port count is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Thirdly, allocate memory for DI data
    pDataBuf := AllocMem ( nPortCount );
    if ( pDataBuf = nil ) then
    begin
      Application.MessageBox('No enough memory to allocate!', 'System Error', MB_OK or MB_ICONERROR );
      Exit;
    end;}

   {Fourthly, read data from the ports}
   lErrCde := AdxDioReadDiPorts( DevHandle, nPortStart, nPortCount, DataBuffer[0] );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR );
      Exit
   end
   else
   begin
      for i := 0 To (nPortCount - 1)  do
      begin
         str := str + IntToHex( DataBuffer[i], 2 ) + ' ';
      end;
      DIData.Text := str;
   end;

end;
{-----------------------------------------------------------------------}
function TMainForm.CharToHex ( c : char ) : BYTE;
var
   str   : string;
begin
   if ( ( c >= '0' ) AND ( c <= '9' ) ) then
   begin
      Result := BYTE(c) - BYTE('0');
      Exit;
   end;

   str := LowerCase( c );
   c := str[1];
   if ( ( c >= 'a' ) AND ( c <= 'f' ) ) then
   begin
      Result := BYTE(c) - BYTE('a') + $0a;
      Exit;
   end;
   Result := 0;
end;
{-----------------------------------------------------------------------}
procedure TMainForm.BtnWriteClick(Sender: TObject);
var
   nPortStart             : Longword;
   nPortCount             : Longword;
   lErrCde                : Longint;
   i                      : Longword;
   j                      : Longword;
   k, nDataCnt            : Longword;
   str                    : string;
   strTemp                : ShortString;
   nTextLen               : Longword;
   High, Low              : BYTE;
begin

   if ( DevHandle = 0 ) then
   begin
      Application.MessageBox('Please open a device firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Firstly, get start port number}
   strTemp := DOPortStart.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortStart := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;
   if ( nPortStart >= DOPortCount ) then
   begin
      Application.MessageBox('Start port is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Secondly, get port count}
   strTemp := DOPortCnt.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortCount := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;
   if ( ( ( nPortStart + nPortCount ) > DOPortCount ) OR ( nPortCount = 0 ) ) then
   begin
      Application.MessageBox('Port count is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Thirdly, allocate memory for DI data
    pDataBuf := AllocMem ( nPortCount );
    if ( pDataBuf = nil ) then
    begin
      Application.MessageBox('No enough memory to allocate!', 'System Error', MB_OK or MB_ICONERROR );
      Exit;
    end;}

   {Fourthly, Get data that user input and convert them to BYTE data}
   {          Note: two characters indicates one number}
   str := DOData.Text;
   nTextLen := Length( str );
   if ( nTextLen = 0 ) then
   begin
      Application.MessageBox('Please input a data firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   nDataCnt := nTextLen div 2;

   i := 1;
   j := 0;
   k := 0;  {Character index}

   While (j < nPortCount) do
   begin
      If ( j <= nDataCnt ) Then
      begin
        { 16 means move the number for four bits left to be high data. 16 = 2^4}
        High := CharToHex(str[i]) * 16;
        k := k + 1;
        If ( (j = nDataCnt) And (nTextLen = k) ) Then {The last charater in the string}
        begin
            Low := 0;
        end
        Else
        begin
            Low := CharToHex(str[i + 1]);
            k := k + 1;
        End;
        DataBuffer[j] := High or Low;
        i := i + 2;
      End
      Else
      begin
        DataBuffer[j] := 0 ;
      End;
      j := j + 1;
   end;

   {Fifthly, write data to the ports}
   lErrCde := AdxDioWriteDoPorts( DevHandle, nPortStart, nPortCount, DataBuffer[0] );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR );
      Exit
   end;

end;
{-----------------------------------------------------------------------}
procedure TMainForm.BtnDOStateClick(Sender: TObject);
var
   nPortStart : Integer;
   nPortCount : Integer;
   lErrCde    : Longint;
   i,j        : Integer;
   str        : string;
   strTemp    : ShortString;

begin

   if ( DevHandle = 0 ) then
   begin
      Application.MessageBox('Please open a device firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Firstly, get start port number}
   strTemp := DOSPortStart.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortStart := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;

   if ( nPortStart >= DOPortCount ) then
   begin
      Application.MessageBox('Start port is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Secondly, get port count}
   strTemp := DOSPortCnt.Text;
   if ( Length(strTemp) = 0 ) then
   begin
      Application.MessageBox('Please input an appropriate number firstly!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;
   try
      nPortCount := StrToInt( strTemp );
   except
      on EConvertError do
      begin
      Application.MessageBox('Please input an appropriate number !', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
      end;
   end;
   if ( ( ( nPortStart + nPortCount ) > DOPortCount ) OR ( nPortCount = 0 ) ) then
   begin
      Application.MessageBox('Port count is not avaliable, please input again!', 'Prompt', MB_OK or MB_ICONINFORMATION );
      Exit;
   end;

   {Thirdly, allocate memory for DI data
    pDataBuf := AllocMem ( nPortCount );
    if ( pDataBuf = nil ) then
    begin
      Application.MessageBox('No enough memory to allocate!', 'System Error', MB_OK or MB_ICONERROR );
      Exit;
    end;}

   {Fourthly, read data from the ports}
   lErrCde := AdxDioGetCurrentDoPortsState( DevHandle, nPortStart, nPortCount, DataBuffer[0] );
   if ( lErrCde <> SUCCESS ) then
   begin
      DRV_GetErrorMessage( lErrCde, @szErrMsg );
      Application.MessageBox( @szErrMsg, 'Driver Error', MB_OK or MB_ICONERROR );
      Exit
   end
   else
   begin
      for i := 0 To (nPortCount - 1) do
      begin
         str := str + IntToHex( DataBuffer[i], 2 ) + ' ';
      end;
      DOSData.Text := str;
   end;
end;

{-----------------------------------------------------------------------}
procedure TMainForm.FormDestroy(Sender: TObject);
begin
   if ( DevHandle <> 0 ) then
   begin
      DRV_DeviceClose( DevHandle );
   end;

end;
{-----------------------------------------------------------------------}

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -