i am creating a project related to power consumed by our computer systems.
now my requirement is ,
i need to find out power consumed by a usb device(like pendrive) in the system.
can anybody plz help me.
I don't think you can because the USB specification seems not to mention anything about delivering those measurements. The only information I know you can get is if the device is a low-power or high-power device. For USB 2.0 that is 100mA or 500mA with a voltage between 4.4 and 5.25V. So a low-power power device may consume from almost 0 to 5.25*0.1=0,525W and a high-power up to 5.25*0.5=2.625W.
Unfortunately the WMI classes doesn't seem to give you even that information but that could be just me looking at the wrong places.
//using System.Management
var USBDevices = new ManagementObjectSearcher(#"Select * From Win32_USBControllerDevice");
foreach (var device in USBDevices.Get())
{
foreach (var prop in device.Properties)
{
Console.WriteLine(prop.Name + " : " + prop.Value);
}
}
You might have a look at http://www.sharpdevelop.net/OpenSource/SharpUSBLib/default.aspx
An USB library by the creators of SharpDevelop (at least on the same server)
Related
I am using Lecia Disto e7100i which basically measures distance and area using laser. This device has bluetooth and can be paired with windows.
I am trying to develop an wpf app that reads the mesaured data using c#
There is no sdk that comes along with the device.
I have tried to use 32feet.Net but since there is no proper documentation I don't know where to start.
Is there any way that I can do to solve my problem?
This is not a full response, instead its more of a guideline on how to resolve your issue:
Pair the device with your Computer
Run the included software that displays the data somehow
Use WireShark to analyze the traffic
see if it is a standard protocol type or something custom
understand the protocol and reimplement it using c# and BluetoothSockets
To get started, you can try:
var client = new BluetoothClient();
// Select the bluetooth device
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
BluetoothSecurity.PairRequest(addr, "PIN"); // set the pin here or take user input
device.SetServiceState(BluetoothService.HumanInterfaceDevice, true);
Thread.Sleep(100); // Precautionary
if (device.InstalledServices.Length == 0)
{
// handle appropriately
}
client.Connect(addr, BluetoothService.HumanInterfaceDevice);
Also make sure that
Device appears in "Bluetooth devices" in the "Control panel".
Device is HID or change code accordingly.
Hope it helps. Cheers!
Try this demo project, and the following articles after that one.
Try to follow this tutorial
Here you can see a direct answer by the mantainer of 32feet, with which you can get in touch
Check also this answer
I'd like to play a soundfile from the speakers integrated in the laptop, even if an additional speaker is connected via the headphone jack (but it's not listed in the sound device list).
I've had a look at How can I make the computer beep in C#? and How to beep using PC speaker? and have understood that the beep driver was removed in windows 7.
However, I do not want to use this extremely simple integrated speaker controlled by the removed driver, but the normal one present in most modern laptops.
Unfortunately, when I connect my external speakers, they do not show up as an additional device. When I run this code (using NAudio), I always get just one result, and it's the same whether the external speakers are connected or not:
var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
Console.WriteLine(endpoint.FriendlyName);
}
Is it possible to play a sound on the normal laptop speaker, while an external speaker is connected?
Edit:
I don't think my question is a duplicate of Play a sound in a specific device with C#
I don't see an additional device when I connect my external speakers, so I guess my laptop doesn't have multiple sound cards (how can I verify?).
The solution from the other question (waveOut.DeviceNumber = deviceNumber;) doesn't work for me neither. When I choose deviceNumber = 0;, the sound comes from my external speaker if it's connected. If I set deviceNumber = 1; (while it's connected) I get a MmException
I see I'm a bit late for the party but i was working on something similar and got to this solution.
Hope it helps someone in the future :)
var enumerator = new MMDeviceEnumerator();
IWavePlayer waveOut = new WasapiOut();
foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
if (endpoint.FriendlyName == "Name of the device you want")
{
waveOut = new WasapiOut(
endpoint, AudioClientShareMode.Exclusive, false, 0);
AudioFileReader wave = new AudioFileReader("Audio Name");
waveOut.Init(wave);
waveOut.Play();
break;
}
}
I am trying to write a WLAN fingerprinting program using NativeWifi in C#. To do this i run a loop to get the wlan information many times and then later use matlab to average / analyze the data.
The problem is that i get all the same values, even as i move about the house, when the program is running. From the internet i've seen that there is a cache that stores the data of available networks. I was wondering if there is a system call which resets this cache.
I have also seen this using the cmd call
netsh wlan show networks mode=bssid
this gives me the same values until i open the available wifi networks in my OS and if i run it again after, it will give different values.
edit: This system will be for my use only, so i would be comfortable starting over on a linux platform if there is a known library that can handle this for me. I don't even know what to google to even get the information, though. Anything related to "network cache" takes me to help threads of unrelated topics...
I will provide the relevant part of my code below:
public void get_info_instance(StreamWriter file)
{
try
{
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();
foreach (Wlan.WlanBssEntry network in wlanBssEntries)
{
int rss = network.rssi;
byte[] macAddr = network.dot11Bssid;
string tMac = "";
for (int i = 0; i < macAddr.Length; i++)
{
tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
}
file.WriteLine("Found network: " + System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString());
file.WriteLine("Signal: " + network.linkQuality + "%");
file.WriteLine("BSS Type: " + network.dot11BssType + ".");
file.WriteLine("RSSID: " + rss.ToString());
file.WriteLine("BSSID: " + tMac);
file.WriteLine(" ");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Internally, netsh is powered by this API. What this means, is that calling netsh wlan show networks mode=bssid just returns the cache of the networks that showed up during the last scan. This is what you've discovered.
This means that in order to refresh this cache, you need to trigger a scan. If your C# library you are using includes it, you could make this happen on demand with a call to WlanScan. I am not sure which C# wrapper you are using, but it probably includes this function. When you get a scan complete notification (register with source WLAN_NOTIFICATION_SOURCE_ACM and look out for wlan_notification_acm_scan_list_refresh), the cache should be updated.
If you let me know which C# library you are using, maybe I can point you to the relevant functions.
You mentioned that opening the available networks causes the cache to refresh. This is because opening the available networks triggers a call to WlanScan.
Profiles are not relevant to the available network list -- profiles are what the Wlan service uses to keep track of which networks are configured on your machine -- deleting them does not make WlanSvc scan again. It may be a coincidence that deleting them happens to coincide with a scan, but it is more of a side effect than the designed usage.
edit: to subscribe to notifications using the Managed Wifi API you are using, this snippet should work:
wlanIface.WlanNotification += wlanIface_WlanNotification;
And the callback:
static void wlanIface_WlanNotification(Wlan.WlanNotificationData notifyData)
{
if (notifyData.notificationCode == (int)Wlan.WlanNotificationCodeAcm.ScanComplete)
{
Console.WriteLine("Scan Complete!");
}
}
You can test this by running this, then opening the available networks on Windows. You should see "Scan Complete" shortly after you open it each time. You can use a messagebox instead of Console.WriteLine if you prefer.
To trigger a scan yourself:
wlanIface.Scan();
for all
netsh wlan delete profile name=* i=*
you might not want to do it on all interfaces, and hard code the interface in there for faster result
Update: This may not be "Pairing". This may just need to have a service started and bound to a port. However, this code is not storing it either. I need the device to be stored even after the application is closed.
I am building a program specifically suited for Zebra RW 420's on a Windows Mobile 6 Handheld device. The application needs to allow a mobile device to pair with the printer on COM1. I believe I am very close to getting it, but I can't get the pair request to work.
I am able to communicate with the printer and even print by directly connecting and printing, but I can't get the mobile device to actually pair with it. I've tried a variation of pins to include null, "1", "0000", and "1234". No matter what, the method always returns false. Any suggestions or ideas why this might be failing? I can pair the device just find in the WM6 Bluetooth menu, but not in my application.
It might be important to note that the little light bulb icon on the printer comes on when the program says it is attempting to pair, but after about 5 to 10 seconds, it fails.
BluetoothSecurity.PairRequest(device, "1"))
Additional Information:
I've successfully paired with my Android phone using this code.
I then logged in and set a PIN on the Zebra printer. However, this code still fails to pair with the printer even when I know the pin is correct / set in the printer.
From https://km.zebra.com/kb/index?page=answeropen&type=open&searchid=1336682809706&answerid=16777216&iqaction=5&url=https%3A%2F%2Fkm.zebra.com%2Fkb%2Findex%3Fpage%3Dcontent%26id%3DSO8031%26actp%3Dsearch%26viewlocale%3Den_US&highlightinfo=6292341,26,43#
Zebra Bluetooth enabled mobile printers are 'slave' devices only. The printers will pair with any 'master' device that tries to make a valid connection. Since only a master device can initiate a connection, the printer does not store pairing data, that function is always done on the master device. The printer can only be connected to one master device at a time, but any number of master devices that have stored pairing information for the printer would be able to initiate a connection to the printer without having to rediscover it.
I'm guessing that this means the InTheHand.Net BluetoothSecurity.PairRequest might not work for this type of pairing?
In the Bluetooth section of the WM handheld, under the "Devices" tab, I can add the device. I need to essentially do that. I need to register the device in that list and then set it to use COM 1 in the "COM Ports" section. The application I am using doesn't actually print. It's sole purpose is to prepare the printer for other applications.
The quote from Zebra make it sounds as pairing is actually not required at all. Are you printing from your app? If so just connect to the SPP service and send the text.
BluetoothAddress addr = ...
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
var ep = new BluetoothEndPoint(addr, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write ...
(From General Bluetooth Data Connections)
The Zebra Mobile Printer needed to be properly configured before pairing with this method will work. Here is what I did:
First, I ran the following commands on the printer:
.
! U1 setvar "bluetooth.authentication" "setpin"
! U1 getvar "bluetooth.authentication"
! U1 getvar "bluetooth.enable"
! U1 getvar "bluetooth.discoverable"
! U1 setvar "bluetooth.bluetooth_pin" "0000"
! U1 getvar "bluetooth.bluetooth_pin"
Then, the application with this code ran successfully.
.
int pair_req = 0;
try
{
if (BluetoothSecurity.SetPin(device, "0000")) {
while (status == false && pair_req < 3)
{
++pair_req;
status_box.Text = status_box.Text + '\n' + "Attempt " + pair_req.ToString();
status_box.Update();
if (BluetoothSecurity.PairRequest(device, "0000"))
{
status = true;
client.Refresh();
status_box.Text = "Paired Successfully.";
status_box.Update();
Thread.Sleep(5000);
}
else
{
status = false;
}
}
}
}
catch (ArgumentNullException e)
{
status_box.Text = "Pair failed.";
status_box.Update();
Thread.Sleep(5000);
}
status_box.Update();
Thread.Sleep(400);
To get all avaliable Serialports from the system i use the following command.
SerialPort.GetPortNames
It works fine for the mainboard serial port, but with the bluetooth device i get the wrong portnames.
For Example: Instead of COM7 i get sometimes COM70 or COM7ö. Its always 1 letter to much.
any suggestens?
PS: I am using newest Visual Studio Express in Windows 7
PPS: The dirty hack to cut the last letter didn't work because i don't know which one is the bluetooth serial port (with various bluetoothstick or devices it changes the number of the comport) and after trying various sticks i reached COM10, ergo COM100 or COM10f
EDIT: the code i am using right now. reading the regestry, but still the same problem.
RegistryKey myRegistry = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
foreach (string valuename in myRegistry.GetValueNames())
{
if (myRegistry.GetValue(valuename) is String)
{
if (valuename.Contains("BthModem"))
{
richTextBox1.AppendText(">" + myRegistry.GetValue(valuename) + "<" + Environment.NewLine);
}
}
}
Normally the second or third request is working with a result like
COM11ᯋ<
COM10S<
COM11<
COM10<
COM11<
COM10<
how can that be?
This has been reported as a bug with non-null terminated strings:
Can you manually walk the registry?
HKLM\Hardware\DeviceMap\SerialComm
You can utilize WMI to query the system for serial ports, including those that are added by bluetooth devices and USB-To-Serial devices. Maybe that way you won't encounter this issue. See at CodeProject.
I have the same issue. SerialPort.GetPortNames basically uses the registry anyway- both of those methods don't seem to work with bluetooth.
The workaround I'm currently using is to loop through the first X com ports and see if they exist, which is hardly elegant. MS: FAIL.