Take a look here:
Originally Posted by jmullan99
Basically you create a serial port object passing the port name (don't stip anything off, on windows it might be COM1 on linux it might be /dev/ttys0/). Then you Open the port and Read and Write to it.
That was quick, thanks for that. I now offer up com port selection, and as a test send stuff to the Arduino. The "stuff" is wrong at this point, but hey, it's communicating! Now to get the protocol right.
I have to revisit the com port names though. I try using "COM6" (in the returned list of port names) but when selecting it seems it prefers just 6 (as integer).
I have spent a few hours trying to figure this out. :nutz: My code seems to be coming along. However, I am tossed over this com port thing.
Okay, so this is the scenario. I have 2 valid com ports. COM5 and COM6
COM5 = LCD. COM6 = Arduino.
When I open OM (0.9), my plugin will (by design) start with either no valid com port selected in settings, or last valid com port (should be COM6).
Case #1: Start with no com port selected
My code will determine that there is currently no valid com port selected. User selects from list (first item is COM5). Plugin determines that it is not valid (because in use by LCD). This is good. Select next port COM6, it too is not valid because of exception "Access to the port 'COM6' is denied.". From here, if I go back to COM5 (still invalid) and then back to COM6 again, COM6 is now valid. WTF (sorry for that).
Case #2: Basically continued from Case #1, except that COM6 is now remembered by settings because it was valid. Exit and start OM again and my loadsettings() will reset the previously remembered choice (by design) because it appears no longer valid because of "Access to the port 'COM6' is denied.". Again if I cycle to COM5 and back to COM6, it is okay again.
I am stymied because if it is valid when I switch between ports, why is it not valid when I first instantiate a serial port?
I've come a long way in a week learning VS, .NET and OM plugins, but this little bug is giving me a headache.
My VB project is available for download for anyone interested and checking it out. I update it approx once per day if there is any real progress. PM me if you want the URL.
Update: All I did was unplug my Arduino, re-insert, now it's COM6 doesn't exist.
Problem is, it does. It gets found, it's added to the list, it exists in Registry, but when I connect, the exception is thrown that it doesn't exist.
This is really confusing.
Well, after a LOT of searching, it's at least not my fault!
Apparently, some virtual ports (I guess like the Arduino creates) do NOT like being unplugged while in use, under .NET < 4.0 even if it is listed as an available port, the exception of "does not exist" is still thrown.
It is apparently fixed in 4.0 but if anyone knows of a relatively simple code fix for this relative newby to implement, by all means my ears are open.
The solution is a reboot. Then it is again addressable.
I don't know how this will affect users in the long run. And I have not yet tested it in hibernation. But I can proceed for now.
Have you tried to disable/enable the usb controller instead of a reboot to remedy the problem?
I don't know if this will help, just a suggestion since this is something that might be controllable from software.
Thanks yet again. Heezer7 is helping me outside of the forum. I'll post results when we get that far.
Hi, just a bit of feedback on com ports. Although I have seen the finger of blame pointed at .Net, I am not so sure. Unplugging a USB device from an open active port has always locked the port in VB and this behaviour has not changed with any version of .NET.
From what I am seeing it appears to be, in some cases, that the virtual com driver locks. I have never had to reboot the PC to solve this issue. However I have to close the application that was using the port to enable access to the port. Trying to reinitialise the port in any way in software obviously fails if the driver has hung. You would need to find a way in software to access the associated virtual driver at OS level and unload/reload it.
Sometimes have to unplug and reconnect the USB device after the program has been closed to allow the OS to unload and reload the driver – also seems to depend on the USB device and associated driver.
I have 6 or 7 USB devices in the CAR PC and this behaviour has never caused a problem. I don’t disconnect a USB device that is open and accessed by an app. Every commercial app that I have tried so far behaves in the same way if the accessed USB device is unplugged.
Hibernation/sleep/suspend cause’s no problems in any device I have, however I have set USB properties to NOT allow the OS/drivers to power down the ports.