You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
9.5 KiB
C#

9 months ago
using System;
using System.Runtime.InteropServices;
namespace POSV.Printer
{
public class ByPosDll
{
const string _DllVer = "1.4";
/// <summary>
/// 获取动态库版本号
/// </summary>
public string GetDllVer
{
get { return _DllVer; }
}
/// <summary>
/// 设备打开后的句柄
/// </summary>
public IntPtr POS_IntPtr;
/// <summary>
/// 函数返回值
/// </summary>
public uint POS_SUCCESS = 1001;// 函数执行成功
public uint POS_FAIL = 1002; // 函数执行失败
/*串口通讯时的数据停止位数*/
public const int POS_COM_ONESTOPBIT = 0x00;// 停止位为1
public const int POS_COM_ONE5STOPBITS = 0x01;// 停止位为15
public const int POS_COM_TWOSTOPBITS = 0x02;// 停止位为2
/*串口的奇偶校验方法*/
public const int POS_COM_NOPARITY = 0x00;// 无校验
public const int POS_COM_ODDPARITY = 0x01;// 奇校验
public const int POS_COM_EVENPARITY = 0x02;// 偶校验
public const int POS_COM_MARKPARITY = 0x03;// 标记校验
public const int POS_COM_SPACEPARITY = 0x04;// 空格校验
/*指定串口的流控制(握手)方式、或表示通讯方式*/
public const int POS_COM_DTR_DSR = 0x00;// 流控制为DTR/DST
public const int POS_COM_RTS_CTS = 0x01;// 流控制为RTS/CTS
public const int POS_COM_XON_XOFF = 0x02;// 流控制为XON/OFF
public const int POS_COM_NO_HANDSHAKE = 0x03;// 无握手
public const int POS_OPEN_PARALLEL_PORT = 0x12;// 打开并口通讯端口
public const int POS_OPEN_BYUSB_PORT = 0x13;//打开USB通讯端
public const int POS_OPEN_PRINTNAME = 0X14;// 打开打印机驱动程序
public const int POS_OPEN_NETPORT = 0X15;// 打开以太网打印机
public const int POS_CUT_MODE_FULL = 0x00;//全切
public const int POS_CUT_MODE_PARTIAL = 0x01;//半切
[DllImport("POSDLL.dll" , CharSet = CharSet.Ansi)]
public static extern IntPtr POS_Open([MarshalAs(UnmanagedType.LPStr)]string lpName ,
uint nComBaudrate ,
uint nComDataBits ,
uint nComStopBits ,
uint nComParity ,
uint nParam);
/// <summary>
/// 关闭已经打开的并口或串口USB端口网络接口或打印机。
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_Close();
/// <summary>
/// 复位打印机,把打印缓冲区中的数据清除,字符和行高的设置被清除,打印模式被恢复到上电时的缺省模式。
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_Reset();
/// <summary>
/// 切纸,如果指定为全切,则参数 nDistance 忽略,如果指定为半切,则打印机走纸 nDistance 点,然后切纸
/// </summary>
/// <param name="nMode">模式编号 半切或是全切</param>
/// <param name="nDistance">走位的距离</param>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_CutPaper(uint nMode , uint nDistance);
/// <summary>
/// 往钱箱引脚发送脉冲以打开钱箱。
/// </summary>
/// <param name="nID">指定钱箱的引脚。0x00 钱箱连接器引脚2 0x01 钱箱连接器引脚5 </param>
/// <param name="nOnTimes">指定往钱箱发送的高电平脉冲保持时间,即 nOnTimes × 2 毫秒。可以为1 到 255。</param>
/// <param name="nOffTimes">指定往钱箱发送的低电平脉冲保持时间,即 nOffTimes × 2 毫秒。可以为1 到 255。</param>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_KickOutDrawer(uint nID , uint nOnTimes , uint nOffTimes);
/// <summary>
/// 发送数据到端口或文件。通用端口打印可以使用此函数 一般不能设置字体大小样式等
/// </summary>
/// <param name="hPort">端口或文件句柄。可以通过POS_Open来获取</param>
/// <param name="pszData">指向将要发送的数据缓冲区。</param>
/// <param name="nBytesToWrite">指定将要发送的数据的字节数。</param>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_WriteFile(IntPtr hPort , byte[] pszData , uint nBytesToWrite);
/// <summary>
/// 新建一个打印作业。
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern bool POS_StartDoc();
/// <summary>
/// 结束一个打印作业。
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern bool POS_EndDoc();
/// <summary>
/// 向前走纸。
/// 1如果在标准打印模式行模式下打印文本则打印缓冲区中的数据且打印位置自动移动到下一行的行首。
/// 2如果在标准打印模式行模式下打印位图则在指定的位置打印位图且打印位置自动移动到下一行的行首。
/// 3如果在页模式或标签模式下则把需要打印的数据设置在指定的位置同时把打印位置移动到下一个行首
/// 但是并不立即进纸并打印,而是一直到调用 POS_PL_Print 函数时才打印。
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_FeedLine();
/// <summary>
/// 打印头换n行
/// </summary>
/// <returns></returns>
[DllImport("POSDLL.dll" , SetLastError = true)]
public static extern IntPtr POS_FeedLines(uint nLines);
/// <summary>
/// 设置右边距
/// </summary>
/// <param name="nDistance">右边距</param>
/// <returns></returns>
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_SetRightSpacing(uint nDistance);
/// <summary>
/// 预下载一幅位图到打印机的 RAM 中,同时指定此位图的 ID 号。
/// </summary>
/// <param name="pszPath">指向以 null 结尾的表示位图路径及其文件名的字符串。</param>
/// <param name="nID">指定将要下载的位图的 ID 号。可以为 0 到 7。</param>
/// <returns></returns>
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_PreDownloadBmpToRAM([MarshalAs(UnmanagedType.LPStr)]string pszPath , uint nID);
/// <summary>
/// 预下载一幅或若干幅位图到打印机的 Flash 中。
/// </summary>
/// <param name="pszPath">[in] 指向包含若干位图的文件路径及其名称的字符串数组。</param>
/// <param name="nID">[in] 指定将要下载的位图幅数。可以为 1 到 255。</param>
/// <returns>如果函数成功,则返回值为 POS_SUCCESS</returns>
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_PreDownloadBmpsToFlash(IntPtr[] pszPath , uint nCount);
/// <summary>
/// 下载并打印位图
/// </summary>
/// <param name="pszPath">指向以null 结尾的包含位图文件路径及其名称的字符串。</param>
/// <param name="nOrgx">指定将要打印的位图和左边界的距离点数。可以为 0到 65535 点。</param>
/// <param name="nMode">指定位图的打印模式。</param>
/// <returns></returns>
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_S_DownloadAndPrintBmp([MarshalAs(UnmanagedType.LPStr)]string pszPath , uint nOrgx , uint nMode);
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_S_PrintBmpInRAM(uint nID , uint nOrgx , uint nMode);
[DllImport("posdll.dll" , SetLastError = true)]
public static extern IntPtr POS_S_PrintBmpInFlash(uint nID , uint nOrgx , uint nMode);
/// <summary>
/// 打开打印设备的网口
/// </summary>
/// <param name="IPAddress">设备的IP地址</param>
/// <returns>是否打开成功</returns>
public bool OpenNetPort(string IPAddress)
{
POS_IntPtr = POS_Open(IPAddress , 0 , 0 , 0 , 0 , POS_OPEN_NETPORT);
if ((int)POS_IntPtr != -1)
return true;
else
return false;
}
public bool CutPaper()
{
IntPtr _intPtr = POS_CutPaper(1,5);
return ((uint)_intPtr == POS_SUCCESS);
}
/// <summary>
/// 关闭设备端口
/// </summary>
/// <returns>是否关闭成功</returns>
public bool Close()
{
IntPtr _intPtr = POS_Close();
return ((uint)_intPtr == POS_SUCCESS);
}
}
}