![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
C#只提供了直接操作串口的类,如果直接操作并口的话需要调用API函数:
using System.Runtime.InteropServices;
string sendData = "^xa^fo50,80^adn,36,20^fdTRW EOL2^fs^xz "; //Zebra 打印机命令格式
[DllImport( "kernel32.dll ")]
private static extern int CreateFile(
string lpFileName, // 要打开的串口/并口名称
int dwDesiredAccess, // 指定口的访问方式,一般设置为可读可写方式
int dwShareMode, // 指定口的共享模式,不能共享,所以设置为0
int lpSecurityAttributes, // 设置口的安全属性,WIN9X下不支持,应设为NULL
int dwCreationDisposition , // 对于串口通信,创建方式只能为 OPEN_EXISTING
int dwFlagsAndAttributes, // 指定串口属性与标志
int hTemplateFile); // 对于串口通信必须设置为NULL
[DllImport( "kernel32.dll " )]
public static extern void WriteFile(int ComHandle,string SendData,int StrLen,ref int aLen,int zero);
[DllImport( "kernel32.dll " )]
public static extern void CloseHandle(int handle);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int handle = CreateFile( "lpt1 ", 0x40000000, 0, 0, 3 /**//*OPEN_EXISTING*/, 0, 0);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
if (handle != 0)
{
WriteFile(handle, sendData, strLen, ref aLen, 0);
CloseHandle(handle); //注意,只有关闭并口后打印机才执行命令
}
posted @
2009-02-14 11:50
陈仙荫
阅读(
1043)
评论()
编辑
收藏
举报