How to find to find the actual port used by device?


capstone

Recommended Posts

The application we are developing will be used on different computers, therefore, we don't know which port is used when the device is plugged in. Is there a sequence for telling DAQFactory to set the port to whichever port that the device is connected to??

I know about

Device.MyDevice.Port = 5

but this isn't enough, since we don't know if the port number is 5 or what...

Thanks!

Link to comment
Share on other sites

No there is no built in way to do that. In order to do it, you'd have to try and initialize all ports, starting with 1, and see if the device is there, and if it times out, move to the next device. Alas, it would probably take 10 seconds or so to do this. Why don't you know what the port # is?

Link to comment
Share on other sites

Well, I never tried it, but I guess you would just cycle through the comm ports and try and initialize each. If it succeeds, you then have to see if its the correct port, or another port, say an internal modem. You could presumably try sending a command out and see if you then get the correct response. Lets say your device in DAQFactory is called "mydevice", and its a modbus device with ID 1 and a valid holding register at address 0 / 40,001. The code might look like this:

for (private i = 1, i < 20, i++)
   try
	  device.mydevice.port = i
	  device.mydevice.initcomm()
	  // if got here then inited, try communicating:
	  device.mydevice.readholdingu16(1,0,1)
	  // if got here, then a modbus read was successful.  We found the port!
	  break
   catch
   endcatch
endfor
if (i < 20)
   // found the port, its equal to i
else
   // didn't find it, do something else
endif

Link to comment
Share on other sites

Great! that code works... Thanks! :) The only thing that's annoying is that alert we get in cmd/alert window with orange color for the other ports saying the init failed... Is there a way to stop that from alerting us? ignore would be the same as catch which we already have... hummm...

The other question I have on this subject is this:

let's say we are now connected to port 6, and somebody pulls out the Serial to USB cable from the computer by mistake. Is there a way to keep checking the ports connectivity in a while loop to find out if we are still connected? I know initing doesn't work, cause looks like after the first init which is successful, the next init on the same port, disconnects it.

The reason I'm asking is cause we work on real time monitoring so a component shows the status of the connection, and if such a cable/hardware disconnect happens, that button is still saying connected, even though we are not anymore!

Thanks again!

Link to comment
Share on other sites

Yeah, now you see why I dislike USB-serial converters (and USB in general for industrial apps). You can do anything you want, just script up the logic. The problem is coming up with all the ways its going to fail. There is also the issue that if they plug it back into a different USB port that it will mount on a different com port. Why not use an ethernet based converter instead, or perhaps buy a real industrial laptop that has a serial port built in? If your app requires the sort of reliability your are describing, than USB is probably not the way to go.

Link to comment
Share on other sites

Archived

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