Shopping Cart


Your Cart is currently empty.

Home The Insider Blogs Controlling a Digit-LS from a PC
Controlling a Digit-LS from a PC

In this posting I will show you how to communicate with a Digit-LS from a PC using C or C++. Typically you will do this to either control the device or read information from it. By doing so you would use your PC to control some piece of electronics that is connected to a Digit-LS that you provided with a configuration (See Digit-LS Programming for a posting on that subject).

The big picture is rather simple. Since the Digit-LS comes with a driver, Windows will treat it like any other plug-and-play device. That means that applications will access the device as if it were a file. So you get a file handle by using the Windows API function CreateFile, then write to the device using WriteFile function calls or read from the device using ReadFile function calls and when you are done, you just call the CloseHandle function. So that is very much the same as it would be with ordinary files on your hard disk.

So what is not the same then? Well, the big trick is in the file name. When you plug in a plug-and-play device (like any USB device), Windows' way of handling this is that it will generate a file name for that device automatically. The bad news is that you have no way of knowing, or even guessing, what name Windows assigned to your device. Even if you know it after it was plugged in, that information is still not useful since it could change even when you plug it into a different USB socket let alone plugging it into another computer. The good news is though that Windows knows the concept of Interface Classes. An interface class is essentially a GUID that has been assigned to some specific device (or rather an interface defined with that device). The SetupDi API in Windows offers a number of function calls that can tell you the filename that Windows assigned to your device as long as you know the GUID that was defined for its interface class. The way these functions work are rather complicated and I won't go into it here. You don't need to know neither since the Digit-LS header files come with a handy definition that will do it for you. You call this definition with the Digit-LS' GUID and it will return the file name that Windows assigned to your Digit-LS when it was enumerated. Let's put this in code:

if (!FIND_DEV_PATH ((LPGUID)&GUID_CLASS_DIGITLS_1_0, DeviceName, sizeof (DeviceName))) {
fprintf (stderr, "No Digit-LS found.\n");
exit (-1);
}

After this you will find the file name in the variable DeviceName. Now don't just call CreateFile for that yet! There is a little bit more to consider. Remember that stuff about USB Endpoints? You will need some way of telling Windows which endpoint of your Digit-LS configuration you want to talk to. Now this is one of the best features of the Digit-LS design (at least in my humble opinion it is): you simply use the Endpoint name you used in the Endpoint information structure when you were programming the Digit-LS. Concatenate it to your device path with a backslash (yes, just like it is a subdirectory) and you can get your handle and use it. So let's put that in code too, assuming that you would have filled EndPointName with the correct name of the endpoint you want to use:

wcscpy (FullName, DeviceName);
wcscat (FullName, L"\\");
wcscat (FullName, EndPointName);

And now that we got our full path name, let's create a handle for that and write some data to it that was put in an array of bytes called WriteBytes with length nrWriteBytes:

hDevice = CreateFile(FullName,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );

if (hDevice == INVALID_HANDLE_VALUE) {
fprintf (stderr, "Failed to obtain file handle to device "
"%ws with Win32 error code: %d\n",
FullName, GetLastError());
exit (-1);
}

if(!WriteFile(hDevice, WriteBytes, nrWriteBytes, &nrWriteBytes, NULL)) {
fprintf (stderr, "WriteFile to device %ws failed with Win32 error code: %d\n",
FullName, GetLastError());
CloseHandle (hDevice);
exit (-1);
}
CloseHandle (hDevice);

After this, you have sent data to your Digit-LS and now it would start processing it by calling the Endpoint Transaction Handler function you defined in the Digit-LS configuration. So what will happen next will depend on what you programmed there but by programming the above lines of code you would have succesfully managed to get the relevant data from your PC to your Digit-LS. Obviously that code needs a lot more details added like the declarations and quite a few lines of security code to make sure that no buffer overflow takes place or other error conditions can occur without being handled properly. But still, it's no rocket science at all, anyone with a basic idea of how file handling works can write code that talks to a Digit-LS. Next time I will show you a real life example that uses the LED example that I discussed in my earlier posting Digit-LS Programming.

The Insider

Last Updated on Friday, 17 April 2009 16:28
 
Copyright Sofrides IO