For a project i have to communicate with a netduino,
So i use serial communication to communicate with the netduino.
But here's my problem
I cannot find my Usb portname, i use this small piece of code to find the port names.
private void GetPortNames()
{
string[] ports = SerialPort.GetPortNames();
ComportListbox.DataSource = ports;
}
It doesnt show the usb port names.
What am i doeing wrong, or how can i fix this issue.
EDIT
Question edited:
Can i see the usbportname from my usbport where the NETduino is attached to. So i hope to see COM10 for example. I looked in the system managment and saw that the usb is called Port_#0001.Hub_#0001. How can i open this port.
If ComportListbox has an "add" method, why not just use that with a for loop.
foreach ( string portName in ports )
{
ComportListbox.Items.Add( portName );
}
If not, let me know and I will delete this answer.
Otherwise you may have to use a BindingList<string>. see: Binding List<T> to DataGridView in WinForm
Or you might even have to create an object that contains a string property for the binding name.
Related
I've been working on USB HID Device in embedded system and C# for a while. I decided to use USBHid library in C#. I got the ideal result with this library. But I have a problem. While defining the USB in the USBHid library, the following code is sufficient in the project of the library that I found on the internet.
public UsbHidDevice Device;
Device = new UsbHidDevice(vvvv,pppp);
However, when I use the same library, it asks me for an expression in the following format.
public UsbHidDevice Device; string vidandpid =
"\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49}";
Device = new UsbHidDevice(vidandpid);
I adapted this format for my own HID device, but without success. How should this string expression be? I am open to your views. Thank you from now.
How to find USB HID DevicePath?
I gave up on USBHID library and found solution with HidLibrary library. As far as I understand, HidLibrary falls short on some issues.
Here I am sharing the C# code that I linked with HidLibrary. Thank you for all the replies.
device = HidDevices.Enumerate(VendorID, ProductID).FirstOrDefault();
if (device != null)
{
device.OpenDevice();
device.Inserted += DeviceAttachedHandler;
device.Removed += DeviceRemovedHandler;
device.MonitorDeviceEvents = true;
device.ReadReport(myfunction);
}
else { RtBox_Feedback.AppendText("NOT DEVICE!"); }
\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49} - is a device interface for the {eeof37d0-1963-47k4-aa41-74476db7uf49} interface. It is almost always unique and semi-random for each device. It also may change if yore put device in another USB slot. This string may be used as path to open this "file" with CreateFile Win32 API and talk with device by means of interface-specific IOCTLs. More info here.
For HID devices Windows have another device interface GUID - GUID_DEVINTERFACE_HID - {4D1E55B2-F16F-11CF-88CB-001111000030}
You can use CM_Get_Device_Interface_ListW or SetupDiGetClassDevs/SetupDiEnumDeviceInterfaces/SetupDiGetDeviceInterfaceDetail APIs to enumerate device interfaces by interface GUID or by device Instance ID.
Mentioned "USBHid library in C#" may already have code that doing such enumeration and filtering by HID deivce VID/PID but I cannot say it for sure since you haven't added link to the code of this library. :)
I'm trying to understand how I might access the RSSI of a Bluetooth (not LE) connection in either C# or C++ on Windows.
My understanding is that there is no straightforward "GetRSSI()" type command but is there any indirect way to access it?
Everything I've found so far seems to be aimed at LE connections.
Edit:
I've had a look into AEPs and tried to get the SignalStrength AEP from a connected BT device.
foreach (var key in deviceInformation.Properties.Keys)
{
Debug.WriteLine($"{key}: {deviceInformation.Properties.GetValueOrDefault(key)}");
}
Gives:
System.ItemNameDisplay: <ommitted>
System.Devices.DeviceInstanceId:
System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2001
System.Devices.GlyphIcon: C:\Windows\System32\DDORes.dll,-3001
System.Devices.InterfaceEnabled:
System.Devices.IsDefault:
System.Devices.PhysicalDeviceLocation:
System.Devices.ContainerId:
With the item name omitted by me.
So it looks like there are no AEPs, unless I'm missing something?
I know this is late, but I just started a new project where I also want information about the SignalStrength for Bluetooth (not LE) devices.
#Mike-Petrichenko was giving you some good hints. After following his advice of searching for "System.Devices.Aep.SignalStrength" I found this post
After going throw the OPs Code and debugging a little, I came up with this solution:
private const string SignalStrengthProperty = "System.Devices.Aep.SignalStrength";
var additionalProperties = new[] { SignalStrengthProperty };
DeviceWatcher mWatcher = DeviceInformation.CreateWatcher(BluetoothDevice.GetDeviceSelector(), additionalProperties);
var rssi = Convert.ToInt16(deviceInformation.Properties[SignalStrengthProperty]);
This program is an audio visualizer for an rgb keyboard that listens to windows' default audio device. My audio setup is a bit more involved, and I use way more than just the default audio device. For instance, when I play music from Winamp it goes through the device Auxillary 1 (Synchronous Audio Router) instead of Desktop Input (Synchronous Audio Router) which I have set as Default. I'd like to be able change the device that the program listens to for the visualization.
I found in the source where the audio device is declared; Lines 32-36 in CSCoreAudioInput.cs:
public void Initialize()
{
MMDevice captureDevice = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
WaveFormat deviceFormat = captureDevice.DeviceFormat;
_audioEndpointVolume = AudioEndpointVolume.FromDevice(captureDevice);
}
The way that I understand it from the documentation, the section MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console) is where Windows gives the application my default IMMEndpoint "Desktop Input."
How would I go about changing DefaultAudioEndpoint?
Further Reading shows a few ways to get an IMMDevice, with DefaultAudioEnpoint being one of them. It seems to me that I'd have to enumerate the devices, and then separate out Auxillary 1 (Synchronous Audio Router) using PKEY_Device_FriendlyName. That's a bit much for me, as I have little to no C# experience. Is there an easier way to go about choosing a different endpoint? Am I on the right track? or am I missing the mark completely?
Also, what is the difference between MMDevice and IMMDevice? The source only seems to use MMDevice while all the Microsoft documentation references IMMDevice.
Thanks.
I DID IT!
I've found why the program uses MMDevice rather than IMMDevice. The developer has chosen to use the CSCore Library rather than Windows' own Core Audio API.
From continued reading of the CSCore MMDeviceEnumerator Documentation, it looks like I'll have to make a separate program that outputs all endpoints and their respective Endpoint ID Strings. Then I can substitute the DefaultAudioEndpoint method with the GetDevice(String id) method, where String id is the ID of whichever Endpoint I chose from the separate program.
To find the the Endpoint I wanted, I wrote this short program to find all the info I wanted:
static void Main(string[] args)
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection collection = enumerator.EnumAudioEndpoints(DataFlow.Render,DeviceState.Active);
Console.WriteLine($"\nNumber of active Devices: {collection.GetCount()}");
int i = 0;
foreach (MMDevice device in collection){
Console.WriteLine($"\n{i} Friendly name: {device.FriendlyName}");
Console.WriteLine($"Endpoint ID: {device.DeviceID}");
i++;
}
Console.ReadKey();
}
This showed me that the Endpoint I wanted was item number 3 (2 in an array) on my list, and instead of using GetDevice(String id) I used ItemAt(int deviceIndex).
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection collection = enumerator.EnumAudioEndpoints(DataFlow.Render,DeviceState.Active);
MMDevice captureDevice = collection.ItemAt(2);
However in this case, the program was not using captureDevice to bring in the audio data. These were the magic lines:
_capture = new WasapiLoopbackCapture(100, new WaveFormat(deviceFormat.SampleRate, deviceFormat.BitsPerSample, i));
_capture.Initialize();
I found that WasapiLoopbackCapture uses Windows' default device unless changed, and the code was using DefaultAudioEndpoint to get the properties of the default device. So I added
_capture.Device = captureDevice;
//before
_capture.Initialize();
And now the program properly pulls the audio data off of my non-default audio endpoint.
I had been asked to solve a similar type of problem this week. Although there are a few librarys to do this I was specifically asked to do this for "non ish" programmers so I developed this in PowerShell.
Powershell default audio device changer - Github
Maybe you can alter it to your needs.
I try to write to a NdisProt driver and send raw ethernet packets. I imported some C++ comands to my C# program, so that I can access the driver. When I try to open a handle to the driver I always get a invalid handle. I already tried it with just "NdisProt" as the path, but it didn't solve it. Do you have any suggestions why i get a invalid handle?
private bool OpenDriver()
{
// User the CreateFile API to open a handle to the file
this.m_iHandle = CreateFile("\\\\.\\NdisProt,
GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// Check to see if we got a valid handle
if ((int)m_iHandle <= 0)
{
// If not, then return false and reset the handle to 0
this.m_iHandle = IntPtr.Zero;
return false;
}
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
Thanks for any help!
Update: I just solved the problem by adding another manifest, so that the application is run as admin.
The solution with the NDIS Driver did still not work so I searched for another solution. I found the SharpPcap library. With that library I am able to modify the packets I want to send e.g. change the Destination-MAC-Adress.
Thanks for your answers!
If you now any other solutions to send raw ethernet packets in a C#
program please let me know.
What about Socket class?
Constructor:
public Socket (System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType),
where socketType can take the next value: SocketType.Raw
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
You can import functions from Windows Winsock library and use SOCK_RAW flag when creating a socket.
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
I just got myself into using the SerialPort object in C# and I realised it throws an exception saying that "COM1" does not exist.
I checked my device manager to see what COM ports I can use, but is there a way to find out what COM ports are available and programmatically select one of them?
Yes, use SerialPort.GetPortNames(), which returns an array of strings of available port names.
Then create your SerialPort object by specifying one of the names in the constructor.
string[] ports = SerialPort.GetPortNames();
SerialPort port = new SerialPort(ports[0]); // create using first existing serial port, for example
One-liner :
if(SerialPort.GetPortNames().ToList().Contains(comportName))
{
port = new SerialPort(comportName)
}
Here is another way
string portExists = SerialPort.GetPortNames().Any(x => x == "COM1");