DAQFactory acts as a Server


Recommended Posts

Yes, but it can only handle one connection on each IP port, so isn't appropriate if you need multiple devices to connect to DAQFactory. It is also a little harder to write protocols for servers because of its async nature. You create a server the same way you create a client: go to Quick - Device configuration, select New Serial /Ethernet, then instead of clicking New Ethernet Client, click New Ethernet Server.

Link to comment
Share on other sites

Probably, as long as two devices don't try this procedure at the same time. A better pattern, however, is to use UDP discovery. This is where the system goes out and sends a UDP ping to all visible devices, and those that are listening respond with their IP address. I would use this method instead, writing the UDP discovery part in normal C or C++ as a DLL, then call it using extern().

Link to comment
Share on other sites

  • 2 weeks later...

If you control the client code, I'd just add a query to whatever protocol you are implementing. If not, and you are creating your server in C code, there is a function for retrieving the IP of the attached client on a socket. I don't know what it is off hand and C code like this is a little outside the realm of this forum.

Link to comment
Share on other sites

I created a dll file, which has a function called AcceptConnection. The nature of this function is to perform the function accept() of C/C++. In DAQ workspace, I created a sequence (with name: DAQServer) to use the functions in this dll file. When sequence execute AcceptConnection function, DAQ is hangs, does not work. I use TaskManager and see that, DAQ only use 1%CPU and little RAM. If I use a client to connect to DAQ, it still success. I can send/receive data. I still try function StartThread to run sequence (DAQServer) in other thread but DAQ still does not work normal.

Link to comment
Share on other sites

I create a sequence with name DAQServer as below:

function DAQServer(global port)

extern("DAQServer.dll","short InitServer(short)","InitDAQServer","stdcall")

extern("DAQServer.dll","short AcceptConnection(short)","DAQAcceptConnect","stdcall")

global retSock

global retAccept

retSock = InitDAQServer(port)

if (retSock == -1)

system.messagebox("cannot init socket")

else

while (1)

retAccept = DAQAcceptConnect(retSock)

endwhile

endif

continue, I create a sequence with name "main" as below:

private string strStatus

global port = 6000

StartThread("DAQServer(port)","MyServer",1)

while (1)

strStatus = GetThreadStatus("MyServer")

? strStatus

delay(10)

endwhile

In function AcceptConnection (in file dll), I accept new incoming connection and write IP address to text file. With all clients connect to DAQ, I always receive IP in text file. DAQ only use 1%CPU but DAQ's status is "not responding" and I cann't do anything on DAQ, I still don't see value of "strStatus" in "Command/Alert window". But, the clients still connect to DAQ and send/receive data.

Link to comment
Share on other sites

CPU usage isn't the best indication of hanging. You have infinite loops in both your sequences with no delays built in. If Main is run in the main DAQFactory thread (by doing Main() from a button for example, or the command/alert window), it will cause DAQFactory to appear hung because the UI never gets the CPU back. Its stuck in your second loop. So:

1) start Main on its own thread by starting the sequence instead of calling it

2) add at least a minimal delay in every loop: delay(0.05) for example

Link to comment
Share on other sites

accept() is function block. Program will block until new incoming connection. So, the loop "while (1)" isn't run continuous. If I run sequence in debug mode, DAQ status is "not responding" when run command "DAQAcceptConnection" but it will normal when have new incoming connection.

I try with delay() command but it still doesn't work normal.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.