7
votes

I am using GsmComm to connect to usb modem. The com ports for the modem I am using doesn't show up in device manager when I initially connect the modem to the computer. Computer shows it as a removable drive. However, when I run the application provided with the modem the com ports show up in device manager.

So every time I want to use the device with my application, I have to first connect it to the pc, run their software to initialize the com ports, and then run my application.

But is there any way to initialize the com ports from my application with C#?

I have read something about creating virtual com ports to connect to usb devices, but I have no idea how to do it. Any help or pointers will be highly appreciated.

Update 14 Feb 2016

I followed antiduh's answer and found that the device is recognized as a cdrom when first connected.

enter image description here

After running their application the link changes to harddiskvolume -

enter image description here

and three new com links are created.

enter image description here

3
Without knowing anything about the specific device you are using it is impossible to answer this. But take a look at draisberghof.de/usb_modeswitch for more information about what your device is doing. If it is listed in draisberghof.de/usb_modeswitch/device_reference.txt you'll find the data you need to send to the device there as well.AVee
@AVee, well, its a Chinese brand. So I don't think I will find it in the link you provided. Even the brand name is not written anywhere in the device.th1rdey3

3 Answers

6
votes

Virtual serial ports are emulated by the device driver that came with the device. If they don't show up in Device Manager until you run their software then it either installs a device driver dynamically or it sends a secret handshake to the driver to tell it to start emulating the port.

The former requires UAC elevation and a .sys file, if you don't see a prompt when you run their software and don't see an installed service that might do it nor that .sys file then you can scratch that possibility. The latter is usually done through a DeviceIoControl() call, the kind you can spy on with a filter driver. Like IoSpy, a utility that is included with the WDK. Using Dumpbin.exe /imports on the utility can provide useful implementation details, as does SysInternals' Process Monitor.

Hardly a guarantee for success, best to ask the manufacturer for the details. They however don't often return the phone call and do not include such details in the manual. They of course prefer anybody to use their shovelware. Keep in mind that you see the curly tail of a pig in a poke, best to cut your losses by returning the device and buying another one from a different manufacturer.

3
votes

I have a hypothesis.

Have you ever seen the Windows Object Manager? It's a neat little namespace in Windows that is used for wiring and exposing all types of crazy little objects, including devices-as-files. Think of it as Window's horrible version of `/dev'.

Interestingly though, user-space programs can access it by calling CreateFile, using a special prefix.

For instance, one way that you open serial ports in windows is by calling CreateFile(@"\\.\COM3"). This is a path that maps to the Object Manager path \GLOBAL??\COM3.

Here is what the path looks like using WinObj: Screenshot of the Windows Object Manager program showing OM paths

In my case, you can see that \GLOBAL??\COM3 is actually wired to \Device\QTUSBSerial0.

If you were to watch WinObj before and after this special software runs, you might find out what target device is symlinked to COMX, and then you might be able to find out if that real device is actually always there.

Naively, I would think it would be possible to path arbitrary Object Manager paths to CreateFile to access objects without having to rely on the \\.\ -to-\GLOBAL??\ mapping. However, it seems there's a hangup - according to this answer, CreateFile will only accept arguments that target the \GLOBAL??\ section of the object manager - it will only accept \\.\ as the path prefix, and won't accept, for instance \\.\Device\QTUSBSerial0 or some similar string.

There's still one possible out: Create a really small device driver / kernel module to create the symlink yourself, using IoCreateSymbolicLink. Write a driver that creates the Object Manager symlink \GLOBAL??\CrazyDevice --> \Device\CrazyDevice, and then use the hacked-up SerialPortNet code to call CreateFile(@"\\.\CrazyDevice").

It's a bit of a stretch, but maybe it'll solve your issue.

Disclaimer: I've never written a Windows device driver or manipulated the Object Manager. I barely know what I'm doing here.

2
votes

Look its been ages since I have used windows, however, the concept still applies. First of all it seems this usb modem has mode switching, meaning it first identifies it self as a CD-ROM to enable you to acquire the drivers, then it mode switches once the installation finishes to a mass storage device which contains the script that created the 3 COM port symbolic links. To use this in an application, you need a few things, first is the usb PID and VID to be able to enumerate the device on your computers hub. Second, you need a copy of that script that triggers the symbolic link creation and you need to call that script from your application once the device is detected ( by enumerating for VID and PID) once the script executes, three com ports will appear automatically and you should be able to access them as usual. also you might want to check if the CD-ROM application in the beginning installed dlls (almost certainly it did & almost certainly the second script checks for the dlls before creating the COM port links, so make sure the dlls stay where they are supposed to). you will need to link those with your application as well to gain any extra functionality that those provide (but that opens up pandoras box for native interface, if your not comfortable with that dont do it ... I can do it / show example in Java but not C#) , otherwise, if just using the com port is what you want and you actually know how to talk to the device (AT Commands) then forget about it and open com ports and fire away. last point, you will have to figure out the native interface (not really native interface, just a system call to execute a bash command to run a script/exe, thats all) for C#, so look for a system call function built in the .NET framework.

Let me know if you need any further clarifications on the steps.

UPDATE:

for usb enumeration, you can use a library similar to javas usb4java and implement a function similar to the following

public Device findDevice(short vendorId, short productId)
{
// Read the USB device list
DeviceList list = new DeviceList();// ---> Here is empty device list 
int result = LibUsb.getDeviceList(null, list);// ---> Now list is populated
if (result < 0) throw new LibUsbException("Unable to get device list", result);

try
{
    // Iterate over all devices and scan for the right one
    for (Device device: list)
    {
        DeviceDescriptor descriptor = new DeviceDescriptor();
        result = LibUsb.getDeviceDescriptor(device, descriptor);
        if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
        //Match the VID and PID (function inputs)---> if you find a match, then return success/or the device
        // you can find the VID and PID from the device manager
        if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) 
        return device;
    }
}
finally
{
    // Ensure the allocated device list is freed
    LibUsb.freeDeviceList(list, true);
}

// Device not found
return null;
}

This function allows you to access the usb device directly from the application. you can then start initialization sequence and bulk transfers (most probably will work for a Virtual COM port device, that would be typical for an ftdi chip for example, but since this is an unknown Chines chip, this is as far as we can go at low level, we have to build on whats provided and let windows do the driver dirty work) ...

At this point your program knows for a fact that the usb device is plugged in, if fucntion returns null, then sleep for a second and keep polling till the device is plugged.

from this point, you need to run the script that will create the symbolic links, I will assume its a .exe file. Here is a copy and past for a C# code posted by another member

using System.Diagnostics;

// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after  the executable name itself
start.Arguments = arguments; 
// Enter the executable to run, including the complete path
start.FileName = "C:/path/to/your/.exe";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
int exitCode;


// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
 proc.WaitForExit();

 // Retrieve the app's exit code
 exitCode = proc.ExitCode;
}

having the exit code is very useful to indicate whether the script successfully created the symbolic links or not (hopefully the Chines guy who made this followed proper coding practices).


EDIT:

The script might fail. The reason is simple: It doesnt know the ProductID and VendorID to enumerate and perform the initialization on. (99.99999% they didnt recompile a simple initialization script for every single unit just to hard code the pid and vid) so it probably receives the pid and vid as args (best case) or reads from usb mass storage hidden sectors (at which point you might have path issues if your running the script from non-root location)... you might have to gdb to find out if some args are missing specially if the .exe doesnt output anything to stderr


finally at this point you can start looking for a list of COM ports using standard C# libraries using something like :

Code Source

var portNames = SerialPort.GetPortNames();

foreach(var port in portNames) {
    //Try for every portName and break on the first working
}

and when you find the port you are looking for you can open it using

Code Source

public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);

// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();

// Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;

_serialPort.Open();
_continue = true;
readThread.Start();

Console.Write("Name: ");
name = Console.ReadLine();

Console.WriteLine("Type QUIT to exit");

while (_continue)
{
    message = Console.ReadLine();

    if (stringComparer.Equals("quit", message))
    {
        _continue = false;
    }
    else
    {
        _serialPort.WriteLine(
            String.Format("<{0}>: {1}", name, message));
    }
}

readThread.Join();
_serialPort.Close();
}

public static void Read()
{
while (_continue)
{
    try
    {
        string message = _serialPort.ReadLine();
        Console.WriteLine(message);
    }
    catch (TimeoutException) { }
}
}

Hope that helps get you started !