📄 cup_connection.pas
字号:
unit Cup_Connection;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, OptoSnapIoMemMapXLib_TLB, StdCtrls,ExtCtrls,Winsock,
Mask,IdGlobal;
type
SIOMM_AnaOutSingleMod = packed record
fValue:array[0..11] of Single;
end;
SIOMM_AnaInMod012 = packed record
fValue:array[0..11] of Single;
end;
AnaIn_Result = packed record
pAnaInMod012Data:SIOMM_AnaInMod012;
nResult:Integer;
end;
DigIn_Result = packed record
pDigInMod_N_States:Integer;
nResult:Integer;
end;
{AnaOut_Result = packed record
AnaOutModSiData:SIOMM_AnaOutSingleMod;
nResult:Integer;
end;}
TForm1 = class(TForm)
Label1: TLabel;
GroupBox1: TGroupBox;
Label2: TLabel;
IP_Address: TEdit;
Connect: TButton;
Disconnect: TButton;
Label3: TLabel;
Port_Number: TEdit;
GroupBox2: TGroupBox;
Label4: TLabel;
Error_Message: TMemo;
Timer1: TTimer;
GroupBox3: TGroupBox;
AnaModule0_1: TEdit;
Label5: TLabel;
OptoSnapIoMemMapX1: TOptoSnapIoMemMapX;
AnaModule0_2: TEdit;
Label6: TLabel;
GroupBox4: TGroupBox;
Label7: TLabel;
AnaModule6_1: TEdit;
AnaModule6_1_set: TButton;
Label8: TLabel;
AnaModule6_2: TEdit;
AnaModule6_2_set: TButton;
GroupBox5: TGroupBox;
HDD32_DigInMod8_States: TEdit;
Label9: TLabel;
Label10: TLabel;
HDD32_DigInMod8_0_State: TEdit;
GroupBox6: TGroupBox;
Label11: TLabel;
ScratchbitRead: TEdit;
Label12: TLabel;
ScratchRead_intN: TEdit;
GroupBox7: TGroupBox;
Label13: TLabel;
ScratchWrite_bitN_set: TButton;
CcratchWrite_bitN_clear: TButton;
Label14: TLabel;
ScratchbitWrite: TEdit;
Label15: TLabel;
ScratchbitWrite_set: TButton;
Label16: TLabel;
ScratchWrite_IntN: TEdit;
ScratchWrite_IntN_set: TButton;
Label17: TLabel;
Str1: TEdit;
ScratchWrite_CNStr_set: TButton;
GroupBox8: TGroupBox;
DigModule4: TEdit;
Label18: TLabel;
GroupBox9: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Label19: TLabel;
Label20: TLabel;
procedure ConnectClick(Sender: TObject);
procedure DisconnectClick(Sender: TObject);
//procedure DataProcessClick(Sender: TObject);
procedure HandleError(nResult: Integer);
function GetResultAsString(nResult: Integer): String;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
//function GetAna_inModule012Value(pAnaInMod012Data:SIOMM_AnaInMod012):Integer;
function GetAna_inModule012Value():AnaIn_Result;
function GetDig_inModule_N_States(ModuleNumber:Integer;half_Bank:boolean):DigIn_Result;
procedure Timer1Timer(Sender: TObject);
function PtConfigure():Integer;
//function SetAna_OutModule_N_Value(ModuleNumber:Integer):AnaOut_Result;
function SetAna_OutModule_N_Value(ModuleNumber:Integer;AnaOutModSiData:SIOMM_AnaOutSingleMod):Integer;
procedure AnaModule6_1_setClick(Sender: TObject);
procedure AnaModule6_2_setClick(Sender: TObject);
procedure ScratchWrite_bitN_setClick(Sender: TObject);
procedure CcratchWrite_bitN_clearClick(Sender: TObject);
procedure ScratchbitWrite_setClick(Sender: TObject);
procedure ScratchWrite_IntN_setClick(Sender: TObject);
procedure ScratchWrite_CNStr_setClick(Sender: TObject);
function IntToBinaryStr(TheVal: Integer): string;
function GetDig_inModuleStates(ModuleNumber:Integer;half_Bank:boolean;DigInResultData:DigIn_Result):AnsiString;
private
{ Private declarations }
//function GetDig_inModuleStates(ModuleNumber:longint;half_Bank:boolean;nStatePts:longint):AnsiString;
public
{ Public declarations }
end;
var
Form1: TForm1;
const SIOMM_OK = 1;
const SIOMM_ERROR = -1;
const SIOMM_TIME_OUT = -2;
const SIOMM_ERROR_NO_SOCKETS = -3; // Unable to access socket interface
const SIOMM_ERROR_CREATING_SOCKET = -4;
const SIOMM_ERROR_CONNECTING_SOCKET = -5;
const SIOMM_ERROR_RESPONSE_BAD = -6;
const SIOMM_ERROR_NOT_CONNECTED_YET = -7;
const SIOMM_ERROR_OUT_OF_MEMORY = -8;
const SIOMM_ERROR_NOT_CONNECTED = -9;
const SIOMM_ERROR_STREAM_TYPE_BAD = -10;
// Error values generated by the I/O brain itself
const SIOMM_BRAIN_ERROR_UNDEFINED_CMD = 57345;
const SIOMM_BRAIN_ERROR_INVALID_PT_TYPE = 57346;
const SIOMM_BRAIN_ERROR_INVALID_FLOAT = 57347;
const SIOMM_BRAIN_ERROR_PUC_EXPECTED = 57348;
const SIOMM_BRAIN_ERROR_INVALID_ADDRESS = 57349;
const SIOMM_BRAIN_ERROR_INVALID_CMD_LENGTH = 57350;
const SIOMM_BRAIN_ERROR_RESERVED = 57351;
const SIOMM_BRAIN_ERROR_BUSY = 57352;
const SIOMM_BRAIN_ERROR_CANT_ERASE_FLASH = 57353;
const SIOMM_BRAIN_ERROR_CANT_PROG_FLASH = 57354;
const SIOMM_BRAIN_ERROR_IMAGE_TOO_SMALL = 57355;
const SIOMM_BRAIN_ERROR_IMAGE_CRC_MISMATCH = 57356;
const SIOMM_BRAIN_ERROR_IMAGE_LEN_MISMATCH = 57357;
const SIOMM_BRAIN_ERROR_FEATURE_NOT_IMPL = 57358;
const SIOMM_BRAIN_ERROR_WATCHDOG_TIMEOUT = 57359;
// The types of socket connections
const SIOMM_TCP = 1;
const SIOMM_UDP = 2;
// Error values generated by the users
const User_Error1 = -11;
implementation
uses DataProcess;
{$R *.dfm}
//--------------------对模块进行初始化,该程序没用到--------------------
function TForm1.PtConfigure():Integer;
var nResult:Integer;
i:Integer;
begin
//Configure the AI Channels —Module 0,1,2
for i:=0 to 3 do
nResult:= OptoSnapIoMemMapX1.SetAnaPtConfiguration(i,11,0,0,100,-100);
//only configure module 0
//High Density digital point donot need configure.—Module 3,4
//Configure the A0 Channels —Module 5,6,7
for i:=24 to 27 do
nResult:= OptoSnapIoMemMapX1.SetAnaPtConfiguration(i,167,0,0,100,-100);
result:=nResult;
exit;
end;
//------------------------------建立连接--------------------------------
procedure TForm1.ConnectClick(Sender: TObject);
var
nResult: Integer;
StartTime:Dword;
begin
// Attempt to open the Snap I/O..
nResult := OptoSnapIoMemMapX1.OpenEnet2(WideString(IP_Address.Text), StrToInt(Port_Number.Text),100000,1,SIOMM_TCP); //ToInt是指取整
// Check the result
if (nResult = SIOMM_OK) then
begin
Form1.Cursor := crHourglass; //mouse change to the hourglass
Form1.Update(); //refresh form1
// Sleep for a little while
Sleep(250);
// Keep calling IsOpenDone() until we succeed, time-out, or error
nResult := OptoSnapIoMemMapX1.IsOpenDone();
StartTime := GetTickCount(); //用于获取自windows启动以来经历的时间长度(毫秒)
while ((nResult <> SIOMM_OK)And((GetTickCount()-StartTime)<3000)) do
begin
// let other tasks get some CPU
Sleep(250);
// Check the status of the open operation
nResult := OptoSnapIoMemMapX1.IsOpenDone(); // keep trying
end;
// Reset the cursor to the arrow
Form1.Cursor := crArrow;
if (SIOMM_OK = nResult) Then
begin
Error_Message.Text:= 'Connection OK !';
Error_Message.Color:= $FFFFFF ;
//DataProcess.Enabled:= true;
end
else
begin
Error_Message.Text:= 'Connection OverTime !';
Error_Message.Color:= $FF ;
HandleError(nResult);
end
end
// Check the result
else HandleError(nResult);
end;
//---------------------------关断连接--------------------------------
procedure TForm1.DisconnectClick(Sender: TObject);
var
nResult: Integer;
begin
nResult := OptoSnapIoMemMapX1.Close();
if (nResult = SIOMM_OK) then
begin
Error_Message.Text:='Disconnection ok!';
Error_Message.Color:= $FFFFFF ;
// DataProcess.Enabled:=false;
end
else
HandleError(nResult);
end;
//----------------------------------------------------------------------
{procedure TForm1.DataProcessClick(Sender: TObject);
begin
Form2.Show;
end;}
//---------------------------------报错---------------------------------
procedure TForm1.HandleError(nResult: Integer);
begin
// Update the error message
Error_Message.Text := GetResultAsString(nResult);
if (nResult = SIOMM_OK) then
// Set background to white
Error_Message.Color := $FFFFFF
else
begin
// Set background to red
Error_Message.Color := $FF;
// shutdown everything on a bad error
if (nResult <> SIOMM_ERROR_NOT_CONNECTED_YET) then
begin
OptoSnapIoMemMapX1.Close;
end;
end;
end;
//--------------------------------将结果转换成字符串-------------------------
function TForm1.GetResultAsString(nResult: Integer): String;
begin
If (nResult = SIOMM_OK) Then
begin
Result := 'Ok';
exit;
end
else if (nResult = SIOMM_ERROR) Then
begin
Result := 'General error';
exit;
end
else if (nResult = SIOMM_TIME_OUT) Then
begin
Result := 'Timeout';
exit;
end
else if (nResult = SIOMM_ERROR_NO_SOCKETS) Then
begin
Result := 'No sockets';
exit;
end
else if (nResult = SIOMM_ERROR_CREATING_SOCKET) Then
begin
Result := 'Could not create socket to Snap I/O';
exit;
end
else if (nResult = SIOMM_ERROR_CONNECTING_SOCKET) Then
begin
//GetResultAsString := 'Could not connect socket to Snap I/O'
Result := 'Could not connect socket to Snap I/O';
exit;
end
else if (nResult = SIOMM_ERROR_RESPONSE_BAD) Then
begin
Result := 'Bad response';
exit;
end
else if (nResult = SIOMM_ERROR_NOT_CONNECTED_YET) Then
begin
Result := 'Not connected yet';
exit;
end
else if (nResult = SIOMM_ERROR_OUT_OF_MEMORY) Then
begin
Result := 'ActiveX out of memory';
exit;
end
else if (nResult = SIOMM_ERROR_NOT_CONNECTED) Then
begin
Result := 'Not connected';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_UNDEFINED_CMD) Then
begin
Result := 'Snap I/O Error: Undefined Command';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_INVALID_PT_TYPE) Then
begin
Result := 'Snap I/O Error: Invalid point type';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_INVALID_FLOAT) Then
begin
Result := 'Snap I/O Error: Invalid float';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_PUC_EXPECTED) Then
begin
Result := 'Snap I/O Error: Powerup Clear expected';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_INVALID_ADDRESS) Then
begin
Result := 'Snap I/O Error: Invalid memory address';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_INVALID_CMD_LENGTH) Then
begin
Result := 'Snap I/O Error: Invalid command length';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_RESERVED) Then
begin
Result := 'Snap I/O Error: Reserved';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_BUSY) Then
begin
Result := 'Snap I/O Error: Busy';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_CANT_ERASE_FLASH) Then
begin
Result := 'Snap I/O Error: Cannot erase flash';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_CANT_PROG_FLASH) Then
begin
Result := 'Snap I/O Error: Cannot program flash';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_IMAGE_TOO_SMALL) Then
begin
Result := 'Snap I/O Error: Downloaded imaged too small';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_IMAGE_CRC_MISMATCH) Then
begin
Result := 'Snap I/O Error: Image CRC mismatch';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_IMAGE_LEN_MISMATCH) Then
begin
Result := 'Snap I/O Error: Image length mismatch';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_FEATURE_NOT_IMPL) Then
begin
Result := 'Snap I/O Error: Feature not implement';
exit;
end
else if (nResult = SIOMM_BRAIN_ERROR_WATCHDOG_TIMEOUT) Then
begin
Result := 'Snap I/O Error: Watchdog timeout';
exit;
end
else if (nResult = User_Error1) Then
begin
Result := 'Snap I/O Error: Invalid Digital Module number';
exit;
end
else begin
Result := 'Unknown error';
exit;
end;
end; // GetResultAsString()
//---------------------------------关闭窗口-------------------------------
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
OptoSnapIoMemMapX1.Close;
Error_Message.Text:= 'Disconnection !';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -