ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems connecting to a remote OPC server in write mode using CWDataSocket control

Hi.

I have been struggling with an odd feature when trying to connect to a remote OPC server using CWDataSocket control, though I'm not sure if it's a CWDataSocket related problem. But anyway, the following error occurs: "Active: Can't write data." Error number: "-2147467259". Now, when I connect to a local OPC server
and write some data and then again connect to the remote server, no errors occure and reading and writing work fine. The same error occurs when I try the same in LabVIEW. What could be the problem?

No errors occure when I connect to the server with some other OPC client application.

Thanks in advance.
- Asko -
0 Kudos
Message 1 of 6
(7,176 Views)
Hello Asko


Before trying to write data, check to see the connection status and make sure the connection has gone through. You can wait on the Status property to check the connection state. Datasocket is asynchronous so Connect or ConnectTo actaully return before the connection actaully goes through.

I hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 6
(7,176 Views)
It seems that the connection goes through because the status is active (cwdsConnectionActive). I open the connection like this:writeok = (dsWrite.LastError = 0)


' Connect
dsWrite.ConnectTo txtUrl.Text, cwdsWrite

The error occures during the open attempt. The URL I pass to the ConnectTo method is valid. I assume, that once the connection is established, some default value is written or am I right? If so, is there a way to prevent this initial writing?

The weird part here is that after I have connected to a local server and written some values, the connection to the remote server works like a charm.
0 Kudos
Message 3 of 6
(7,176 Views)
There was a typo in my last message... The connection example should have been like this:

' Connect
dsWrite.ConnectTo txtUrl.Text, cwdsWrite
writeok = (dsWrite.LastError = 0)

- Asko -
0 Kudos
Message 4 of 6
(7,176 Views)
The ConnectTo operation (as are all DataSocket operations) is asynchronous. In order for it to complete, you must process Windows messages after calling it and before calling another operation. Is it possible that you are trying to write data before the connection operation is complete?

One easy way to test this would be to put the ConnectTo and the Write in separate control callbacks, cause ConnectTo to be called, wait a while, then cause the Write to be called. Alternatively, you could do something like the following:

dsWrite.ConnectTo txtUrl.Text, cwdsWrite
While Not ((dsWrite.Status = cwdsConnectionActive) Or (dsWrite.Status = cwdsConnectionIdle))
DoEvents
Wend

If this is the problem, then you'll probably want to go with the
second option. In a production system, you should include a timeout in the While loop so that if the connection actually cannot be made, you are not stuck in an infinite loop.
0 Kudos
Message 5 of 6
(7,176 Views)
I solved the problem by setting the data to write before opening the connection:

dsWrite.Data.Value = myVal
dsWrite.ConnectTo txtUrl.Text, cwdsWrite

The data is written once while the connection is established.

- Asko -
0 Kudos
Message 6 of 6
(7,176 Views)