Assign fixed COM ports to keyspan adapters on Windows - c#

I have an application that reads and sends data from/to 2/3 machines attached to the PC through keyspan USB to Serial adapter. Sending/receiving works well.
My problem is that on restart of the PC, the COM ports are assigned "at random", often swapped. That makes automatic processing of data impossible.
I tried to manually set the COMm ports in the Device Manager and with Keyspan assist software without success.
Questions:
how to fix COM ports on Windows so after restart they are always same?
How to assign COM ports with java/c#/powershell (get ports setting and save it(once configured), set on next restart from configuration file)?
Thank you in advance for any suggestions.
Roman

thank you very much for your help.
In the mean time I've slightly modified the solution from http://syswow.blogspot.ch/2013/03/change-device-com-port-via-powershell.html
Unfortunately my current keyspan device ID's are not stable (KEYSPAN*USA19HMAP\00_00 and KEYSPAN*USA19HMAP\01_00 and KEYSPAN*USA19HMAP\02_00), as noted before. These ID's can and often change in between restarts (I suppose as Windows is "registering" them).
I have swapped to another vendor and now I get stable ID's / COM's (FTDIBUS\VID_0403+PID_6001+FTH8ZL5AA\0000 and FTDIBUS\VID_0403+PID_6001+FTH919SZA\0000 etc.).
Thank you once again for your help.
Roman

It's not possible without special USB drivers from your vendor. I don't know who made the USB drivers for your keyspan USB, but you might send them an email asking if they offer something like this. We use Silabs CP210X drivers for usb drivers for our products and they offer a special one that makes it so the COM ports are assigned and don't change randomly when our devices are plugged in. You might try it and see, but no promises it will work with your USB.

In my experience, COM port names are stable in Windows. I suspect there is something weird about the way your USB-to-Serial adapters were implemented, or something weird about the Windows driver you are using with it.
However, given that the COM port names are not stable for you, I would suggest using libusbp. It's a USB abstraction libary written in C which can enumerate the USB devices on your system and tell you the names of their COM ports. What you would do is:
Get a list of devices using libusbp_list_connected_devices.
For each device, call libusbp_device_get_os_id. This will return a string that is something like USB\\VID_1234&PID_DA01\6&11A23516&18&0000. That string should be stable across reboots, so you would have a list of those strings in a configuration file for your software.
If you see a device whose ID matches what is in your configuration file, then use libusbp_serial_port_create and libusbp_serial_port_get_name to get the COM port name.
The library is in C, but it can compile to a Win32 DLL, and you can use PInvoke (FFI) to call functions in it from C# or Java. The library might have some minor compilation errors if you try to compile it in Visual Studio, since it is mainly used in MinGW/GCC/clang environments. You should be able to fix those errors and/or report them as issues on GitHub.

Related

Microsoft Point of Service won't detect printer

Some background: I'm using an Epson TM-T88V receipt printer connected via USB with Epson OPOS for .NET version 1.12.20 installed. It is detected and can print test sheets just fine from the devices manager.
My problems begin with Microsoft Point of Service. It seems to not want to connect to the printer or even acknowledge its existence. The configuration xml file has been auto-populated (correctly I think?) and looks like this:
<?xml version="1.0"?>
<PointOfServiceConfig Version="1.0">
<ServiceObject Type="PosPrinter" Name="TM-T88V">
<Device HardwarePath="TM-T88V" Enabled="yes">
<LogicalName Name="EpsonReceiptPrinter" />
...
</Device>
</ServiceObject>
</PointOfServiceConfig>
However when I try to access it via posdm.exe "listdevices", it doesn't pick up on the existing physical printer and only lists simulators. Posdm.exe is clearly using this file, as it gets written to/read from when I use the "adddevice" command.
I'm fairly new to POS/OPOS programming, but I'm also assuming this is why the printer goes undetected when I try to access it via C# with the PosExplorer.
Is there anything I'm missing that would make the POS refuse to connect to my device?
After a lot of headaches and trial-and-error, I've found a way to make this work.
First, I happened to stumble across this forum post, which indicated that the current Epson OPOS driver I'm using might not support Microsoft PoS 1.14. So I uninstalled it and downloaded Pos 1.12 instead.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/7fbc7186-560d-400b-9bfb-c638c5126e46/need-help-with-tmt20ii?forum=posfordotnet
And second, it seems that having any other windows drivers installed will enable Windows to take hold of the device and prevent any other applications from using it. So, I uninstalled the default Epson driver utility. This nugget of wisdom came from here:
http://discuss.joelonsoftware.com/default.asp?dotnet.12.699035.8
The combination of these two changes worked for me. However it seems Microsoft PoS is pretty finicky and a lot of the drivers out there leave a lot to be desired, so this might not be the solution for everyone.

Getting unique hardware ID changes everytime C#

I want to make a program and sell it, so I was thinking to make some kind of licensing system. What I've found on google is this:
http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer
I am running Windows 7 (32 bit) in VirtualBox from Ubuntu. I get the same UID, but when I restart my Windows, I get a new UID. I am just thinking that some people will buy my program and they may run it from VirtualBox, so it would be a problem.
Do you guys have any suggestions?
Even if it's an easier way, but something that won't change even if I restart my VBox.
You need to not get so bogged down in the whole DRM process, there are always smarter people out there and someone will work out how to circumvent it.
If you're really concerned:
Set up an online license server to hold your users licenses
Prevent the application from doing anything until the user enters a valid license
This licence would be validated over the internet by your service
You could restrict the number of "active" applications using your service that way
Think along the lines of how iTunes allows you to authorise up to 5 computers to access your media library.
Even if a lot of time has passed, I want to add a possible solution to this question, pointing out something that happened to me trying to generate a fingerprint with exactly that code and facing the same problem.
The identifier function retrieves only the first not-empty value of wmiProperty for each wmiClass. The first important thing you should be aware of is this: what is returned for every wmiClass might not belong to the same ManagementObject. Hence, if something changes to "not the first object", this might affect also your fingerprint.
Be advised that using Win32_DiskDrive as wmiClass is dangerous since plugging in a USB device often results in this device being recognized as the first one (instead of your hard drive), changing the fingerprint as you plug-in or out your device. Even if this doesn't happen, still one of the wmiProperty could be taken from your USB device if your main drive has it empty (e.g. the disk signature, which happened to me).
A possible solution to that would be excluding all USB devices by checking InterfaceType and excluding all those who match "USB", or at least that's what I did.

Write/Read file in my iPhone app Shared File

I wrote an app on my iPhone. It's a more portable and smaller version of my pc software. I activated the File Sharing feature on my app so now I can transfer files through iTunes. But I want my pc software to be able to read or write files to that shared folder on my iPhone without having to do it manually through iTunes.
I have big constraints:
I can't use a Jailbroken iPhone/iPod/iPad
The vast majority of my customers don't have Internet access (It's a farm management software so even cellular are not available in some area)... :(
I heard there is a way using Manzana and MobileDevice.dll (itunesmobiledevice.dll). I don't really know how to use these dll. I tried to use Manzana a little but I can't access my folder since it's not a jailbroken iPhone.. Can someone help me with a little bit of code example?
Or is there other ways to make my iPhone app communicate with my C# application using the USB cable without internet access or Wifi?
mobiledevice.codeplex.com. This project should let you send and retrieve files from the phone
I'd suggest seeing if you can use the iTunes scripting interface. Add the COM reference iTunes 1.1 Type Library to a project and you can control many parts of itunes automatically. I can't find the documentation for it, but you can play around with the library and see if there is something to access the file sharing section.
Here's a decent introduction to using it:
http://www.codeproject.com/Articles/7723/Controlling-iTunes-through-COM

Read USB interrupt data

I have a USB device which which uses the libusb WIN32 drivers and
Interrupt data is available from the
accelerometer through the USB-HID
interface endpoint 83 (in EP83). Data
is in little end-in format with the
following fields (x,y,z,Vbat,CpuTemp.)
Data are acquired every 62.5ms (16Hz).
There a number of USB HID "get" and
"set" Reports available (through ep0)
How can I access this data via .NET and C#?
libusb32 is c/c++ library of generic usb driver. it comes in 2 layers. low layer in kernel mode is generic client driver libusb0.sys + .inf file that you change and it tells to what device to upload this generic usb driver.They have inf-wizard.exe tool that helps for you to make this .inf file for your device. Upper layer in user space is libusb.lib (you can make also static link) that talks to libusb driver. You can find in sources usb.h that actually defines interface between you code and usb (usb driver). To access unmanaged code or you should write your layer of interop in c# or in c++/cli or use ready layer that been made by somebody. Here the link to one of the projects, http://sourceforge.net/projects/libusbdotnet/
How to use libusb library i advice you to see some example from them. Usually it like you open handle to usb bus, then find there your device by VendorId & ProductId, get it's handle. Then make write/read to endpoints of device.
If it is HID device I recommend you to use Windows's default driver - hid.dll, I used it and it was ok. This way you should not care about deploying also a driver, it's there anyway and you just have to understand the API and use it. For this I recommend you Jan Axelson book USB Complete, she has pretty good explanation and samples there, it is a mixture of C/C++ and C# but the trend is for .net Also she has a HID Page on her website and there you find the code samples you just need.

How to uniquely identify computer using C#?

How to uniquely identify computer (mainboard) using C#(.Net/Mono, local application)?
Edition. We can identify mainboard in .Net using something like this (see Get Unique System Identifiers in C#):
using System.Management;
...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_MotherboardDevice");
...
But unfortunately Mono does not support System.Management. How to do it under Mono for Linux? - I don't know :(
Write a function that takes a few unique hardware parameters as input and generates a hash out of them.
For example, Windows activation looks at the following hardware characteristics:
Display Adapter
SCSI Adapter
IDE Adapter (effectively the motherboard)
Network Adapter (NIC) and its MAC Address
RAM Amount Range (i.e., 0-64mb, 64-128mb, etc.)
Processor Type
Processor Serial Number
Hard Drive Device
Hard Drive Volume Serial Number (VSN)
CD-ROM / CD-RW / DVD-ROM
You can pick up a few of them to generate your unique computer identifier.
Please see: Get Unique System Identifiers in C#
You realistically have MotherboardID, CPUID, Disk Serial and MAC address, from experience none of them are 100%.
Our stats show
Disk serial Is missing 0.1 %
MAC Is missing 1.3 %
Motherboard ID Is missing 30 %
CPUID Is missing 99 %
0.04% of machines tested yielded no information, we couldn't even read the computer name. It maybe that these were some kind of virtual PC, HyperV or VMWare instance, or maybe just very locked down? In any case your design has to be able to cope with these cases.
Disk serial is the most reliable, but easy to change, mac can be changed and depending on the filtering applied when reading it can change if device drivers are added (hyperv, wireshark etc).
Motherboard and CPUID sometimes return values that are invalid "NONE", "AAAA..", "XXXX..." etc.
You should also note that these functions can be very slow to call (they may take a few seconds even on a fast PC), so it may be worth kicking them off on a background thread as early as possible, you ideally don't want to be blocking on them.
Try this:
http://carso-owen.blogspot.com/2007/02/how-to-get-my-motherboard-serial-number.html
Personally though, I'd go with hard drive serial number. If a mainboard dies and is replaced, that PC isn't valid any more. If the HDD drive is replaced, it doesn't matter too much because the software was on it.
Of course, on the other hand, if the HDD is just moved elsewhere, the information goes with it, so you might want to look at a combination of serial numbers, depending what you want it for.
How about the MAC address of the network card?

Categories

Resources