求c# 控制串口打印机 如何用C# 控制串口打印机打印中文? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我想打印机应该有打印命令SerialPort sp = new SerialPort("COM1", 9600); sp.Open(); sp.Write("打印命令\r"); 这样子 private string prnPort = "LPT1"; //打印机端口 public ClsPrintLPT(string portName) { prnPort = portName; } private const uint GENERIC_READ = 0x80000000; private const uint GENERIC_WRITE = 0x40000000; private const int OPEN_EXISTING = 3; /// <summary> /// 打开一个vxd(设备) /// </summary> [DllImport("kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto)] private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); /// <summary> /// 开始连接打印机 /// </summary> private bool PrintOpen() { iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (iHandle.ToInt32() == -1) { MessageBox.Show("没有连接打印机或者打印机端口不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } else { fs = new FileStream(iHandle, FileAccess.ReadWrite); sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据 return true; } }这里用打印口(并口)可以打印数据,但是换成串口 (Com1)就打印乱码 http://blog.csdn.net/pfworld/archive/2008/02/05/2084666.aspx 不同的打印机有不同的控制代码,这一点可以参看打印机的手册;串口只是一个通讯接口罢了,VS2003下可以用MSComm32控件,VS2005直接用SerialPort类,kuaiyiyijian的调用CreateFile函数也可以。一般来讲这种直接从串口或并口操作打印机的代码会很繁杂,需要自己定位每一个打印元素。主要还是参考打印机的编程手册吧。 关键是COM口~~会出现乱码-。=~~蛮搞的C++好像不会有这个问题 ASP.net 做有消息就提示出来的功能 wpf浏览器应用程序访问程序文件 C#中两个窗体之间如何调用函数 richtextbox如何简单判断内容为空 各位大侠来帮忙,坐标轴的拖动,在线等待!!! 数据库连接问题 OleDbConnection 怎样再C#中调用C++头文件或类 如何将2个图片合并成一个图片?也就是如何实现合并图层的功能? 项目难点:c#打印和视频共享 acdsee5的这是什么按钮怎么做呀?? 《求教》如何判断 两个日期之间有几个 周末??? 关于c#程序打包的问题。
SerialPort sp = new SerialPort("COM1", 9600);
sp.Open();
sp.Write("打印命令\r");
这样子
{
prnPort = portName;
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
/// <summary>
/// 打开一个vxd(设备)
/// </summary>
[DllImport("kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
/// <summary>
/// 开始连接打印机
/// </summary>
private bool PrintOpen()
{
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (iHandle.ToInt32() == -1)
{
MessageBox.Show("没有连接打印机或者打印机端口不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
return true;
}
}这里用打印口(并口)可以打印数据,但是换成串口 (Com1)就打印乱码
一般来讲这种直接从串口或并口操作打印机的代码会很繁杂,需要自己定位每一个打印元素。主要还是参考打印机的编程手册吧。