using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;
public delegate bool mPCH341_NOTIFY_ROUTINE(uint iEventStatus);
namespace text { public partial class MainForm : Form { const uint CH341_DEVICE_ARRIVAL = 3; // 设备插入事件,已经插入 const uint CH341_DEVICE_REMOVE_PEND = 1; // 设备将要拔出 const uint CH341_DEVICE_REMOVE = 0; // 设备拔出事件,已经拔出
public static bool NotifyRoutine(uint iEventStatus) { if (iEventStatus == CH341_DEVICE_ARRIVAL) { //m_TextBox2.Text = "检测到有CH341设备已插入"; he.DemoEvent(m_TextBox2, "检测到有CH341设备已插入"); //MessageBox.Show("检测到有CH341设备已插入"); } else if (iEventStatus == CH341_DEVICE_REMOVE) { m_TextBox2.Text = "检测到有CH341设备已拔出"; he.DemoEvent(m_TextBox2, "检测到有CH41设备已拔出"); MessageBox.Show("检测到有CH341设备已拔出!"); } return true; } static void memset(byte[] buffer, byte c, uint Length) { for (uint i = 0; i < Length; i++) buffer[i] = c;
} public MainForm() { InitializeComponent(); Test test = new Test(); m_TextBox2 = m_TextBox; he = new HasEvent(); he.SampleEvent +=new EventHandler(SampleEventHandler); mPCH341_NOTIFY_ROUTINE myCallBack = new mPCH341_NOTIFY_ROUTINE(MainForm.NotifyRoutine);
byte[] iDeviceID = null; test.SetDeviceNotify(0, iDeviceID, myCallBack); } private static void SampleEventHandler(object sender, MyEventArgs mea) { System.Windows.Forms.TextBox tb = (TextBox)sender; tb.Text = mea.Message; } class Test { System.IntPtr handle; [DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] static extern unsafe IntPtr CH341OpenDevice // 打开CH341设备,返回句柄,出错则无效 ( uint iIndex // 指定CH341设备序号,0对应第一个设备,-1则自动搜索一个可以被打开的设备并返回序号 );
[DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] static extern unsafe void CH341CloseDevice // 关闭CH341设备 ( uint iIndex // 指定CH341设备序号 );
[DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] static extern unsafe void CH341GetVersion();
[DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] static extern unsafe void CH341DriverCommand ( uint iIndex, mPCH341_NOTIFY_ROUTINE ioCommand );
[DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] static extern unsafe uint CH341SetDeviceNotify // 打开CH341设备,返回句柄,出错则无效 ( uint iIndex, // 指定CH341设备序号 byte[] iDeviceID, mPCH341_NOTIFY_ROUTINE iNotifyRoutine );
public bool Open(uint iIndex) { // open the existing file for reading handle = CH341OpenDevice(iIndex); System.IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); if (handle != INVALID_HANDLE_VALUE) { return true; } else { return false; } } public unsafe bool SetDeviceNotify(uint iIndex, byte[] iDeviceID, mPCH341_NOTIFY_ROUTINE iNotifyRoutine) { if (CH341SetDeviceNotify(iIndex, iDeviceID, iNotifyRoutine) == 0) { return false; } return true; } public void Close(uint iIndex) { CH341CloseDevice(iIndex); } }
public class MyEventArgs : EventArgs { private string msg;
public MyEventArgs(string messageData) { msg = messageData; } public string Message { get { return msg; } set { msg = value; } } } public class HasEvent { // Declare an event of delegate type EventHandler of // MyEventArgs.
public event EventHandler SampleEvent;
public void DemoEvent(object obj, string val) { // Copy to a temporary variable to be thread-safe. EventHandler temp = SampleEvent; if (temp != null) temp(obj, new MyEventArgs(val)); } } } } 不知道为什么不能识别341的设备,希望各位大侠指点迷津!UploadImages/201111410321011.rar