设置首页收藏本站
开启左侧

C#如何打印输出

[复制链接]
hxg 发表于 2016-11-1 20:05:10 | 显示全部楼层 |阅读模式
C#如何能将字符串数据通过并口发送出去??
比如VB可以以下使用:
Open "LPT1" For Output #1
Print #1,Text3.Text
Close #1

C#中有什么简便的方式吗?
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
Criss 发表于 2016-11-1 20:23:46 | 显示全部楼层

回帖奖励 +5 视觉币

查看本论坛写的C#串口程序,看看怎么发送数据的
链接:http://pan.baidu.com/s/1bSAjHO 密码:135d
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
泉林影 发表于 2016-11-2 10:56:40 | 显示全部楼层

回帖奖励 +5 视觉币

C#中有串口控件,但是没有并口控件,通常使用并口通讯时通过接口调用API函数进行通讯
[DllImport( "inpout32.dll ",   EntryPoint   =   "Out32 ")]
    public   static   extern   void   Output(int   adress,   int   value);

  [DllImport( "inpout32.dll ",   EntryPoint   =   "Inp32 ")]
    public   static   extern   int   Input(int   adress);   
或者调用kernel32.dll进行读写。
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| hxg 发表于 2016-11-2 21:14:16 | 显示全部楼层
已经作出来了测试程序,测试成功。将Textbox内的字符串数据通过并口发送给条码打印机。代码如下
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11. using Microsoft.Win32.SafeHandles;

  12. namespace LPTPrint
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }

  20.         [DllImport("kernel32.dll")]
  21.         private static extern int CreateFile(
  22.         string lpFileName,
  23.         uint dwDesiredAccess,
  24.         int dwShareMode,
  25.         int lpSecurityAttributes,
  26.         int dwCreationDisposition,
  27.         int dwFlagsAndAttributes,
  28.         int hTemplateFile
  29.         );
  30.         [DllImport("kernel32.dll")]
  31.         private static extern bool CloseHandle(
  32.         int hObject
  33.         );

  34.         private const uint GENERIC_WRITE = 0x40000000;
  35.         private const int OPEN_EXISTING = 3;

  36.         private void button1_Click(object sender, EventArgs e)
  37.         {
  38.             int hPort = CreateFile("LPT1", GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  39.             System.IntPtr hPortP = new IntPtr(hPort);//定义一个IntPtr对象并初始化,用于对句柄操作。        
  40.             SafeFileHandle sfHandle = new SafeFileHandle(hPortP, true);//因为用hPortP给出警告。      
  41.             FileStream fs;
  42.             fs = new FileStream(sfHandle, FileAccess.Write, 1);//利用安全句柄和写模式实例一个文件流。        
  43.             StreamWriter writer = new StreamWriter(fs);//定义一个写入流进行向LPT1写入数据。        
  44.             writer.AutoFlush = false;//不让自动清空        
  45.             writer.WriteLine(textBox1.Text);//把数据(字符串)写入到LPT1。      
  46.             writer.Flush();
  47.             writer.Close();

  48.         }
复制代码

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
唐豆豆 发表于 2016-11-7 10:05:56 | 显示全部楼层

回帖奖励 +5 视觉币

  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Windows.Forms;

  6.   
  7. namespace ParallelPort
  8. {
  9.   public partial class Form1 : Form
  10.   {
  11.     const uint GENERIC_READ = 0x80000000;
  12.     const uint GENERIC_WRITE = 0x40000000;
  13.     const uint FILE_ATTRIBUTE_NORMAL = 0x80;
  14.   
  15.     #region win32 API
  16.     [DllImport("kernel32.dll ")]
  17.     private static extern int CreateFile(
  18.       string lpFileName,
  19.       uint dwDesiredAccess,
  20.       int dwShareMode,
  21.       int lpSecurityAttributes,
  22.       int dwCreationDisposition,
  23.       uint dwFlagsAndAttributes,
  24.       int hTemplateFile
  25.       );
  26.   
  27.     [DllImport("kernel32.dll ")]
  28.     private static extern bool WriteFile(
  29.       int hFile,
  30.       byte[] lpBuffer,
  31.       int nNumberOfBytesToWrite,
  32.       ref int lpNumberOfBytesWritten,
  33.       int lpOverlapped
  34.       );
  35.   
  36.     [DllImport("kernel32.dll ")]
  37.     private static extern bool DefineDosDevice(
  38.     int dwFlags,
  39.     string lpDeviceName,
  40.     string lpTargetPath);
  41.   
  42.     [DllImport("kernel32.dll ")]
  43.     private static extern bool CloseHandle(
  44.       int hObject
  45.       );
  46.     [DllImport("kernel32.dll ")]
  47.     private static extern bool ReadFile(
  48.       int hFile,
  49.       byte[] lpBuffer,
  50.       int nNumberOfBytesToRead,
  51.       ref int lpNumberOfBytesRead,
  52.       int lpOverlapped
  53.       );
  54.     #endregion

  55.   
  56.     public Form1()
  57.     {
  58.       InitializeComponent();
  59.     }
  60.   

  61.     private void button1_Click(object sender, EventArgs e)
  62.     {
  63.       int iHandle = -1;
  64.       try
  65.       {
  66.         int i = 0;
  67.         //创建实例
  68.         DefineDosDevice(0x00000001, "LptPortName",@"\Device\Parallel0");
  69.         iHandle = CreateFile(@"\\.\LptPortName",GENERIC_READ | GENERIC_WRITE, 0, 0, 3, FILE_ATTRIBUTE_NORMAL, 0);
  70.         if (iHandle !=-1)
  71.         {
  72.           byte[] mybyte = new byte[3]{ 0x12, 0x14, 0x14 };//要发送的命令(16进制)
  73.           WriteFile(iHandle, mybyte, mybyte.Length, ref i, 0);
  74.           byte[] mybyte1 = new byte[3];
  75.           string content = String.Empty;
  76.           int j = 0;
  77.           ReadFile(iHandle, mybyte1, 3, ref j, 0);
  78.           if (mybyte1 != null)
  79.           {
  80.             foreach(var tempByte in mybyte1)
  81.             {
  82.               content += tempByte.ToString();
  83.             }
  84.           }               
  85.           MessageBox.Show(content);//获取的状态值
  86.         }
  87.         else
  88.         {
  89.           MessageBox.Show("创建文件失败!");
  90.         }
  91.       }
  92.       catch(Exception ex)
  93.       {
  94.         MessageBox.Show(ex.Message);
  95.       }
  96.       finally
  97.       {
  98.         if (iHandle > 0)
  99.         {
  100.           CloseHandle(iHandle);
  101.         }
  102.       }
  103.     }
  104.   }
  105. }
复制代码

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
wideplus 发表于 2017-4-21 08:40:17 | 显示全部楼层

回帖奖励 +5 视觉币

原来c#打印机可以这么玩!!!:P
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
jun198923 发表于 2018-9-5 09:41:25 | 显示全部楼层
厉害,还能这么玩,长见识了
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
xiaohe96 发表于 2018-12-20 11:04:25 | 显示全部楼层
C#中有串口控件,但是没有并口控件,通常使用并口通讯时通过接口调用API函数进行通讯
[DllImport( "inpout32.dll ",   EntryPoint   =   "Out32 ")]
    public   static   extern   void   Output(int   adress,   int   value);

  [DllImport( "inpout32.dll ",   EntryPoint   =   "Inp32 ")]
    public   static   extern   int   Input(int   adress);   
或者调用kernel32.dll进行读写。
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表