C#編程實(shí)現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(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)建實(shí)例 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進(jìn)制) 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); } } } } }
相關(guān)文章
C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法
這篇文章主要介紹了C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07深入淺析c#靜態(tài)多態(tài)性與動(dòng)態(tài)多態(tài)性
多態(tài)就是多種形態(tài),也就是對(duì)不同對(duì)象發(fā)送同一個(gè)消息,不同對(duì)象會(huì)做出不同的響應(yīng)。這篇文章主要介紹了c#靜態(tài)多態(tài)性與動(dòng)態(tài)多態(tài)性的相關(guān)知識(shí),需要的朋友可以參考下2018-09-09WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05C#結(jié)合JavaScript實(shí)現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺(tái)的操作方法
這篇文章主要介紹了C#結(jié)合JavaScript實(shí)現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺(tái),上傳視頻功能,主要要解決兩個(gè)問(wèn)題,一是在服務(wù)端通過(guò)C#生成簽名和SDKID,二是在客戶(hù)端通過(guò)JavaScript上傳視頻到騰訊云點(diǎn)播服務(wù)器,感興趣的朋友跟隨小編一起看看吧2023-11-11C#利用DesignSurface如何實(shí)現(xiàn)簡(jiǎn)單的窗體設(shè)計(jì)器
這篇文章主要介紹了C#利用DesignSurface如何實(shí)現(xiàn)簡(jiǎn)單窗體設(shè)計(jì)器的相關(guān)資料,文中通過(guò)圖文及示例代碼介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02C#中IEnumerator<T>和IEnumerable的區(qū)別
在C#中,IEnumerator<T>和IEnumerable是用于實(shí)現(xiàn)迭代的接口,本文主要介紹了C#中IEnumerator<T>和IEnumerable的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C#之Windows自帶打印功能的實(shí)現(xiàn)
這篇文章主要介紹了C#之Windows自帶打印功能的實(shí)現(xiàn)方式,具有很好的價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06