C#版本CH341上位機

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; // 設(shè)備插入事件,已經(jīng)插入 const uint CH341_DEVICE_REMOVE_PEND = 1; // 設(shè)備將要拔出 const uint CH341_DEVICE_REMOVE = 0; // 設(shè)備拔出事件,已經(jīng)拔出

public static bool NotifyRoutine(uint iEventStatus) { if (iEventStatus == CH341_DEVICE_ARRIVAL) { //m_TextBox2.Text = "檢測到有CH341設(shè)備已插入"; he.DemoEvent(m_TextBox2, "檢測到有CH341設(shè)備已插入"); //MessageBox.Show("檢測到有CH341設(shè)備已插入"); } else if (iEventStatus == CH341_DEVICE_REMOVE) { m_TextBox2.Text = "檢測到有CH341設(shè)備已拔出"; he.DemoEvent(m_TextBox2, "檢測到有CH41設(shè)備已拔出"); MessageBox.Show("檢測到有CH341設(shè)備已拔出!"); } 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設(shè)備,返回句柄,出錯則無效 ( uint iIndex // 指定CH341設(shè)備序號,0對應(yīng)第一個設(shè)備,-1則自動搜索一個可以被打開的設(shè)備并返回序號 );

[DllImport("CH341DLL", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] static extern unsafe void CH341CloseDevice // 關(guān)閉CH341設(shè)備 ( uint iIndex // 指定CH341設(shè)備序號 );

[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設(shè)備,返回句柄,出錯則無效 ( uint iIndex, // 指定CH341設(shè)備序號 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的設(shè)備,希望各位大俠指點迷津!UploadImages/201111410321011.rar

把整個工程打包后上傳到論壇,我?guī)湍憧纯?/p>


UploadImages/201111417342486.rar 測試過,是可以檢測插拔的。


回第2樓,我就是按照這個程序來寫的,但是無法檢測CH341芯片的設(shè)備,調(diào)用CH341OpenDevice、CH341CloseDevice、CH341SetDeviceNotify這三個函數(shù),不能檢測,你幫我看看我的程序到底哪里出了問題!


你根本就沒調(diào)用 public static void SetDeviceNotify(int index, deleSetNotify iNotifyRoutine)這個函數(shù)!


只有登錄才能回復(fù),可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频