From 872ecdcdcfa857bb679f95dc1adc10ad91a14b5e Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Sun, 15 Nov 2015 23:33:55 +0000 Subject: [PATCH] =?UTF-8?q?testusb=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestUsb.sln | 20 ++++++ TestUsb/App.config | 6 ++ TestUsb/Program.cs | 105 +++++++++++++++++++++++++++++ TestUsb/Properties/AssemblyInfo.cs | 36 ++++++++++ TestUsb/TestUsb.csproj | 61 +++++++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 TestUsb.sln create mode 100644 TestUsb/App.config create mode 100644 TestUsb/Program.cs create mode 100644 TestUsb/Properties/AssemblyInfo.cs create mode 100644 TestUsb/TestUsb.csproj diff --git a/TestUsb.sln b/TestUsb.sln new file mode 100644 index 0000000..18b3b95 --- /dev/null +++ b/TestUsb.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUsb", "TestUsb\TestUsb.csproj", "{90E06CAE-E0B8-4033-8730-1C6506E59CD2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {90E06CAE-E0B8-4033-8730-1C6506E59CD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {90E06CAE-E0B8-4033-8730-1C6506E59CD2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {90E06CAE-E0B8-4033-8730-1C6506E59CD2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {90E06CAE-E0B8-4033-8730-1C6506E59CD2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TestUsb/App.config b/TestUsb/App.config new file mode 100644 index 0000000..fad249e --- /dev/null +++ b/TestUsb/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TestUsb/Program.cs b/TestUsb/Program.cs new file mode 100644 index 0000000..9c2c428 --- /dev/null +++ b/TestUsb/Program.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LibUsbDotNet; +using LibUsbDotNet.Main; + +namespace TestUsb +{ + class Program + { + public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0xDEAD, 0xBEEE); + public static UsbDevice MyUsbDevice; + public static UsbEndpointReader reader; + public static UsbEndpointWriter writer; + + + static void Main(string[] args) + { + if (connect()) + { + config(); + while (true) + { + read(); + } + } + } + + private static String send = ""; + + private static void read() + { + ErrorCode ec = ErrorCode.None; + + if (Console.KeyAvailable) + { + ConsoleKeyInfo k = Console.ReadKey(true); + if (k.Key == ConsoleKey.Enter) + { + send += "\r\n"; + int bytesWritten; + ec = writer.Write(Encoding.Default.GetBytes(send), 2000, out bytesWritten); + Console.WriteLine("OUT: (" + bytesWritten + ") " + send); + send = ""; + } + else + { + send += k.KeyChar; + } + } + + byte[] readBuffer = new byte[1024]; + int bytesRead; + ec = reader.Read(readBuffer, 100, out bytesRead); + + if (bytesRead != 0) + { + // Write that output to the console. + Console.Write("IN: (" + bytesRead + ") "+Encoding.Default.GetString(readBuffer, 0, bytesRead)); + } + if (ec != ErrorCode.None && ec != ErrorCode.IoTimedOut) + { + //throw new Exception(ec.ToString()); + if (connect()) + { + config(); + } + System.Threading.Thread.Sleep(1000); + } + } + + private static void config() + { + 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.Ep02); + + // open write endpoint 1. + writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01); + + } + + private static bool connect() + { + MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder); + if (MyUsbDevice == null) + return false; + return true; + } + } +} diff --git a/TestUsb/Properties/AssemblyInfo.cs b/TestUsb/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ab2a660 --- /dev/null +++ b/TestUsb/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("TestUsb")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestUsb")] +[assembly: AssemblyCopyright("Copyright © 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("3203f19b-27cf-4084-923b-b5dd68ccbeae")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TestUsb/TestUsb.csproj b/TestUsb/TestUsb.csproj new file mode 100644 index 0000000..96d6189 --- /dev/null +++ b/TestUsb/TestUsb.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {90E06CAE-E0B8-4033-8730-1C6506E59CD2} + Exe + Properties + TestUsb + TestUsb + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + E:\Programme\LibUsbDotNet\LibUsbDotNet.dll + + + + + + + + + + + + + + + + + + + \ No newline at end of file