SoFunction
Updated on 2025-03-08

C# programming implements sending instructions to parallel-port devices and obtaining the status of parallel-port devices

C# programming implements sending instructions to parallel-port devices and obtaining the status of parallel-port devices

Updated: June 16, 2015 11:31:35 Submission: junjie
This article mainly introduces the C# programming implementation of sending instructions to parallel-port devices and obtaining the status of parallel-port devices. This article directly gives the example code. Friends who need it can refer to it.
using System;
using ;
using ;
using ;
using ;

 
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(" ")]
    private static extern int CreateFile(
      string lpFileName,
      uint dwDesiredAccess,
      int dwShareMode,
      int lpSecurityAttributes,
      int dwCreationDisposition,
      uint dwFlagsAndAttributes,
      int hTemplateFile
      );
 
    [DllImport(" ")]
    private static extern bool WriteFile(
      int hFile,
      byte[] lpBuffer,
      int nNumberOfBytesToWrite,
      ref int lpNumberOfBytesWritten,
      int lpOverlapped
      );
 
    [DllImport(" ")]
    private static extern bool DefineDosDevice(
    int dwFlags,
    string lpDeviceName,
    string lpTargetPath);
 
    [DllImport(" ")]
    private static extern bool CloseHandle(
      int hObject
      );
    [DllImport(" ")]
    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;
        //Create an instance        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 };//The command to be sent (hexadecimal)          WriteFile(iHandle, mybyte, , ref i, 0);
          byte[] mybyte1 = new byte[3];
          string content = ;
          int j = 0;
          ReadFile(iHandle, mybyte1, 3, ref j, 0);
          if (mybyte1 != null)
          {
            foreach(var tempByte in mybyte1)
            {
              content += ();
            }
          }                
          (content);//The obtained status value        }
        else
        {
          ("File creation failed!");
        }
      }
      catch(Exception ex)
      {
        ();
      }
      finally
      {
        if (iHandle > 0)
        {
          CloseHandle(iHandle);
        }
      }
    }
  }
}

 
  • C#
  • Parallel device
  • Send command

Related Articles

  • Comparison of the efficiency of 6 methods of judging characters empty

    This article mainly introduces the method of judging whether characters are empty in C#, and compares the execution efficiency of various methods in actual measurement. Finally, it is recommended that you use IsNullOrEmpty, which has a relatively balanced efficiency and ease of use.
    2016-05-05
  • C# development int and string conversion operations

    This article mainly introduces the int and string conversion operations of C# development, which are of good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2020-12-12
  • How to convert strings and byte arrays in C#

    This article introduces the conversion method of strings and byte arrays in C#, and the article introduces it in detail through sample code. It has certain reference value for everyone's study or work. Friends who need it can refer to it.
    2022-05-05
  • Unity accesses Gaode Open API to achieve IP positioning

    This article mainly introduces how Unity can connect to the Gaode Open API to realize IP positioning function. The sample code in the article is explained in detail, which is of certain reference value for our study or work. If you need it, please refer to it.
    2022-04-04
  • C# wpf defines ViewModelBase for simplified property binding

    The binding mechanism is the core of wpf and the fundamental basis of interface independence. Especially the use of mvvm mode, this article mainly introduces how wpf defines ViewModelBase for simplified property binding. If you need it, you can refer to it.
    2024-04-04
  • C# implementation puzzle game

    This article mainly introduces the C# implementation puzzle game in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2022-08-08
  • C# () and system color comparison table at a glance

    This article mainly introduces C# () and a list of system color comparison tables, which are of good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2021-01-01
  • Example of C# Message-based Publishing Subscription Model (Part 2)

    This article mainly introduces examples of C# based message publishing and subscription model, which helps everyone better understand and use C#. Interested friends can learn about it.
    2021-03-03
  • Example of c# closure usage method

    This article mainly introduces how to use C#'s closure function. The example is very simple. Please refer to it.
    2013-11-11
  • C# uses Grid++ reports in Winform development

    This article mainly introduces the use of Grid++ reports in Winform development. The article introduces the example code in detail, which has certain reference learning value for everyone's study or work. If you need it, please learn with the editor below.
    2019-03-03

Latest Comments