I am trying to write a program that uses dual ported RAM. I have a made a pointer to the memory address listed in the resources for the device in device manager. But every time I try to read from it, I get an Access Violation, what I am doing wrong? According to the device manufacturer, an offset of 0x0800 is open to read and write.
IntPtr ptr = new IntPtr(0xF7E00000);
float value = Marshal.ReadInt32(ptr, 0x0800);
MessageBox.Show(value.ToString());
If this is a physical address, you need a device driver. You can't create a device driver with .NET. If the device comes with a driver, it probably has an API you can call.
If you really need to write a driver, I'd recommend downloading the DDK from Microsoft, and learning C and kernel-mode programming. This is not a simple task.
Related
I am currently iterating through a list of USB Cameras connected to my computer that I retrieved with the following code:
DsDevice[] cams = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
I then determine the HardwareID and InstanceID values from the DevicePath for each device, and I look up the following registry key for all devices:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB{HardwareID}{InstanceID}\
I then read the LocationInformation subkey value. Here are some examples of what I'm receiving based on which USB port the device is plugged in to:
0000.001a.0000.001.003.000.000.000.000
0000.001a.0000.001.004.000.000.000.000
0000.001d.0000.001.008.000.000.000.000
0000.001d.0000.001.007.000.000.000.000
0000.001d.0000.001.005.000.000.000.000
0000.001d.0000.001.006.000.000.000.000
It appears that the second block (index 1 of the returned Split('.') array) is a distinction between the internal USB hub that the USB port is connected to. Is this correct?
I have deduced that the fifth block (index 4) is the USB port number, but on a separate computer I'm using (an Intel Compute Stick), the port number differentiation actually occurs in the fourth block (index 3). Is there a way to know which block I need to check based on the computer? (Third-party libraries are acceptable though I'd much rather use built-in functionality of .NET or straight Windows APIs that I can hook into, if possible.)
Is there any documentation on what each section of the LocationInformation is? I have Googled for information, but I've come up short of what I'm looking for. Perhaps I just don't know the best search terms to find this information. In any case, I'd bet that this documentation could be extremely useful to other StackOverflow users as well.
Thanks!
I have a USB mass storage device that I encrypt with TrueCrypt. When I connect it to Windows, it asks to format. Using TrueCrypt reveals its contents, as expected.
How can I read the first 100 bytes of that device?
I know the bytes will not make sense because they're encrypted but I want to read them in order to create a checksum.
Did this on the top of my head. But is should work.
public static long getBytes(string letter)
{
ManagementObject disk = new ManagementObject(String.Format("win32_logicaldisk.deviceid=\"{0}:\"", letter));
disk.Get();
return long.Parse(disk["Size"].ToString());
}
EDIT: Tested it and changed int to long. It works.
What solutions have you considered so far? Does your application figure out when the USB device is plugged in or unplugged?
As far as I know, there's no native support in .Net for directly accessing USB devices. I had to use libraries such as LibUsbDotNet (http://sourceforge.net/projects/libusbdotnet/) or SharpUSBLib (http://www.icsharpcode.net/OpenSource/SharpUSBLib/) There were pros and cons to both in terms of samples, documentation etc. I am sure you will be able to find what suits you best.
In one case I was able to connect to the device using WMDC, once the connection was established I used OpenNETCF RAPI library to read from / write to the device.
Here's another excellent resource that I had found useful when I wrote an application that needed to interact with a USB device (a barcode scanner).
http://www.developerfusion.com/article/84338/making-usb-c-friendly/
There was a good resourceful discussion to a similar question here on Stackoverflow : Working with USB devices in .NET
I have to make a program which monitor usb ports and when an usb device is plugged (joypad, flash drives, mouse, ecc...) I get a unique identifier (a deviceid or something else would be good).
At first I tried with C# using the system.management classes and querying the cim_logicaldevice class each second to get the new device plugged.
Some device returned more rows with DeviceID, but this isn't a problem. The problem is that the memory occupied by the program (in task manager) grows up constantly.
This is the source code:
http://pastebin.com/dQv3cMQC
Is there a way to avoid the growing of the memory usege?
I have to do this program in C++ or C# and it has to be the most efficient possible (because it has to be opened forever).
I would recommend looking at the USBView sample in the WDK. If you are unfamiliar with this, simply run it - this tool walks the entire USB tree on the system and prints out information and descriptor listings for each device.
In your case, I'd start at the RefreshTree() function in this sample, you can then follow the code to see how it enumerates the host controllers, hubs and finally devices. For each device that you find you can look at the bInterfaceClass in the interface descriptors to find out what types of interfaces it is advertising.
The easiest way to get the source to this sample is to install the 7.1.0 WDK which is currently available here: http://www.microsoft.com/en-us/download/details.aspx?id=11800
I am adapting some test software that is used to upload a .bin file to one of our products via USB. The product has a Atmel AT91SAM7X256 processor. The software is all written in c# and is running on a windows XP machine.
The previous programmer (no longer with the company) uses a dll file called AT91BootDLL.dll to scan USB ports for an ateml processor and obtain an handle on it. Then it can open a USB connection and do various things like read, write etc. This all works fine but every now and then it can no longer find anything connected. When this happens I can no longer connect anything to the PC via USB as windows fails to detect it. I then have to shut-down the PC and when doing this windows decides to freeze.
I need to get to the bottom of it and as I have no experience with either this dll or embedded programming I only have these conclusions. Either the processor is doing something causing the USB ports to lock-up or this dll file is doing it.
Is there anything I can do in the program that might help prevent this?
For uploading and then reading back from this processor, does anyone know of an alternative method?
Here is some of the code used in scanning and opening.
private static AT91BootDLL at91 = new AT91BootDLL();
private static unsafe int GetHandle()
{
// Create a dummy list for the dummy SAM-BA interface ;-)
byte* dummyName = stackalloc byte[100];
byte** dummyList = stackalloc byte*[100]; // { n1, n2, n3, n4, n5 };
for (int i = 0; i < 100; ++i)
{
dummyList[i] = dummyName;
}
// This function needs to be called before a USB connection can be opened.
at91.AT91Boot_Scan(ref *(byte*)dummyList);
// Now that the scan function has been called, we may open an USB connection.
int handle;
byte[] name = Encoding.ASCII.GetBytes("\\usb\\ARM0\x00");
at91.AT91Boot_Open(ref name[0], out handle);
return handle;
}
You can download the latest SAM-BA kit here so make sure you have the latest version of everything:
http://www.atmel.com/tools/ATMELSAM-BAIN-SYSTEMPROGRAMMER.aspx
But looking at the above code it's pretty much relying on the Atmel supplied routines. There may be other errors in the calling logic and/or firmware but unless you post complete code I don't think anyone will be able to help you. If you don't want to post it maybe try Atmel engineering there support is OK if you're a volume customer.
Personally I've always coded my own bootloaders for the AT91SAM series using the CDC interface to write the new image to external FLASH from the main application and then setting a flag and reboot to load it up to get rid of external dependencies, but that is a reasonably large undertaking.
Could anyone please give me any idea as to where to start my coding in order to get data from OPOS(Datalogic Magellan device) weighting and barcode scanning in C#?? For example, what library and what function I should be using for this case. I am clueless as I have already spent numerous of hours searching for an answer online. Not even came close online.
I don't know any api that I can use to get the weight and barcode for the usb device into my C# program.
I am currently using Datalogic scale. I tried the build-in windows reader but it didn't read in any data from the device.
First off, I used the Microsoft.PointOfService library which can directly create connection to most of the opos base machine. And make sure you have your Logical Device Name right! Very Very important. This is NOT any normal name you found in your regedit, it MUST be define manually by yourself inside the opos adk program that you installed along with the opos machine.
Then you can pass in the name as usual in your C# program.
For example: you set USB_Scale as your logical device name inside OPOS program
in C#
this.myDevice = explore.GetDevice("Scale", "USB_Scale");
Note: Make sure you set claim to 1000; It might not work if you didn't do so.
Also : this.myScale = ((Scale)explore.CreateInstance(myDevice)); <- this might help~
The rest is just straight forward.