本文整理汇总了C#中LibUsbDotNet.UsbDevice.OpenEndpointWriter方法的典型用法代码示例。如果您正苦于以下问题:C# UsbDevice.OpenEndpointWriter方法的具体用法?C# UsbDevice.OpenEndpointWriter怎么用?C# UsbDevice.OpenEndpointWriter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LibUsbDotNet.UsbDevice
的用法示例。
在下文中一共展示了UsbDevice.OpenEndpointWriter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TraqpaqDevice
/// <summary>
/// Class constructor for the traq|paq
/// </summary>
public TraqpaqDevice()
{
// find the device
traqpaqDeviceFinder = new UsbDeviceFinder(Constants.VID, Constants.PID);
// open the device
this.MyUSBDevice = UsbDevice.OpenUsbDevice(traqpaqDeviceFinder);
// If the device is open and ready
if (MyUSBDevice == null) throw new TraqPaqNotConnectedException("Device not found");
this.reader = MyUSBDevice.OpenEndpointReader(ReadEndpointID.Ep01);
this.writer = MyUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
// create the battery object
this.battery = new Battery(this);
// get a list of saved tracks
getAllTracks();
// get the record table data
tableReader = new RecordTableReader(this);
tableReader.readRecordTable();
this.recordTableList = tableReader.recordTable;
}
示例2: USBFadecandy
public USBFadecandy(int pixelCount = 0, string serialNumber = "") : base(pixelCount)
{
if(pixelCount > 512)
throw new ArgumentOutOfRangeException("pixelCount");
var packets = pixelCount/21; // Can fit 21 pixels into each packet
activeBuffer = new byte[64 * packets]; // Each packet needs to be 64 bytes
queuedBuffer = new byte[64 * packets];
// Setup control bytes for all packets. Never wiped out, only needs to be done once.
for (int i = 0; i < packets; i++)
{
byte packet_index = (byte)(i & 0x1F); // Bits 0-4, packet index
byte final = (byte)(i == packets - 1 ? 1 : 0); // Bit 5, final bit (denotes last packet)
//byte type = 0; // Bits 6-7, type code (0 for video data)
activeBuffer[i*64] = (byte)(packet_index & (final >> 5)); // First byte in each packet is a bitfield
}
Array.Copy(activeBuffer, queuedBuffer, activeBuffer.Length);
dirty = false;
UsbDeviceFinder usbFinder = string.IsNullOrEmpty(serialNumber) ? new UsbDeviceFinder(0x1d50, 0x607a) : new UsbDeviceFinder(0x1d50, 0x607a, serialNumber);
ErrorCode ec = ErrorCode.None;
// Find and open the usb device.
fcdevice = UsbDevice.OpenUsbDevice(usbFinder);
// If the device is open and ready
if (fcdevice == 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 = fcdevice 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 write endpoint 1.
writer = fcdevice.OpenEndpointWriter(WriteEndpointID.Ep01);
}
示例3: GarminUnit
public GarminUnit(UsbDevice Device)
{
Configuration = new Dictionary<string, ushort>();
IUsbDevice wholedevice = Device as IUsbDevice;
if ( !ReferenceEquals(wholedevice, null))
{
wholedevice.SetConfiguration(1);
wholedevice.ClaimInterface(0);
}
Reader = Device.OpenEndpointReader(ReadEndpointID.Ep01);
Writer = Device.OpenEndpointWriter(WriteEndpointID.Ep02);
}
示例4: SmartScopeUsbInterfaceLibUsb
public SmartScopeUsbInterfaceLibUsb(UsbDevice usbDevice)
{
if (usbDevice is IUsbDevice)
{
bool succes1 = (usbDevice as IUsbDevice).SetConfiguration(1);
if (!succes1)
throw new ScopeIOException("Failed to set usb device configuration");
bool succes2 = (usbDevice as IUsbDevice).ClaimInterface(0);
if (!succes2)
throw new ScopeIOException("Failed to claim usb interface");
}
device = usbDevice;
serial = usbDevice.Info.SerialString;
dataEndpoint = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
commandWriteEndpoint = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
commandReadEndpoint = usbDevice.OpenEndpointReader(ReadEndpointID.Ep03);
Common.Logger.Debug("Created new ScopeUsbInterface from device with serial " + serial);
}
示例5: PolhemusStream
public PolhemusStream()
{
try
{
// Find and open the usb device.
PolhemusUsbDevice = UsbDevice.OpenUsbDevice(UsbFinder);
// If the device is open and ready
if (PolhemusUsbDevice == null) throw new Exception("Polhemus 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 = PolhemusUsbDevice 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);
}
//Polhemus uses EndPoint 2 for both read and write
PolhemusReader = PolhemusUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
PolhemusWriter = PolhemusUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
}
catch (Exception e)
{
throw new Exception("Error in PolhemusStream constructor: " + e.Message);
}
}
示例6: OpenDevice
private void OpenDevice()
{
if(_usbDevice != null && _usbDevice.IsOpen)
return;
// Find and open the usb device.
_usbDevice = UsbDevice.OpenUsbDevice(_usbFinder);
// If the device is open and ready
Debug.Assert(_usbDevice != null, string.Format("No device with PID: {0:X4} & VID: {1:X4} Found!", _pid, _vid));
// 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.
var wholeUsbDevice = _usbDevice 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.
_epReader = _usbDevice.OpenEndpointReader((ReadEndpointID) EpIn);
_epReader.Flush();
// open write endpoint 1.
_epWriter = _usbDevice.OpenEndpointWriter((WriteEndpointID) EpOut);
LibMain.OnConnectedChanged(true);
}
示例7: Battery
public Battery(UsbDevice usb = null)
{
usbDevice = usb;
// 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 = usbDevice 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 batInfoReader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
ErrorCode ec = ErrorCode.None;
byte[] batInfoReadBuffer = new byte[64];
int bytesRead = 0;
// If the device hasn't sent data in the last 100 milliseconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = batInfoReader.Read(batInfoReadBuffer, 100, out bytesRead);
if (bytesRead == 0) throw new Exception("No more bytes!");
// create BinaryReader and set source to readBuffer
BatteryBinaryReader batInfoBReader = new BatteryBinaryReader(batInfoReadBuffer);
// set battery variables using binary reader
fwVersion = batInfoBReader.ReadByte() + ((float) batInfoBReader.ReadByte()) / 10;
id = new string(batInfoBReader.ReadChars(6));
name = new string(batInfoBReader.ReadChars(20));
currentTime = batInfoBReader.ReadInt32();
lastSyncTime = batInfoBReader.ReadInt32();
capacity = batInfoBReader.ReadUInt16();
lastFullCap = batInfoBReader.ReadUInt16();
factoryCap = batInfoBReader.ReadUInt16();
percentage = 100 * capacity / lastFullCap;
int eepromPages = batInfoBReader.ReadUInt16();
// get unix time
int time = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
// check if battery time is correct
if (currentTime != time || currentTime != time - 1)
{
// open write endpoint 1
UsbEndpointWriter writer = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
// convert time to byte array
byte[] timeArray = BitConverter.GetBytes(time);
int bytesWritten = 0;
// write time to endpoint 1
ec = writer.Write(timeArray, 100, out bytesWritten);
}
UsbTransfer usbReadTransfer;
UsbEndpointReader dataReader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
int bytesToRead = 128 * eepromPages;
byte[] dataReadBuffer = new byte[bytesToRead];
// reset error code
ec = ErrorCode.None;
// read logged data using async bulk transfer
ec = dataReader.SubmitAsyncTransfer(dataReadBuffer, 0, bytesToRead, 100, out usbReadTransfer);
// dispose of the usb transfer
usbReadTransfer.Dispose();
// create a string array with the lines of text
var dataReadString = System.Text.Encoding.Default.GetString(dataReadBuffer);
// Set a variable to the My Documents path
string docPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// write the string array to a new file named "DAS_battery.txt".
using (StreamWriter outputFile = new StreamWriter(docPath + @"\DAS_battery.txt"))
{
outputFile.Write(dataReadString);
}
}
示例8: Open
public bool Open()
{
bool success = true;
//
try
{
// Find and open the usb device.
myUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
//
// If the device is open and ready
if (myUsbDevice == null) throw new Exception("X10 CM15Pro device not connected.");
//
// 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.
reader = myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// open write endpoint 2.
writer = myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
//
this.WriteData(new byte[] { 0x8B }); // status request
}
catch
{
success = false;
//throw new Exception("Error opening X10 CM15Pro device.");
}
return success;
}
示例9: Main
public static void Main(string[] args)
{
ErrorCode ec = ErrorCode.None;
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();
//.........这里部分代码省略.........
示例10: Open
public void Open()
{
if (_portType == PortType.COM)
{
_serialPort.Open();
_isOpen = true;
}
if (_portType == PortType.USB)
{
// Find and open the usb device.
_usbDevice = UsbDevice.OpenUsbDevice(_usbFinder);
// If the device is open and ready
if (_usbDevice == 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 = _usbDevice 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 write endpoint 1.
_usbWriter = _usbDevice.OpenEndpointWriter(_endpointId);
_isOpen = true;
}
if (_portType == PortType.EtherNet)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(_ipEndPoint);
_socket.SendTimeout = 1000;
_isOpen = true;
}
}
示例11: Connect
public static bool Connect()
{
ErrorCode ec = ErrorCode.None;
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 angle "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 angle
// device.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is angle "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(1);
}
// open write endpoint 1.
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
return false;
}
Connected = true;
return true;
}
示例12: Open
// USB connection and adapter management
public override void Open()
{
this.Close();
try
{
caLibUsbAdapter.write_lock.WaitOne();
usb_finder = new UsbDeviceFinder(this.vid, this.pid);
Debug.Assert(this.usb_finder != null);
// open device
usb_device = UsbDevice.OpenUsbDevice(usb_finder);
if (usb_device == null)
{
throw new Exception("No compatible adapters found");
}
wholeUsbDevice = usb_device as IUsbDevice;
if (!ReferenceEquals (wholeUsbDevice, null)) {
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(1);
} else {
throw new Exception("Failed to claim interface");
}
// open endpoints
ep_reader = usb_device.OpenEndpointReader(this.read_ep_id);
ep_writer = usb_device.OpenEndpointWriter(this.write_ep_id);
if(ep_reader == null || ep_writer == null)
{
throw new Exception("Failed to open endpoints");
}
// clear garbage from input
this.ep_reader.ReadFlush();
// attach read event
ep_reader.DataReceived += (read_usb);
ep_reader.DataReceivedEnabled = true;
} catch (Exception e) {
this.Close();
throw e;
} finally {
caLibUsbAdapter.write_lock.ReleaseMutex();
}
}
示例13: button2_Click
private void button2_Click(object sender, EventArgs e)
{
try
{
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUsbDevice == null)
{
throw new Exception("Device Not Found.");
//connected = false;
}
else
{
Device_l.Text = "Device: Connected";
//connected = true;
Scan_b.Enabled = true;
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);
}
reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep03);
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep04);
}
}
catch (Exception ex)
{
MessageBox.Show((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
}
示例14: openAsTestDevice
private bool openAsTestDevice(UsbRegistry usbRegistry)
{
if (!ReferenceEquals(mUsbDevice, null))
closeTestDevice(mUsbDevice);
mUsbDevice = null;
try
{
if (usbRegistry.Open(out mUsbDevice))
{
UsbInterfaceInfo readInterfaceInfo;
UsbInterfaceInfo writeInterfaceInfo;
UsbDevice.UsbErrorEvent += OnUsbError;
if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
0x80,
out readInterfaceInfo,
out mReadEndpointInfo))
{
throw new Exception("failed locating read endpoint.");
}
mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mReadEndpointInfo.Descriptor.MaxPacketSize));
if (!UsbEndpointBase.LookupEndpointInfo(mUsbDevice.Configs[0],
0x00,
out writeInterfaceInfo,
out mWriteEndpointInfo))
{
throw new Exception("failed locating write endpoint.");
}
if (((mWriteEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous) ||
((mReadEndpointInfo.Descriptor.Attributes & 3)==(int) EndpointType.Isochronous))
{
throw new Exception("buenchmark GUI application does not support ISO endpoints. Use BenchmarkCon instead.");
}
mBenchMarkParameters.BufferSize -= (mBenchMarkParameters.BufferSize%(mWriteEndpointInfo.Descriptor.MaxPacketSize));
if (writeInterfaceInfo.Descriptor.InterfaceID != readInterfaceInfo.Descriptor.InterfaceID)
throw new Exception("read/write endpoints must be on the same interface.");
mEP1Reader = mUsbDevice.OpenEndpointReader(
(ReadEndpointID)mReadEndpointInfo.Descriptor.EndpointID,
mBenchMarkParameters.BufferSize,
(EndpointType)(mReadEndpointInfo.Descriptor.Attributes & 3));
mEP1Writer = mUsbDevice.OpenEndpointWriter(
(WriteEndpointID) mWriteEndpointInfo.Descriptor.EndpointID,
(EndpointType) (mWriteEndpointInfo.Descriptor.Attributes & 3));
mInterfaceInfo = writeInterfaceInfo;
mEP1Reader.ReadThreadPriority = mBenchMarkParameters.Priority;
mEP1Reader.DataReceived += OnDataReceived;
makeTestBytes(out loopTestBytes, mBenchMarkParameters.BufferSize);
// 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 = mUsbDevice 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(mInterfaceInfo.Descriptor.InterfaceID);
}
return true;
}
}
catch(Exception ex)
{
SetStatus(ex.Message, true);
}
if (!ReferenceEquals(mUsbDevice,null))
{
try
{
closeTestDevice(mUsbDevice);
}
finally
{
mUsbDevice = null;
mEP1Reader = null;
mEP1Writer = null;
}
}
return false;
}
示例15: UsbDeviceFinder
public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x0F44, 0xEF12); //Polhemus Patriot
#endregion Fields
#region Methods
public static void Main(string[] args)
{
byte[] writeBuffer;
byte[] readBuffer;
ErrorCode ec = ErrorCode.None;
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);
}
//Polhemus uses EndPoint 2 for both read and write
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
while (true)
{
Console.Write("New command: ");
string cmdLine = Console.ReadLine();
if (cmdLine == "") break;
if (cmdLine.Substring(0, 1) == "^") //control code
{
int ib = Encoding.ASCII.GetBytes(cmdLine.Substring(1, 1))[0];
writeBuffer = new byte[cmdLine.Length];
writeBuffer[0] = (byte)(ib & 0x1F); //calculate control code
if (cmdLine.Length > 2) //then there are arguments
{
byte[] arg = Encoding.ASCII.GetBytes(cmdLine.Substring(2)); //get arguments
for (int i = 0; i < arg.Length; i++) //and place in buffer
writeBuffer[i + 1] = arg[i];
}
writeBuffer[writeBuffer.Length - 1] = 0x0D; //CR
}
else //straight ASCII string command
writeBuffer = Encoding.ASCII.GetBytes(cmdLine +
((cmdLine != "P") ? "\r" : ""));
int bytesWritten;
ec = writer.Write(writeBuffer, 1000, out bytesWritten);
if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
// If the device hasn't sent data in the last 100 milliseconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = reader.Read(readBuffer, 100, out bytesRead);
if (bytesRead == 0) break;
// Write that output to the console.
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
int byteCount = 1;
for (int i = 0; i < bytesRead; i++, byteCount++)
{
Console.Write(readBuffer[i].ToString("X2"));
if (byteCount >= 16)
{
byteCount = 0;
Console.Write(Environment.NewLine);
}
else
Console.Write(" ");
}
if (byteCount != 0) Console.Write(Environment.NewLine);
}
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
//.........这里部分代码省略.........