最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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);
        }
      }
    }
  }
}

 

相關文章

最新評論

若羌县| 西峡县| 莱州市| 长治市| 高青县| 齐河县| 望都县| 故城县| 上栗县| 招远市| 平谷区| 台南县| 化德县| 苏尼特右旗| 乌兰浩特市| 金川县| 桐柏县| 札达县| 疏附县| 大埔区| 蒲城县| 闵行区| 修武县| 龙口市| 五常市| 交城县| 福鼎市| 饶河县| 汽车| 乌拉特前旗| 同心县| 武夷山市| 六枝特区| 尼勒克县| 潮安县| 华亭县| 苏尼特右旗| 佛坪县| 瑞昌市| 贵港市| 长葛市|