- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在一个 ASP.NET 网站上工作,我需要从客户端访问 USB 设备。
我已经看到 Silverlight 5 通过使用 P/Invoke 允许我们访问客户端计算机上的 dll。我计划在我的一个页面中添加一个 Silverlight 控件,该控件将与我的 USB 设备交互。这样,每个使用这种设备的客户只需要连接到我的网站并开始使用它。
尽管如此,作为与 USB 设备进行这种交互的初学者,我该如何做到这一点?
哪个 windows dll 将为我提供与 USB 设备交互的好方法?
更多信息:
最佳答案
我找到了一个包装类,它使我能够创建到 的连接。串口在 内银光 5 .我现在可以通过串行通信访问我的 USB 设备。
由于我花了很多时间试图让它发挥作用,我将与你分享这门课:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace TestSerialDLL
{
public class SerialWrapper : IDisposable
{
#region Enum
public enum StopBits
{
None,
One,
Two,
OnePointFive,
}
public enum Parity
{
None,
Odd,
Even,
Mark,
Space,
}
#endregion
#region Fields
/// <summary>
/// The baud rate at which the communications device operates.
/// </summary>
private readonly int iBaudRate;
/// <summary>
/// The number of bits in the bytes to be transmitted and received.
/// </summary>
private readonly byte byteSize;
/// <summary>
/// The system handle to the serial port connection ('file' handle).
/// </summary>
private IntPtr pHandle = IntPtr.Zero;
/// <summary>
/// The parity scheme to be used.
/// </summary>
private readonly Parity parity;
/// <summary>
/// The name of the serial port to connect to.
/// </summary>
private readonly string sPortName;
/// <summary>
/// The number of bits in the bytes to be transmitted and received.
/// </summary>
private readonly StopBits stopBits;
#endregion
#region Constructor
/// <summary>
/// Creates a new instance of SerialCom.
/// </summary>
/// <param>The name of the serial port to connect to</param>
/// <param>The baud rate at which the communications device operates</param>
/// <param>The number of stop bits to be used</param>
/// <param>The parity scheme to be used</param>
/// <param>The number of bits in the bytes to be transmitted and received</param>
public SerialWrapper(string portName, int baudRate, StopBits stopBits, Parity parity, byte byteSize)
{
if (stopBits == StopBits.None)
throw new ArgumentException("stopBits cannot be StopBits.None", "stopBits");
if (byteSize < 5 || byteSize > 8)
throw new ArgumentOutOfRangeException("The number of data bits must be 5 to 8 bits.", "byteSize");
if (baudRate < 110 || baudRate > 256000)
throw new ArgumentOutOfRangeException("Invalid baud rate specified.", "baudRate");
if ((byteSize == 5 && stopBits == StopBits.Two) || (stopBits == StopBits.OnePointFive && byteSize > 5))
throw new ArgumentException("The use of 5 data bits with 2 stop bits is an invalid combination, " +
"as is 6, 7, or 8 data bits with 1.5 stop bits.");
this.sPortName = portName;
this.iBaudRate = baudRate;
this.byteSize = byteSize;
this.stopBits = stopBits;
this.parity = parity;
}
/// <summary>
/// Creates a new instance of SerialCom.
/// </summary>
/// <param>The name of the serial port to connect to</param>
/// <param>The baud rate at which the communications device operates</param>
/// <param>The number of stop bits to be used</param>
/// <param>The parity scheme to be used</param>
public SerialWrapper(string portName, int baudRate, StopBits stopBits, Parity parity)
: this(portName, baudRate, stopBits, parity, 8)
{
}
#endregion
#region Open
/// <summary>
/// Opens and initializes the serial connection.
/// </summary>
/// <returns>Whether or not the operation succeeded</returns>
public bool Open()
{
pHandle = CreateFile(this.sPortName, FileAccess.ReadWrite, FileShare.None,
IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (pHandle == IntPtr.Zero) return false;
if (ConfigureSerialPort()) return true;
else
{
Dispose();
return false;
}
}
#endregion
#region Write
/// <summary>
/// Transmits the specified array of bytes.
/// </summary>
/// <param>The bytes to write</param>
/// <returns>The number of bytes written (-1 if error)</returns>
public int Write(byte[] data)
{
FailIfNotConnected();
if (data == null) return 0;
int bytesWritten;
if (WriteFile(pHandle, data, data.Length, out bytesWritten, 0))
return bytesWritten;
return -1;
}
/// <summary>
/// Transmits the specified string.
/// </summary>
/// <param>The string to write</param>
/// <returns>The number of bytes written (-1 if error)</returns>
public int Write(string data)
{
FailIfNotConnected();
// convert the string to bytes
byte[] bytes;
if (data == null)
{
bytes = null;
}
else
{
bytes = Encoding.UTF8.GetBytes(data);
}
return Write(bytes);
}
/// <summary>
/// Transmits the specified string and appends the carriage return to the end
/// if it does not exist.
/// </summary>
/// <remarks>
/// Note that the string must end in '\r\n' before any serial device will interpret the data
/// sent. For ease of programmability, this method should be used instead of Write() when you
/// want to automatically execute the specified command string.
/// </remarks>
/// <param>The string to write</param>
/// <returns>The number of bytes written (-1 if error)</returns>
public int WriteLine(string data)
{
if (data != null && !data.EndsWith("\r\n"))
data += "\r\n";
return Write(data);
}
#endregion
#region Read
/// <summary>
/// Reads any bytes that have been received and writes them to the specified array.
/// </summary>
/// <param>The array to write the read data to</param>
/// <returns>The number of bytes read (-1 if error)</returns>
public int Read(byte[] data)
{
FailIfNotConnected();
if (data == null) return 0;
int bytesRead;
if (ReadFile(pHandle, data, data.Length, out bytesRead, 0))
return bytesRead;
return -1;
}
/// <summary>
/// Reads any data that has been received as a string.
/// </summary>
/// <param>The maximum number of bytes to read</param>
/// <returns>The data received (null if no data)</returns>
public string ReadString(int maxBytesToRead)
{
if (maxBytesToRead < 1) throw new ArgumentOutOfRangeException("maxBytesToRead");
byte[] bytes = new byte[maxBytesToRead];
int numBytes = Read(bytes);
//string data = ASCIIEncoding.ASCII.GetString(bytes, 0, numBytes);
string data = Encoding.UTF8.GetString(bytes, 0, numBytes);
return data;
}
#endregion
#region Dispose Utils
/// <summary>
/// Disconnects and disposes of the SerialCom instance.
/// </summary>
public void Dispose()
{
if (pHandle != IntPtr.Zero)
{
CloseHandle(pHandle);
pHandle = IntPtr.Zero;
}
}
/// <summary>
/// Flushes the serial I/O buffers.
/// </summary>
/// <returns>Whether or not the operation succeeded</returns>
public bool Flush()
{
FailIfNotConnected();
const int PURGE_RXCLEAR = 0x0008; // input buffer
const int PURGE_TXCLEAR = 0x0004; // output buffer
return PurgeComm(pHandle, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
#endregion
#region Private Helpers
/// <summary>
/// Configures the serial device based on the connection parameters pased in by the user.
/// </summary>
/// <returns>Whether or not the operation succeeded</returns>
private bool ConfigureSerialPort()
{
DCB serialConfig = new DCB();
if (GetCommState(pHandle, ref serialConfig))
{
// setup the DCB struct with the serial settings we need
serialConfig.BaudRate = (uint)this.iBaudRate;
serialConfig.ByteSize = this.byteSize;
serialConfig.fBinary = 1; // must be true
serialConfig.fDtrControl = 1; // DTR_CONTROL_ENABLE "Enables the DTR line when the device is opened and leaves it on."
serialConfig.fAbortOnError = 0; // false
serialConfig.fTXContinueOnXoff = 0; // false
serialConfig.fParity = 1; // true so that the Parity member is looked at
switch (this.parity)
{
case Parity.Even:
serialConfig.Parity = 2;
break;
case Parity.Mark:
serialConfig.Parity = 3;
break;
case Parity.Odd:
serialConfig.Parity = 1;
break;
case Parity.Space:
serialConfig.Parity = 4;
break;
case Parity.None:
default:
serialConfig.Parity = 0;
break;
}
switch (this.stopBits)
{
case StopBits.One:
serialConfig.StopBits = 0;
break;
case StopBits.OnePointFive:
serialConfig.StopBits = 1;
break;
case StopBits.Two:
serialConfig.StopBits = 2;
break;
case StopBits.None:
default:
throw new ArgumentException("stopBits cannot be StopBits.None");
}
if (SetCommState(pHandle, ref serialConfig))
{
// set the serial connection timeouts
COMMTIMEOUTS timeouts = new COMMTIMEOUTS();
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if (SetCommTimeouts(pHandle, ref timeouts))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// Helper that throws a InvalidOperationException if we don't have a serial connection.
/// </summary>
private void FailIfNotConnected()
{
if (pHandle == IntPtr.Zero)
throw new InvalidOperationException("You must be connected to the serial port before performing this operation.");
}
#endregion
#region Native Helpers
#region Native structures
/// <summary>
/// Contains the time-out parameters for a communications device.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct COMMTIMEOUTS
{
public uint ReadIntervalTimeout;
public uint ReadTotalTimeoutMultiplier;
public uint ReadTotalTimeoutConstant;
public uint WriteTotalTimeoutMultiplier;
public uint WriteTotalTimeoutConstant;
}
/// <summary>
/// Defines the control setting for a serial communications device.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct DCB
{
public int DCBlength;
public uint BaudRate;
public uint Flags;
public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public sbyte XonChar;
public sbyte XoffChar;
public sbyte ErrorChar;
public sbyte EofChar;
public sbyte EvtChar;
public ushort wReserved1;
public uint fBinary;
public uint fParity;
public uint fOutxCtsFlow;
public uint fOutxDsrFlow;
public uint fDtrControl;
public uint fDsrSensitivity;
public uint fTXContinueOnXoff;
public uint fOutX;
public uint fInX;
public uint fErrorChar;
public uint fNull;
public uint fRtsControl;
public uint fAbortOnError;
}
#endregion
#region Native Methods
// Used to get a handle to the serial port so that we can read/write to it.
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
int flags,
IntPtr template);
// Used to close the handle to the serial port.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);
// Used to get the state of the serial port so that we can configure it.
[DllImport("kernel32.dll")]
static extern bool GetCommState(IntPtr hFile, ref DCB lpDCB);
// Used to configure the serial port.
[DllImport("kernel32.dll")]
static extern bool SetCommState(IntPtr hFile, [In] ref DCB lpDCB);
// Used to set the connection timeouts on our serial connection.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetCommTimeouts(IntPtr hFile, ref COMMTIMEOUTS lpCommTimeouts);
// Used to read bytes from the serial connection.
[DllImport("kernel32.dll")]
static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
int nNumberOfBytesToRead, out int lpNumberOfBytesRead, int lpOverlapped);
// Used to write bytes to the serial connection.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer,
int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, int lpOverlapped);
// Used to flush the I/O buffers.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PurgeComm(IntPtr hFile, int dwFlags);
#endregion
#endregion
}
}
using (var p = new SerialWrapper(@"\\.\COM12", 9600, SerialWrapper.StopBits.One, SerialWrapper.Parity.None))
{
if (!p.Open())
{
Console.WriteLine("Unable to connect.");
return;
}
while (true)
{
Console.Write(p.ReadString(1024));
}
}
关于asp.net - 与 Silverlight 5 的串行通信(COM 端口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989538/
我正在尝试从 F# 编译代码以在 Silverlight 中使用。我编译: --noframework --cliroot "C:\program Files\Microsoft Silverligh
我正在为 Silverlight 安装一个编程环境并试图理顺需要安装的内容,感谢反馈: 在 http://silverlight.net/GetStarted ,第一点允许您安装“ Silverlig
Silverlight RIA 值得学习还是我应该坚持普通的 Silverlight? 背景: 我在 WPF 中做了几个小应用程序 我有 12 年的 VB6/WinForms 模型商业应用经验 我希望
我已经熟悉 Silverlight 编程,但没有任何 GIS 经验。 我作为 Silverlight 开发人员的角色只是显示现有的 GIS 数据。 如果你们有任何经验arcGIS silverligh
我需要在我的 Silverlight 应用程序中创建滚动选取框。选取框需要从右向左滚动。当它完成滚动时,它需要自动重新启动 诀窍是,我需要使用 ItemsControl,因为项目将在滚动时添加到列表中
Silverlight 导航模板在浏览器外运行时是否有效? 最佳答案 当然可以。 并且您可以使用 NavigationServices 函数来创建自定义的“后退”或“前进”按钮 很好的例子:Link
用户通过导航到给定的 URL 在他们的浏览器中启动 Silverlight 应用程序。 然后用户打开另一个浏览器并通过导航到相同的 URL 启动相同的 Silverlight 应用程序。 应用程序的第
Silverlight 4 程序集二进制文件是否与 Silverlight 5 兼容。SL4 程序集是否“在 SL5 运行时中运行”?如果兼容,是否100%兼容。您应该能够在您的 SL5 项目中使用第
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 9年前关闭
我正在寻找是否可以在可以在 application.resources 中设置然后在整个应用程序中使用的 Silverlight 控件中使用应用程序范围的字体。他们不需要指定有关字体的其他内容,例如粗
我正在使用Silverlight4。我想用一个可用字体列表填充组合框。我搜索过很多东西,找不到解决方法。似乎有很多死胡同。五月份曾提出过类似的问题,但没有合适的答案。 当然不是不可能吗? 最佳答案 如
我正在为 Silverlight 很好地实现弱事件模式以避免内存泄漏。 似乎有一些实现,但代码并非微不足道,很难知道哪个是正确的。我找不到微软的任何官方推荐。 如果可能的话,我追求简单的语法。 干杯。
Silverlight 应用程序是在您每次访问该站点时都会下载,还是会检查版本/大小信息并仅下载较新版本的文件? 最佳答案 Silverlight 2 在这方面没有什么特别的,我读过的最简洁的解释来自
我正在尝试在 silverlight 中使用样式触发器,如下所示:
我想尝试制作一个包含多个 Silverlight 画廊中的图片的 asp 网站。我想这样做的原因是我想要这样的东西: Text describing some places. Gallery with
WPF 3.5 有 PresentationTraceSources用于诊断和WPFPerf用于性能和数据绑定(bind)诊断。 Silverlight 是否有等效的工具/库? 最佳答案 尽管我已将
我有一个表示有向图的数据结构,我正在寻找一个好的 Silverlight 可视化,以允许我从一个节点导航到另一个节点,最好有一些漂亮的动画。 有没有人知道这种显示的任何好的 UI 控件或框架?甚至来自
我可以将byte []转换为图像: byte[] myByteArray = ...; // ByteArray to be converted MemoryStream ms = new Memo
我有多个Silverlight项目,希望使用相同的样式,配色方案和一些模板化对象。 我该如何完成? 最佳答案 一种实现方法是创建一个新的silverlight类库,该库将是您共享的主题/样式程序集,其
我正在尝试使用 SLLAUNCHER.EXE 启动已安装的 SL Out-of-Browser App。运行以下命令后,桌面上的 MyApp 启动图标就消失了。如果我在没有覆盖开关的情况下尝试它,则不
我是一名优秀的程序员,十分优秀!