|
|
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
|
using POSV.Printer;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace POSV.Proxy.Peripherals
|
|
|
|
|
{
|
|
|
|
|
public class CashBox : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public CashBox()
|
|
|
|
|
{
|
|
|
|
|
this.LOGGER = NLog.LogManager.GetLogger(GetType().FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 钱箱属性
|
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
|
|
|
|
|
private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
|
|
|
|
|
|
|
|
|
|
private const short FILE_ATTRIBUTE_NORMAL = 0x80;
|
|
|
|
|
private const short INVALID_HANDLE_VALUE = -1;
|
|
|
|
|
private const uint GENERIC_READ = 0x80000000;
|
|
|
|
|
private const uint GENERIC_WRITE = 0x40000000;
|
|
|
|
|
private const uint CREATE_NEW = 1;
|
|
|
|
|
private const uint CREATE_ALWAYS = 2;
|
|
|
|
|
private const uint OPEN_EXISTING = 3;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 端口
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static string _port = "NONE";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 开钱箱指令[二进制]
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static byte[] _instruction = null;
|
|
|
|
|
|
|
|
|
|
IntPtr _IntPtr = IntPtr.Zero;
|
|
|
|
|
|
|
|
|
|
private readonly NLog.Logger LOGGER = null;
|
|
|
|
|
|
|
|
|
|
static CashBox cashBox = new CashBox();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 钱箱操作
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 开启端口
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="port"></param>
|
|
|
|
|
/// <param name="printer"></param>
|
|
|
|
|
public bool Open(string drawerCommand, string port)
|
|
|
|
|
{
|
|
|
|
|
_port = port;
|
|
|
|
|
_instruction = GetBytes(drawerCommand);
|
|
|
|
|
|
|
|
|
|
if (_port == "NONE" || _instruction == null)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 开钱箱.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void OpenCashBox()
|
|
|
|
|
{
|
|
|
|
|
if (_instruction == null) return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_port.ToLower().Contains("com"))
|
|
|
|
|
{
|
|
|
|
|
_IntPtr = CreateFile(_port, (int)GENERIC_WRITE, 0, (int)IntPtr.Zero, (int)OPEN_EXISTING, 0, (int)IntPtr.Zero);
|
|
|
|
|
if ((int)_IntPtr == -1)
|
|
|
|
|
{
|
|
|
|
|
LOGGER.Error("串口打开失败!");
|
|
|
|
|
}
|
|
|
|
|
FileStream lpt = new FileStream(_IntPtr, FileAccess.ReadWrite);
|
|
|
|
|
lpt.Write(_instruction, 0, _instruction.Length);
|
|
|
|
|
lpt.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_port.ToLower().Contains("lpt"))
|
|
|
|
|
{
|
|
|
|
|
_IntPtr = CreateFile(_port, (int)GENERIC_WRITE, 0, (int)IntPtr.Zero, (int)OPEN_EXISTING, 0, (int)IntPtr.Zero);
|
|
|
|
|
if ((int)_IntPtr == -1)
|
|
|
|
|
{
|
|
|
|
|
LOGGER.Error("并口打开失败!");
|
|
|
|
|
}
|
|
|
|
|
FileStream lpt = new FileStream(_IntPtr, FileAccess.ReadWrite);
|
|
|
|
|
lpt.Write(_instruction, 0, _instruction.Length);
|
|
|
|
|
lpt.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_port.ToLower().Contains("usb"))
|
|
|
|
|
{
|
|
|
|
|
YkPosDll.YkOpenDevice(13, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOGGER.Info(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清除资源
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_port = "NONE";
|
|
|
|
|
_instruction = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符转比特
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="strCommand"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static byte[] GetBytes(string strCommand)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(strCommand))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] insByte = null;
|
|
|
|
|
string[] tempArray = strCommand.Split(',');
|
|
|
|
|
if (tempArray.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
_instruction = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tempArray.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
insByte = new byte[tempArray.Length];
|
|
|
|
|
for (int i = 0; i < tempArray.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
insByte[i] = Convert.ToByte(Convert.ToInt16(tempArray[i].Trim()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return insByte;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public static void OpenCashBoxs()
|
|
|
|
|
{
|
|
|
|
|
_port = Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_CASHBOX_PORT, "NONE");
|
|
|
|
|
|
|
|
|
|
if (_port.Equals("NONE"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_instruction = GetBytes(Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_CASHBOX_COMMAND, "27,112,0,128,128"));
|
|
|
|
|
|
|
|
|
|
cashBox.OpenCashBox();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|