如何再c#中跟条码打印机交互?

Nlf 2006-07-15 09:13:52
这个是技术服务提供的代码,但是我们需要c#的,之前也没有做跟硬件接口类的所以不太熟悉,请教大家应该怎么写?根据我的理解,可能需要传一个string入打印机,应该怎么作?

Hi all,

When printing the label by your applications, please replace those red colour data by your parameters. The size and the location of the TEXTs may need to modify to create a better view.

ofstream fout; //declare file-stream handle
fout.open("lpt1:", ios::out); //open out to lpt1:
fout<<"^XA"; // start ZPL command
fout<<"^FO11,19^A0N,17,16^FDYXX^FS"; // define the locate of the printout and the size of the font is 17 x 16
fout<<"^FO45,19^A0N,17,16^FDXXXXX^FS";
fout<<"^FO95,19^A0N,17,16^FDAA^FS";
fout<<"^FO121,19^A0N,17,16^FDB^FS";
fout<<"^FO11,40^A0N,13,12^FDMM/DD/YY^FS"; // define the locate of the printout and the size of the font is 13 x 12
fout<<"^FO81,40^A0N,13,12^FDC^FS";
fout<<"^FO103,40^A0N,13,12^FDD^FS";
fout<<"^FO11,59^A0N,13,12^FDPF^FS";
fout<<"^FO45,60^A0N,13,12^FDKF^FS";
fout<<"^FO73,60^A0N,13,12^FDDD^FS";
fout<<"^FO95,61^A0N,13,12^FDI^FS";
fout<<"^BY1,3,15^FO11,80^BCN,,Y,N^FD0705610065^FS"; // print barcode 128 with human readable
fout<<"^PQ1"; //print one label
fout<<"^XZ"; //sending end of zpl Print one
fout.close(); //close file

Please contact me if any unclear.
...全文
1494 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
easyboot 2007-04-26
  • 打赏
  • 举报
回复
mark
angleoldhen 2007-02-24
  • 打赏
  • 举报
回复
mask
szh3210 2006-10-23
  • 打赏
  • 举报
回复
mark
kssys 2006-07-19
  • 打赏
  • 举报
回复
噢,这个方案看来是备用方案。
始终觉得调用DLL是最好的方法。
Nlf 2006-07-19
  • 打赏
  • 举报
回复
多谢kssys兄弟,为什么吧ref改成out?
今天斑马机的技术支持过来了,根据我们要打印的标签要求设置好了ZPL
现在剩下的任务就是把lpt1口打开
利用你上面给我的调用api的方法是其中一条路,另外一个可以设置一个文本打印机然后将zpl命令作为一个文本文件打印输出,第二种方法我还不是很理解,能解释一下么?怎么样实现呢?

kssys兄最好有msn或者qq或者email,我好实时请教您问题,再次感谢
Nlf 2006-07-19
  • 打赏
  • 举报
回复
.......对啊,很奇怪么?呵呵
那边技术支持又来一个新方案,这个怎么实现?

> 你好,
> 如果直接打开端口的方法不行的话,可以尝试用文本打印驱动,在打印机中添加 "Generic / Text
> Only"然后直接把文本打印到 Generic / Text Only 打印机中。
> 添加打印机,选择 制造商为"Generic" 打印机为 "Generic / Text Only "的打印机,该打印机为WINDOWS 自带的。


CCjian 2006-07-19
  • 打赏
  • 举报
回复
mark!
kssys 2006-07-19
  • 打赏
  • 举报
回复
1.两个参数:
1)lpNumberOfBytesWritten这个参数返回实际的写入字节数.
2)lpOverlapped这个参数指定了当前操作的选项:如是否写完再运行程序...

2.另外这边需要改成:
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
out int lpNumberOfBytesWritten,
out OVERLAPPED lpOverlapped
);

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("端口未打开!");
}
}
Nlf 2006-07-19
  • 打赏
  • 举报
回复
应该如何指定这两个参数的直?
Nlf 2006-07-19
  • 打赏
  • 举报
回复
恩,如果可以的话调用API自然是最好
我把你给我的那个LPTControl类copy进去,优点小问题你给解决一下:
public bool Write(String Mystring)
{
private int i;
OVERLAPPED x;

if(iHandle !=-1)
{

byte[] mybyte=System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle,mybyte,mybyte.Length,ref i,ref x);
}
else
{
throw new Exception("端口未打开!");
}
}

这个里面的ref i 和ref x编译通过不了,说为赋值.
我不太明白wirte函数申明中这两个参数的意思,能说明下么?
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
Nlf 2006-07-18
  • 打赏
  • 举报
回复
多谢了,正研究中
兄弟有没有联系方式可以交流以下 我的li.lei1@byd.com.cn
kssys 2006-07-18
  • 打赏
  • 举报
回复
哈哈,什么观念啊-_-

如果他们提供ZPL的话,那么直接用我上面提供的LPT类来发送ZPL就可以了。

LPTControl.Write("^XA^LH15,5^PRC^LL180");
kssys 2006-07-18
  • 打赏
  • 举报
回复
可以啊。你這是郵件?
jxpxwym 2006-07-17
  • 打赏
  • 举报
回复

private void Form1_Load(object sender, System.EventArgs e)
{
LabelApp=new LabelApplicationClass();
doc=(LabelView.LabelDocument)LabelApp.ActiveDocument();
doc.Open("c:\\worklog\\label.lbl",true);
}

private void PrintLabel(string result)
{
try
{

LabelView.LabelField snLabel=(LabelView.LabelField)((LabelView.LabelFields)doc.LabelFields).Item("SN");
snLabel.Value=result;
doc.LabelSetup();
doc.PrintLabel(2,null,null,null,null,null,null);


}
catch(Exception err)
{
MessageBox.Show(err.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}




private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
LabelApp.Quit();
LabelApp = null;
System.GC.Collect();

}
jxpxwym 2006-07-17
  • 打赏
  • 举报
回复
呵呵,其实你可以借助第三方软件codesoft或labelview的dll文件实现打印.

我前段时间就是这样做的.这样的好处就是不管是什么类型的条码打印机,只要直接做好标签样板文件就可有通过自己编写的程序打印.
Nlf 2006-07-17
  • 打赏
  • 举报
回复
你这样做岂不杀白白浪费了技术支持的资源?我们花那么多米买他们设备就应该来点专用服务啊,你这个代码我看看先,谢谢
Nlf 2006-07-16
  • 打赏
  • 举报
回复
岂不杀完全不需要斑马机厂的支持了?
现在我们就是需要运用ZPL,当然前提是在他们提供帮助的情况下.我们告诉他们我想要达到的效果然后他们提供ZPL命令,我们要做的就是把这个命令传给打印机.怎么做?
Firestone2003 2006-07-15
  • 打赏
  • 举报
回复
定义和这个一样的stream,按照这样的格式写进去阿
kssys 2006-07-15
  • 打赏
  • 举报
回复
是的,上面的就是使用DLL列印条码。就不必记那些烦人的ZPL
zjh222 2006-07-15
  • 打赏
  • 举报
回复
还是要应用到Win API,如果是纯的C#是实现不了的,功能并不强大!!
加载更多回复(8)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧