The function is used to connect a device. After the connection is successful, the connection handle is returned.
[in]: Specify the connection options through the parameter, for example: "protocol=RS485,port=COM2,baudrate=38400bps,deviceid=1,timeout=50000, passwd=”; “protocol=TCP,ipaddress=192.168.12.154,port=4370,timeout=4000,passwd=”; To connect a device, the system needs to transfer the device-related connection parameters. protocol indicates the protocol used for communication. At present, RS485 and TCP can be used. port: Communication port of the device. For example, if the RS485 protocol is used, you can set port to COM1: If the TCP is used, the default port is 4370 unless otherwise noted. deviceid: Device ID used by the serial port. baudrate: Baud rate used for the communication of the communication of the serial port. ipaddress: IP address of the related device for TCP/IP communication. timeout: Timeout time of the connection (unit: ms)If the network connection is in poor condition, you should set the parameter to a larger value. Usually, timeout=5000 (5 seconds) can meet the basic network needs. When the query result contains the error code of -2, you should set timeout to a larger value, for example, timeout=20000 (20 seconds). passwd: Connection password of the communication. If the parameter value is null, it indicates that no password is used. (Note: The connection parameters are case-sensitive)
params = “protocol=TCP,ipaddress=192.168.12.154,port=4370,timeout=4000,passwd=” self.commpro = windll.LoadLibrary("plcommpro.dll") constr = create_string_buffer(params) self.hcommpro = self.commpro.Connect(constr)
params = “protocol=TCP,ipaddress=192.168.12.154,port=4370,timeout=2000,passwd=” ; IntPtr h = Connect(params);
IntPtr h = IntPtr.Zero; [DllImport(@"C:\Windows\SysWOW64\plcommpro.dll", EntryPoint = "Connect")] public static extern IntPtr Connect(string Parameters); //public static extern IntPtr Connect(string Parameters); [DllImport("plcommpro.dll", EntryPoint = "PullLastError")] public static extern int PullLastError(); private void Connect_Pull(string str= "protocol=TCP,ipaddress=10.0.0.44,port=4370,timeout=2000,passwd=") { int ret = 0; // Error ID number // Cursor = Cursors.WaitCursor; if (IntPtr.Zero == h) { h = Connect(str); if (h != IntPtr.Zero) { MessageBox.Show("Connect device succeed!"); } else { ret = PullLastError(); MessageBox.Show("Connect device Failed! The error id is: " + ret); } } }
Dim h As IntPtr = IntPtr.Zero <DllImport("C:\Windows\SysWOW64\plcommpro.dll", EntryPoint:="Connect")> Public Shared Function Connect(Parameters As String) As IntPtr End Function 'public static extern IntPtr Connect(string Parameters); <DllImport("plcommpro.dll", EntryPoint:="PullLastError")> Public Shared Function PullLastError() As Integer End Function Private Sub Connect_Pull(Optional str As String = "protocol=TCP,ipaddress=10.0.0.44,port=4370,timeout=2000,passwd=") Dim ret As Integer = 0 ' Error ID number ' Cursor = Cursors.WaitCursor; If IntPtr.Zero = h Then h = Connect(str) If h <> IntPtr.Zero Then MessageBox.Show("Connect device succeed!") Else ret = PullLastError() MessageBox.Show("Connect device Failed! The error id is: " + ret) End If End If End Sub