USB to Serial Comm Device Change Event

I am working on a project to rewrite some codes currently working on Windows platform then make it to work on Mac OS X. One senario is to detect the USB to Serial port device change event. So when the user plugs in a usb to serial device, the program will detect the device change and then base on PID/VID combination to decide open the COM port or not. When user unplug it, close the COM port. On windows platform the following codes do the job,

Private WM_DEVICECHANGE As Integer = &H219
    Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
        DBT_CONFIGCHANGECANCELED = &H19
        DBT_CONFIGCHANGED = &H18
        DBT_CUSTOMEVENT = &H8006
        DBT_DEVICEARRIVAL = &H8000
        DBT_DEVICEQUERYREMOVE = &H8001
        DBT_DEVICEQUERYREMOVEFAILED = &H8002
        DBT_DEVICEREMOVECOMPLETE = &H8004
        DBT_DEVICEREMOVEPENDING = &H8003
        DBT_DEVICETYPESPECIFIC = &H8005
        DBT_DEVNODES_CHANGED = &H7
        DBT_QUERYCHANGECONFIG = &H17
        DBT_USERDEFINED = &HFFFF
    End Enum

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            Select Case m.WParam
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
                    ChkBox_USB_Detect.CheckState = CheckState.Checked  'here is a checkbox set the intial check state as intermidiate, the whole checkbox is working as a flag
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
                    ChkBox_USB_Detect.CheckState = CheckState.Unchecked
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

Can Xojo do the same job on Mac OS?

no one? any luck? @Paul Lefebvre

You could try the MonkeyBread plugin MacUSBNoticationMBS, it may do the job, but it does have a caveat…
Notes: Written for a client and probably not yet universally useable, so it may not work with your device

URL is…
http://www.monkeybreadsoftware.de/realbasic/plugin-usb.shtml

[quote=44846:@Chris Carter]You could try the MonkeyBread plugin MacUSBNoticationMBS, it may do the job, but it does have a caveat…
Notes: Written for a client and probably not yet universally useable, so it may not work with your device

URL is…
http://www.monkeybreadsoftware.de/realbasic/plugin-usb.shtml[/quote]
Thanks, Chris. Just quick question, I do noticed there are planty of MonkeyBread plugins for Xojo. What’s that? Is that free plugins? If I use those plugins, after I build my program and generate an installer, do I need to provide those plugins in my program, just like you have to include .NET framework in the installer, in case the end user doesn’t intall .NET framework?

[quote=44846:@Chris Carter]You could try the MonkeyBread plugin MacUSBNoticationMBS, it may do the job, but it does have a caveat…
Notes: Written for a client and probably not yet universally useable, so it may not work with your device

URL is…
http://www.monkeybreadsoftware.de/realbasic/plugin-usb.shtml[/quote]
Chris,
I just tried to use the MonkeyBread plugins in Windows, I used the class WinUSBDeviceMBS in order to filt specific USB to Serial port I want to open, then, I have a problem, I can’t find a way to match the USB to serial port to the port number.

For example, I have a usb to serial port device use FTDI chip, so I can filt it out thru WinUSBDeviceMBS. I can also read the COM port number thru Xojo own class, Serial.serialport.Name. But I can’t find a method match those two.

Do you have any idea to solve that problem?

The USB COM ports I have used all have a FriendlyName associated with them which typicaly has (COMn) on the end.
This is held in the registry, but the proper way to get it is with some API calls. ( SetupDi… and CM_Get_… calls )
( This particular property is grabbed using a call to SetupDiGetDeviceRegistryProperty using the SPDRP_FRIENDLYNAME flag, but getting th that point is not trivial )
My own code for this is is embedded in a larger project which scans for lots of device types… but I can extract example code from the existing project, given a little time.

It is always possible that Christian (of MBS) will add this FriendlyName field to his WinUSBDeviceMBS class.

I’ve shoved some windows device information grabbing code into a project, which you can find at…

http://www.cmas-net.co.uk/rslug

Specificaly, it’s…

http://www.cmas-net.co.uk/rslug/Eg-5-DeviceList.rbp

It can grab information for lots of device types, I’ve put comments into the form’s open event to say how to get just the USB COM port information you are after.

[quote=45276:@Chris Carter]I’ve shoved some windows device information grabbing code into a project, which you can find at…

http://www.cmas-net.co.uk/rslug

Specificaly, it’s…

http://www.cmas-net.co.uk/rslug/Eg-5-DeviceList.rbp

It can grab information for lots of device types, I’ve put comments into the form’s open event to say how to get just the USB COM port information you are after.[/quote]
Many thanks to provid the source code to catch up the USB device. I read the code, here is the question I have.

  1. In your codes, all the WinAPI_ are system functions right? I mean you don’t have to wirte your own codes, just call it and you will get the info you needed right?
  2. In VB.NET I use WMI to reach to the PnPEntity, it’s basically a data base script like. So go back to the main theme on my first topic, do you have any idea, how that works on the Mac OS?
  3. Further question is did Mac OS provide some kind of API let you scan all devices? How does that work?

I know it’s a bit of messy to discuss this from Mac OS then Windows then go back to Mac OS, I guess it’s a good case study for me to learn about both platforms to deal with COM port communication. Again, thank you very much for your answer. That’s very helpful.

WinAPI_ prefix is just my own coding convention for Windows API related funtions and constants - these refer to windows system functions which are built in. I tested this under Windows XP…
If you run the project it should work and fill a listbox with a tree view of device information.

I haven’t’ done a lot of MAC specific stuff, but if I plug a USB->Serial device in to my Mac Mini and then use the RB/Xojo serial class to look at COM ports, the USB one(s) have names starting usbmodem

I also see that MBS plugin Mac USB Devices shows some useful information

[quote=45290:@Chris Carter]WinAPI_ prefix is just my own coding convention for Windows API related funtions and constants - these refer to windows system functions which are built in. I tested this under Windows XP…
If you run the project it should work and fill a listbox with a tree view of device information.

I haven’t’ done a lot of MAC specific stuff, but if I plug a USB->Serial device in to my Mac Mini and then use the RB/Xojo serial class to look at COM ports, the USB one(s) have names starting usbmodem

I also see that MBS plugin Mac USB Devices shows some useful information[/quote]
Yep, I run it on my Windows 7 laptop, it runs just like your description. So, all the WinAPI_ is comming from setupapi.dll? How does the Windows API function hook up?

The actual windows function name is in the Alias field of the definition, the .dll for each is in the Lib field

Thanks, Chris!

[quote=45276:@Chris Carter]I’ve shoved some windows device information grabbing code into a project, which you can find at…

http://www.cmas-net.co.uk/rslug

Specificaly, it’s…

http://www.cmas-net.co.uk/rslug/Eg-5-DeviceList.rbp

It can grab information for lots of device types, I’ve put comments into the form’s open event to say how to get just the USB COM port information you are after.[/quote]

@Chris Carter Thanks for the examples in this. I actually learned a few new things like using external methods. :slight_smile:

Glad to be of some help :slight_smile:

@Chris Carter What is the advantage of using the external methods for dynamic library calls? I’ve just been using regular methods for mine because I didn’t know about external methods.

@Chris Carter I am trying to work with the setupapi.dll to disable/enable a usb hid device. Have you done any work with the SetupDiChangeState function?

I normally use regular methods as I often want to wrap a bit of other work around calls.

One advantage I see of external methods is that it is probably more efficient as the routine can be called directly, rather than via the overhead of invoking another method.

Sorry, no, I haven’t done anything with that .dll

@Chris Carter

Thanks for the insights into that. I appreciate it. :slight_smile:

Oh ok, I thought you had worked with setupapi.dll because there are several calls to that dll in that example code you posted.

[quote=312328:@Geoff Haynes]@Chris Carter

Thanks for the insights into that. I appreciate it. :slight_smile:

Oh ok, I thought you had worked with setupapi.dll because there are several calls to that dll in that example code you posted.[/quote]

Oops, Old age & poor memory – I had quite forgotten that setupapi was the .dll these calls had come from - but I almost certain I have neverused the SetupDiChangeState function. though.

@Chris Carter Hey no problem there’s quite a few good examples here of how to create the structures in Xojo for the other functions so I’m sure your examples will give me the hints I need to figure it out. Thanks.