今天看了一段关于.net如何调用LPT1并口打印ZPL条码的代码,尝试了一下,可是结果怎么也调不好,麻烦各位高手指导一下,不胜感激!
我是新手麻烦能够具体一点哦!!!类的代码如下:
namespace Barcode_Print
{
/// <summary>
/// 本类使用说明:
/// 将一行ZPL指令作为string参数传给write函数即可
/// </summary>
public class LPTControl
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
} [DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
); [DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
); [DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
); private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
} } public bool Write(String Mystring)
{ if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
}
else
{
throw new Exception("端口未打开!");
}
} public bool Close()
{
return CloseHandle(iHandle);
}
}}default页面调用:
using Barcode_Print;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LPTControl.Write("^XA^FO20,20^FDZebraTest^FS^XA^FO20,50^BY3^BCN,70,Y,N,N^FDkingmuch^XZ"); }
}错误信息如下:编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0120: 非静态的字段、方法或属性“Barcode_Print.LPTControl.Write(string)”要求对象引用源错误: 行 15: protected void Page_Load(object sender, EventArgs e)
行 16: {
行 17: LPTControl.Write("^XA^FO20,20^FDZebraTest^FS^XA^FO20,50^BY3^BCN,70,Y,N,N^FDkingmuch^XZ");
行 18:
行 19: }
我是新手麻烦能够具体一点哦!!!类的代码如下:
namespace Barcode_Print
{
/// <summary>
/// 本类使用说明:
/// 将一行ZPL指令作为string参数传给write函数即可
/// </summary>
public class LPTControl
{
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
} [DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
); [DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
); [DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
); private int iHandle;
public bool Open()
{
iHandle = CreateFile("LPT1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
} } public bool Write(String Mystring)
{ if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
}
else
{
throw new Exception("端口未打开!");
}
} public bool Close()
{
return CloseHandle(iHandle);
}
}}default页面调用:
using Barcode_Print;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LPTControl.Write("^XA^FO20,20^FDZebraTest^FS^XA^FO20,50^BY3^BCN,70,Y,N,N^FDkingmuch^XZ"); }
}错误信息如下:编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0120: 非静态的字段、方法或属性“Barcode_Print.LPTControl.Write(string)”要求对象引用源错误: 行 15: protected void Page_Load(object sender, EventArgs e)
行 16: {
行 17: LPTControl.Write("^XA^FO20,20^FDZebraTest^FS^XA^FO20,50^BY3^BCN,70,Y,N,N^FDkingmuch^XZ");
行 18:
行 19: }
解决方案 »
- 我在dataGridView输入数据,输入最后一条数据后,点保存,为什么无效?
- 在c#中对sql数据库的修改不能同步到物理数据库
- Sql查询结果
- 怎样用循环提取listbox中的所有项,急,请高手帮忙
- C#连接Excel:打开一个已存在Excel文件对其进行修改后的保存问题!!!
- 有无错误,大家帮忙看看!!
- 我以前是用pb开发的,现在想试试vc#,谁能够提供一点源代码,高分送上
- 能让dateTimePicker只显示年月吗?
- 求助,远程监控系统
- MM急救,如何获取DataGrid中的某一行的值?
- 问个菜鸟级的问题,如果将含有特殊符号的字符串添加的access中!(在线等)
- c#的组合下拉列表框能否只选择,禁止用户编辑
正确的应该是:
{
PrintToLpt lpt = new PrintToLpt();
lpt.Open();
lpt.Write("^XA^FO20,20^FDZebraTest^FS^XA^FO20,50^BY3^BCN,70,Y,N,N^FDkingmuch^XZ");
lpt.Close();
}
因为你写的这个代码是在服务器端执行的!想条码打印还是用windows程序吧!
我上面有code 39,和code93源码
"我测试时使用的是局域网上的打印机,即把LPT1换成网络打印机(网上共享的打印机)的路径,不管在服务器端还是在客户端都能够正常发送打印指令。"
我也在尝试该做法,但不知道如果,我将LPT1换成//xxx.xxx.xxx.xxx/Generic(//局域网内的电脑IP地址/打印机名称)可否成功?
运行值为 -1
可请帮忙看看