andplusdesign
asked on
Accessing a USB Bill Acceptor via USB in .NET
Hello,
I am using the attached example code for LibUSBDotNet, and I'm not able to access any USB device at all, regardless of the values I put in to the usbfinder method.
Needless to say, no matter how many times I run this, debug this, or trace things, I am receiving "Device Not Found." on the console.
I am *not* married to LibUsbDotNet, I simply need to speak to this USB device.
The USB device is a Matrix VTI bill acceptor, and ships with an example program and clear explanations as to how to interface with it - but I can't send or receive any of the data to it if I can't access the device.
http://www.validationtech.com/sellsheet/matrix%20sell%20150.pdf
Thanks to anyone that can help!
-Sean
I am using the attached example code for LibUSBDotNet, and I'm not able to access any USB device at all, regardless of the values I put in to the usbfinder method.
Needless to say, no matter how many times I run this, debug this, or trace things, I am receiving "Device Not Found." on the console.
I am *not* married to LibUsbDotNet, I simply need to speak to this USB device.
The USB device is a Matrix VTI bill acceptor, and ships with an example program and clear explanations as to how to interface with it - but I can't send or receive any of the data to it if I can't access the device.
http://www.validationtech.com/sellsheet/matrix%20sell%20150.pdf
Thanks to anyone that can help!
-Sean
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.LudnMonoLibUsb;
namespace Examples
{
internal class ReadWriteAsync
{
public static UsbDevice MyUsbDevice;
#region "SET YOUR USB Vendor and Product ID!"
public static UsbDeviceFinder MyUsbFinder;
#endregion
public static void Main(string[] args)
{
ErrorCode ec = ErrorCode.None;
MyUsbFinder = new UsbDeviceFinder(3301, 1);
try
{
// Find and open the usb device.
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
// If the device is open and ready
if (MyUsbDevice == null) throw new Exception("Device Not Found.");
// If this is a "whole" usb device (libusb-win32, linux libusb)
// it will have an IUsbDevice interface. If not (WinUSB) the
// variable will be null indicating this is an interface of a
// device.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is a "whole" USB device. Before it can be used,
// the desired configuration and interface must be selected.
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
// open read endpoint 1.
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// open write endpoint 1.
UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
// the write test data.
string testWriteString = "ABCDEFGH";
ErrorCode ecWrite;
ErrorCode ecRead;
int transferredOut;
int transferredIn;
UsbTransfer usbWriteTransfer;
UsbTransfer usbReadTransfer;
byte[] bytesToSend = Encoding.Default.GetBytes(testWriteString);
byte[] readBuffer = new byte[1024];
int testCount = 0;
do
{
// Create and submit transfer
ecRead = reader.SubmitAsyncTransfer(readBuffer, 0, readBuffer.Length, 100, out usbReadTransfer);
if (ecRead != ErrorCode.None) throw new Exception("Submit Async Read Failed.");
ecWrite = writer.SubmitAsyncTransfer(bytesToSend, 0, bytesToSend.Length, 100, out usbWriteTransfer);
if (ecWrite != ErrorCode.None)
{
usbReadTransfer.Dispose();
throw new Exception("Submit Async Write Failed.");
}
WaitHandle.WaitAll(new WaitHandle[] { usbWriteTransfer.AsyncWaitHandle, usbReadTransfer.AsyncWaitHandle },200,false);
if (!usbWriteTransfer.IsCompleted) usbWriteTransfer.Cancel();
if (!usbReadTransfer.IsCompleted) usbReadTransfer.Cancel();
ecWrite = usbWriteTransfer.Wait(out transferredOut);
ecRead = usbReadTransfer.Wait(out transferredIn);
usbWriteTransfer.Dispose();
usbReadTransfer.Dispose();
Console.WriteLine("Read :{0} ErrorCode:{1}", transferredIn, ecRead);
Console.WriteLine("Write :{0} ErrorCode:{1}", transferredOut, ecWrite);
Console.WriteLine("Data :" + Encoding.Default.GetString(readBuffer, 0, transferredIn));
testCount++;
} while (testCount < 5);
Console.WriteLine("\r\nDone!\r\n");
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
finally
{
if (MyUsbDevice != null)
{
if (MyUsbDevice.IsOpen)
{
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Release interface #0.
wholeUsbDevice.ReleaseInterface(0);
}
MyUsbDevice.Close();
}
MyUsbDevice = null;
// Free usb resources
UsbDevice.Exit();
}
// Wait for user input..
Console.ReadKey();
}
}
}
}
I assume you have installed the supplied device driver and the thing is visible in windows hardware device manager ?
ASKER
@akoster - yes, your assumption is correct.
@SameerJaqdale, those examples are the same code I pasted in the question. And in terms of the important points, yes, I followed those (i.e. verified resources).
Thanks for the help, I hope someone has a working example here...
@SameerJaqdale, those examples are the same code I pasted in the question. And in terms of the important points, yes, I followed those (i.e. verified resources).
Thanks for the help, I hope someone has a working example here...
the pdf file indicates it also can talk over RS232, have you tried communicating with it using a com port ?
also : which operating system do you use ?
also : which operating system do you use ?
ASKER
Hi Akoster, no I haven't tried communicating over a COM port - I understand that sometimes USB is just a virtual COM port? I am on Windows 7.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello andplusdesign, Iam really interested in your solution I have a matrix usb bill acceptor I can do nothing with. If you could contact me at jmbynum@sbcglobal.net I would be very grateful
however, there are some important points mentioned as well. Have you followed those?
http://libusbdotnet.sourceforge.net/V2/html/e61bb81e-108b-4f84-a77d-8ccdecebfa31.htm
http://libusbdotnet.sourceforge.net/V2/html/8564be8a-3154-4b2e-8017-5013ca8244ee.htm