C#編程實現(xiàn)向并口設備發(fā)送指令、獲取并口設備的狀態(tài)
更新時間:2015年06月16日 11:31:35 投稿:junjie
這篇文章主要介紹了C#編程實現(xiàn)向并口設備發(fā)送指令、獲取并口設備的狀態(tài),本文直接給出實例代碼,需要的朋友可以參考下
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace ParallelPort { public partial class Form1 : Form { const uint GENERIC_READ = 0x80000000; const uint GENERIC_WRITE = 0x40000000; const uint FILE_ATTRIBUTE_NORMAL = 0x80; #region win32 API [DllImport("kernel32.dll ")] private static extern int CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, uint dwFlagsAndAttributes, int hTemplateFile ); [DllImport("kernel32.dll ")] private static extern bool WriteFile( int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, int lpOverlapped ); [DllImport("kernel32.dll ")] private static extern bool DefineDosDevice( int dwFlags, string lpDeviceName, string lpTargetPath); [DllImport("kernel32.dll ")] private static extern bool CloseHandle( int hObject ); [DllImport("kernel32.dll ")] private static extern bool ReadFile( int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, int lpOverlapped ); #endregion public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int iHandle = -1; try { int i = 0; //創(chuàng)建實例 DefineDosDevice(0x00000001, "LptPortName",@"\Device\Parallel0"); iHandle = CreateFile(@"\\.\LptPortName",GENERIC_READ | GENERIC_WRITE, 0, 0, 3, FILE_ATTRIBUTE_NORMAL, 0); if (iHandle !=-1) { byte[] mybyte = new byte[3]{ 0x12, 0x14, 0x14 };//要發(fā)送的命令(16進制) WriteFile(iHandle, mybyte, mybyte.Length, ref i, 0); byte[] mybyte1 = new byte[3]; string content = String.Empty; int j = 0; ReadFile(iHandle, mybyte1, 3, ref j, 0); if (mybyte1 != null) { foreach(var tempByte in mybyte1) { content += tempByte.ToString(); } } MessageBox.Show(content);//獲取的狀態(tài)值 } else { MessageBox.Show("創(chuàng)建文件失敗!"); } } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { if (iHandle > 0) { CloseHandle(iHandle); } } } } }
相關文章
C#實現(xiàn)一鍵換IP、重置DNS、網(wǎng)關及掩碼的方法
這篇文章主要介紹了C#實現(xiàn)一鍵換IP、重置DNS、網(wǎng)關及掩碼的方法,很實用的功能,需要的朋友可以參考下2014-07-07深入淺析c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性
多態(tài)就是多種形態(tài),也就是對不同對象發(fā)送同一個消息,不同對象會做出不同的響應。這篇文章主要介紹了c#靜態(tài)多態(tài)性與動態(tài)多態(tài)性的相關知識,需要的朋友可以參考下2018-09-09WinForm中comboBox控件數(shù)據(jù)綁定實現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實現(xiàn)方法,結(jié)合實例形式分析了WinForm實現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關操作技巧,需要的朋友可以參考下2017-05-05C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺的操作方法
這篇文章主要介紹了C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺,上傳視頻功能,主要要解決兩個問題,一是在服務端通過C#生成簽名和SDKID,二是在客戶端通過JavaScript上傳視頻到騰訊云點播服務器,感興趣的朋友跟隨小編一起看看吧2023-11-11C#利用DesignSurface如何實現(xiàn)簡單的窗體設計器
這篇文章主要介紹了C#利用DesignSurface如何實現(xiàn)簡單窗體設計器的相關資料,文中通過圖文及示例代碼介紹的很詳細,對大家具有一定的參考價值,需要的朋友們下面來一起學習學習吧。2017-02-02C#中IEnumerator<T>和IEnumerable的區(qū)別
在C#中,IEnumerator<T>和IEnumerable是用于實現(xiàn)迭代的接口,本文主要介紹了C#中IEnumerator<T>和IEnumerable的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-01-01