How to identify identical USB dongles connected at the same time to an USBhub - windows-10

I have an USBhub (D-Link DUB-H7) connected to my laptop with Windows 10. Connected to the hub are 4 identical PEAK dongles to talk to a CANbus. Now, my problem is to programmatically identify these 4 seperate dongles and knowing which one is which. The dongles are fixed in the hub.
To start I downloaded and installed USBDeview to view my devices. In the program I can see my 4 PCAN-USB dongles and I can see that their Instance ID are different. That's good, now I have my identifier. However, once I try to get the ID's with Python things aren't looking good. This is my following test program:
import win32com.client
wmi = win32com.client.GetObject("winmgmts:")
input("Connect the hub")
dongle_list = [(usb.name, usb.DeviceID) for usb in wmi.InstancesOf("Win32_USBHub")]
input("Disconnect the hub")
nod_list = [(usb.name, usb.DeviceID) for usb in wmi.InstancesOf("Win32_USBHub")]
diff = list(set(dongle_list) - set(nod_list))
for d in diff:
print(d)
Running this gives me only 2 new usb devices and the ID's point to the USBhub rather than the dongles connected to the hub. I also tried with wmi.InstancesOf("CIM_USBDevice") but the result stays the same.
('Generic USB Hub', 'USB\\VID_05E3&PID_0608\\5&4A43CD6&0&4')
('Generic USB Hub', 'USB\\VID_05E3&PID_0608\\6&9EBFB9C&0&4')
So how can I retrieve the usb information of the devices connected to the USBhub using Python or powershell/cmd invoked by Python?
Another route I was thinking I could take is by using port identifiers. If I disconnect a dongle I can see that it was connected to Port_#0001.Hub_#000x, where is x is a positive integer. So maybe I could poll the port to see if the dongle is connected and then I too know which one is which (dongles are fixed in the hub). Although I think that using the Instance ID is more foolproof.

I ended up creating a powershell command and invoke that using Python. I noticed however that the InstanceID's are hardware addresses of the the USBhub and not the dongles.
I also noticed that the dongles sometimes switch their addresses related on how they were connected. So I also needed to disable them and enable them again in the order I want.
This is what I have now:
poll_cmd = """
$ret = (Get-PnpDevice | ? {($_.description -like "*pcan*") -and ($_.status -eq "ok")} | sort instanceid).instanceid;
$ret | % {Disable-PnpDevice $_ -Confirm:$False};
$ret | % {Start-Sleep -Seconds 1; Enable-PnpDevice $_ -Confirm:$False};
$ret
"""
usbs = (
subprocess.check_output(["powershell.exe", poll_cmd])
.decode()
.strip()
.split("\r\n")
)
usbs holds the list of usb dongles I'm interested in.
However this gave me one more problem: UAC. I need admin rights to enable the usbs. And used the top answer from Request UAC elevation from within a Python script? to give my program admin rights.

Related

Using Findstr to find a serial number in a line

I am using devcon to query a verifone device to get the serial number and I a trying to find the serial number below "123-552-666" and output that to a text file. I have been playing around with various commands but just cant seem to get it.
I have used this:
findstr /RC:"[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]" "verifone1.txt"
But just got this lie returned
USB\VID_11CA&PID_0300\123-552-666_____
Below is the full data I am searching. Would appreciate any assistance
USB\VID_11CA&PID_0300\123-552-666_____
Name: Verifone V,P Family USB UART device (COM7)
Driver is running.

alsa: How to programmatically find if a device is busy/in use using it name and without opening it

We have a Linux device which has a speaker and MIC devices. These devices are shared among different modules - example a VOIP call can use speaker, a hazard Warning system can use speaker, a Voice prompt can use a speaker etc:
There seems to be a function in ALSA which uses pcm to provide the status.
int snd_pcm_status (snd_pcm_t * pcm, snd_pcm_status_t * status)
However the *pcm is returned by snd_pcm_open. We do not want to open the device as we would like to know the status of the device using its "name"
Alsa API is here
How can we check if a resource/device is busy without opening it and using its name?
The same information for playback stream X of device Y on card Z is available in the file /proc/asound/cardZ/pcmYp/subX/status; when the device is not open, it just says "closed".
Please note that you cannot use this information to decide if you can open the device, because some other process could have openend it just after you've read this information. The only way to check if you can open it is to actually try.
Though it requires /dev/dsp, this seems to work:
#!/bin/dash
## If the speaker is not used by any, returns 0, and prints "free"
## Otherwise, returns 1 and prints "not free"
iExit (){
trap '' 0
exit $1
}
iCatch (){
# The speaker is already in use
echo not free
iExit 1
}
trap iCatch 0
{
exec 3>&1 1>/dev/dsp
# If the execution reaches here, the speaker is not used by any
# Otherwise, it's catched by iCatch
exec 1>&3
echo free
iExit 0
} 2>/dev/null
Without PulseAudio, it seems on some PC only one output is accepted at one time; on others simultaneous playbacks are allowed. But even in the latter case, the above code works.
NB: The above code does not work with bash; for bash, simply use if/else rather than trap.
NB 2: /dev/dsp may be lacking depending on the kernel configuration.

Get numbers of all screens in Xlib

I've been googling for quite a long time and I just can't find any information on how to get screen_number for every screen connected to computer. Here I found a list of macros and some of them (like for example ScreenOfDisplay(display, screen_number) ) use argument screen_number. However there is no such macro that could give me a list of those numbers (one for every connected screen). I know how to get number of default screen (DefaultScreen() ) and count of all screens ( ScreenCount() ) but what about other screens? I noticed that screen_number of default screen is 0, although I have only one screen connected to my computer so I can't really test what happens when there are more of them. I think that screen_number could be assigned in a very simple way which is screen_number=0 for first screen,screen_number=1 for second,screen_number=2 for third and so on but as I said... I can't test wheather it's true and even if I had multiple screens how could I be sure that it works like this for all computers .Please ,if anyone of you has more experience with X11 and knows all details about how it works,tell me if I am right.
The ScreenCount(dpy) macro and int XScreenCount(Display*) function both return the number of screens connected to the display. Valid screen numbers are 0 to ScreenCount(dpy)-1. Macros in Xlib.h confirm:
#define ScreenCount(dpy) (((_XPrivDisplay)dpy)->nscreens)
#define ScreenOfDisplay(dpy, scr) (&((_XPrivDisplay)dpy)->screens[scr])
Your source (2.2.1. Display Macros) provides enough information. Normally the default screen-number is 0, e.g., when connecting to the local host you could use :0.0 as indicated in the documentation for XOpenDisplay.
That is "normally". If you run VNC, normally that runs on a different display (the first 0 in the simple connection string shown).
But (reading the documentation), when an application calls XOpenDisplay, it asks for the given screen-number (which the X server may/may not honor):
screen_number
Specifies the screen to be used on that server. Multiple screens can be controlled by a single X server. The screen_number sets an internal variable that can be accessed by using the DefaultScreen() macro or the XDefaultScreen() function if you are using languages other than C (see "Display Macros").

Raspberry Pi, JCblock and Trendnet TFM 561U. Dosen't seem to work

The dev suggested to ask here to get more help.
This is what is happening, I bought a Raspberry Pi (second one, I know how to handle them, and I'm used to apt and ssh) and a Trendnet TFM561U modem, downloaded NOOBS, did the initial setup with an extra in the shape of Webmin. So far aside from Webmin, it's a vanilla Raspbian. I then downloaded JCblock ( http://sourceforge.net/projects/jcblock/ ) and following the instructions here http://weeklytechforum.com/2013/03/28/block-unwanted-calls-with-a-raspberry-pi/ I edited the files that needed editing and finally compiled. I found out what serial port the modem was using (same as the guide! cool) and finally proceeded to test it. That's where the trouble started.
The differences between me and the guide is that I'm in Italy, so the dev of JCblock made me edit the source with a few extra lines of code to talk to the modem... now it's a great time to point out that while I can edit code and recompile it, this is C and I absolutly have no clue about it, knowing just some basics from Pascal and a bit of Visual Basic, so in the end I'm editing the source blindly, just guessing where things need to go. Anyway the edits are to send the Country Code command: AT+GCI=59\r and Caller ID command: AT+VCID=1\r but we do not seem to be geting caller ID data from the modem.
What I'm expecting to see: I call with my cellphone (and that number is in the blacklist) the Raspberry doesn't make the phone ring, or in case of wrong configuration of the blacklist I get some output that will show a call not filtered.
What I get: the phone rings, the modem data light flashes, the program dosen't output anything.
Any help will be appreciated, thanks.
EDIT:
So, I went ahead and used NCID to see what would happen.
As it generates a cornucopia of data, I think it can be useful
Started: 10/27/2014 22:24:54
Server: ncidd (NCID) 1.0
API: 1.0 Feature Set 1 2 3 4
Command line: ncidd
-Dv3
Logfile: /var/log/ncidd.log
Processed config file: /etc/ncid/ncidd.conf
Verbose level: 3
Configured to send 'cidlog' to clients.
Configured to send 'cidinfo' to clients.
Helper tools:
/usr/bin/cidupdate
/usr/bin/ncidutil
Processed alias file: /etc/ncid/ncidd.alias
Alias Table:
Number of Entries: 1
Leading 1 from a call required in an alias definition
Calls in the blacklist file will be terminated
Processed blacklist file: /etc/ncid/ncidd.blacklist
Blacklist Table:
Number of Entries: 12
Calls in the whitelist file will not be terminated
Processed whitelist file: /etc/ncid/ncidd.whitelist
Whitelist Table:
Number of Entries: 0
CID logfile: /var/log/cidcall.log
CID logfile maximum size: 110000 bytes
Data logfile not present: /var/log/ciddata.log
Maximum number of clients/gateways: 25
Telephone Line Identifier: -
TTY port opened: /dev/ttyACM0
TTY port speed: 19200
TTY lock file: /var/lock/LCK..ttyACM0
TTY port control signals enabled
CallerID from AT Modem and optional gateways
Handles modem calls without Caller ID
Sent Modem 20 of 20 characters:
AT Z S0=0 E1 V1 Q0
Modem response: 26 characters in 1 read:
AT Z S0=0 E1 V1 Q0
OK
Try 1 to init modem: return = 0.
Modem initialized.
Sent Modem 6 of 6 characters:
ATI3
Modem response: 39 characters in 1 read:
ATI3
CX93001-EIS_V0.2002-V92
OK
Sent Modem 9 of 9 characters:
AT+GCI?
Modem response: 27 characters in 1 read:
AT+GCI?
+GCI: 59
OK
Sent Modem 13 of 13 characters:
AT+FCLASS=?
Modem response: 32 characters in 1 read:
AT+FCLASS=?
0,1,1.0,8
OK
Sent Modem 11 of 11 characters:
AT+VCID=1
Modem response: 17 characters in 1 read:
AT+VCID=1
OK
Modem set for CallerID.
Hangup option set to hangup on a blacklisted call
Modem used for CID and to terminate calls
Network Port: 3333
Debug Mode
Not using PID file, there was no '-P' option.
Modem is fd 4
NCID connection socket is sd 5 pos 1
RING
CIDINFO: *LINE*POTS*RING*1*TIME*22:25:21*
CIDINFO: *LINE*POTS*RING*0*TIME*22:25:33*
What I can tell from this, is that it should be properly set up to hang up for a blacklisted number (my cellphone, for testing purposes) and that no caller id gets to the software. About that, it's important to point out how we have a mess of different phones (make, model, cordless, wired...) at home, and all of them do show 1 missed call from me. So at least I can confirm that the caller id is being sent.
I looked on Trendnet TFM561U description page and Amazon for this modem and didn't see "caller ID" in the description.
Your modem looks like it has Caller ID because its response to the activate caller ID command is "OK":
AT+VCID=1
OK
But looking at
Testing Modem Caller ID Support with HyperTerminal (or Putty) page
However, if, in step 8), the modem responded with "OK", then you probably have the correct string. In this case, either your phone line does not have caller id enabled (this can be tested with a hardware caller id device), or the modem does not support caller id. The latter is possible even if, in step 8), the modem responded with "OK", since many modems use the same firmware regardless of whether the modem contains the appropriate caller id hardware. In other words, many modems don't know whether they have caller id support!
I would say return your modem and get one that has "caller ID" in the description.

Setting playback device by executing a batch file / powershell script

I've got my computer(Windows 7) hooked up to the TV, and i very often change output device for sound between Digital Audio (S/PDIF)(High definition audio device) and my headset (2- Corsair CA-HS1 USB Headset)
I wanna be able to execute a batch/script file who changes this for me so i don't have to "right click volume > playback devices > "Mark output device" and click "set default".
I know it's a luxury problem, but hey, maybe I can learn something from someone?
All help appreciated!
This is how I set 'Line 1' as the playback device:
start /min "" G:\......\nircmd.exe setdefaultsounddevice "Line 1"
NirCmd is a small command-line utility which you can download that allows you to do some useful tasks without displaying any user interface.
I had the exact same requirement as yourself, and AFTER stumbling across your posting I found the following:
https://web.archive.org/web/20131231034118/http://downloadsquad.switched.com/2010/06/16/windows-7-tip-how-to-change-the-default-audio-device-with-a-hot/
Unfortunately it's not a native Windows function; it requires the download of a small open-source scripting tool called AutoHotKey, but it works nicely and only requires a small amount of memory (1 ~ 2.5Mb)
The script provided in the original article doesn't work for me. It's searching for Enabled/Disabled devices and changing that value, as opposed to changing the default device.
I've edited it to switch between 2 default devices now.
It works by opening your Sound control panel (mmsys.cpl), then scrolling down the list of playback devices to the second item in the list (that's the {Down 2} part).
This is because my Speakers are the second item in my list.
It then checks to see if the device is default or not. If not, it sets it as the default and closes the window. If it's already the default, it scrolls down another 2 times and sets that as the default.
So, you'll need to ammend the {Down 2} lines to fit your own list of devices.
#+a::
Run, mmsys.cpl
WinWait,Sound
ControlSend,SysListView321,{Down 2}
ControlGet, selectedDevice, List, Focused, SysListView321
Loop, Parse, selectedDevice, %A_Tab%
if a_index <> 3
continue
else
{
if A_LoopField <> Default Device
{
ControlClick,&Set Default
ControlClick,OK
WinWaitClose
SoundPlay, *-1
return
}
else
{
ControlSend,SysListView321,{Down 2}
ControlClick,&Set Default
ControlClick,OK
WinWaitClose
SoundPlay, *-1
return
}
}
To follow up on Dale Newton's post, NirCmd is a great way to do this. On top of that if you pair it with AutoHotKey you can create an executable that will change your devices without opening pesky CMD windows every time you run it. For example, I have two sources that I switch between all the time, one is my headphones and they other is my monitor. For my monitor I created an ahk script that does this:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir% ;I have nircmd in the same folder as these scripts
Run, nircmd setdefaultsounddevice "Acer X34-8" 1
Run, nircmd setdefaultsounddevice "Acer X34-8" 2
And another for my headphones with the last two lines changed to:
Run, nircmd setdefaultsounddevice "Headset Earphone" 1
Run, nircmd setdefaultsounddevice "Headset Earphone" 2
Afterwards you can compile each ahk script into an exe and bind each exe to a keyboard macro so you can execute them with a couple key presses. Personally I am using a Corsair K95 so I use their software to bind these to my 'G' keys.
Also to note, if you are in your sound preferences you can rename any of the devices to avoid naming conflicts.
I had a HDMI device that keeps changing it's name, so none of the existing solutions worked for me.
I eventually ended up with this powershell and use of the NirCmd app.
#File: TV.ps1
$name = "SMART*"
# list active audio playback devices. (Note for cature devices change Render to Capture)
$device = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\*\" | where {$_."DeviceState" -eq 1} | foreach-object -Process {(Get-ItemPropertyValue -Path ($_.PSPath + "\Properties\") -Name "{a45c254e-df1c-4efd-8020-67d146a850e0},2")} | Where-Object {$_ -like $name}
C:\bin\NIRCMDC setdefaultsounddevice $device 1
C:\bin\NIRCMDC setdefaultsounddevice $device 2
As far as I understand there is no way to do this programmatically. This is a deliberate design, since Microsoft does not want applications to override audio setting set by user.
You will find same answer here but if you solutions that manipulate windows you can have a look here.
The following script is written in Windows 7 and uses sendkeys. It is based on other snippets I found but adds to them to ensure selection is consistent and stray windows are not left displayed.
You may want to adjust SleepTime for your system if it still plays up.
Call the script using a shortcut with the index of the item you wish to select in the Playback Devices window as first parameter.
You can create a new 'toolbar' on your 'taskbar' to select each device with a single click:
Taskbar toolbar picture
'===============================================================================
'This script uses sendkeys to select the Sound Output device
'First parameter should be the desired device number in 'Playback Devices' list
'===============================================================================
Option Explicit
Const SleepTime = 200
Dim WindSh
'===============================================================================
'MAIN ROTUINE
'===============================================================================
'Check the command line input
if ( Wscript.Arguments.Count <> 1) then
MsgBox "You must provide a single integer argument representing the device number", vbinformation + vbokonly, Wscript.ScriptName
Wscript.Quit 1
elseif ( false = IsNumeric( Wscript.Arguments.Item(0) ) ) then
MsgBox "The argument provided was not an integer number: " & Wscript.Arguments.Item(0), vbinformation + vbokonly, Wscript.ScriptName
Wscript.Quit 2
End If
set WindSh = CreateObject("Wscript.Shell")
WindSh.run("control.exe mmsys.cpl")
do while (WindSh.AppActivate("Sound") = false)
WScript.Sleep SleepTime
loop
WindSh.sendkeys("{DOWN " & Clng( Wscript.Arguments.Item(0) ) & "}")
WScript.Sleep SleepTime
WindSh.sendkeys("{TAB 2}")
WScript.Sleep SleepTime
WindSh.sendkeys("{ENTER}")
WScript.Sleep SleepTime
WindSh.sendkeys("%{F4}")
WScript.Sleep SleepTime
if (WindSh.AppActivate("Sound") = true) then
WindSh.sendkeys("%{F4}")
end if
Might not be related to switching the audio device around via script, but I also have my computer hooked up to my TV via HDMI.
When I want to watch video files on the TV I use Media Player Classic Home Cinema - this lets me specify the audio device for MPH:HC in the options without switching over my default sound device on the entire computer.
If your usage is similar to mine maybe MPC:HC will solve your problem for you.
Note that if you use nircmd setdefaultsounddevice speakers in an enterprise or educational environment login script, the sound will still come out from the speakers when headphones are plugged in.

Resources