/// <summary> /// 执行开钱箱操作 /// 没钱箱或打印机原功能都可以正常使用 /// </summary> public void ExecuteOpenCashBoxOperate() { string l_strDefaultPortName = ""; string l_strDefaultBrand = ""; if (serverCommon.ReadLocalSet("PrintSet", "UseCashBox", "0") == "0") {//如果不使用钱箱直接退出,默认不使用钱箱 return; } try { //获取默认打印机的相关信息 string l_strSQL = string.Format("SELECT * from Win32_Printer where default = true "); ManagementObjectSearcher searcher = new ManagementObjectSearcher(l_strSQL); ManagementObjectCollection printers = searcher.Get(); foreach (ManagementObject print in printers) { l_strDefaultPortName = print["PortName"].ToString(); l_strDefaultBrand = print["DriverName"].ToString();//驱动名称不能冲突 做为识别牌子的标准 } if (string.IsNullOrEmpty(l_strDefaultPortName)) return; string l_strNetIP = serverCommon.ReadLocalSet("PrintSet", "NetIP", "192.168.0.31"); string l_strNetPort = serverCommon.ReadLocalSet("PrintSet", "NetPort", "9100"); string l_strBaudRate = serverCommon.ReadLocalSet("PrintSet", "BaudRate", "38400"); string l_strDataBits = serverCommon.ReadLocalSet("PrintSet", "DataBits", "128"); int l_intPortCOM = Int32.Parse(serverCommon.ReadLocalSet("PrintSet", "PortCOM", "2")); int l_intBaudRateCOM = Int32.Parse(serverCommon.ReadLocalSet("PrintSet", "BaudRateCOM", "9600")); int l_intDataBitsCOM = Int32.Parse(serverCommon.ReadLocalSet("PrintSet", "DataBitsCOM", "8")); if (l_strDefaultBrand.IndexOf("BTP") > -1 && l_strDefaultPortName.IndexOf("USB") > -1) { OpenBYCashBox(); } else if (l_strDefaultBrand.IndexOf("GP") > -1 && l_strDefaultPortName.IndexOf("IP") > -1) { OpenJBCashBox(l_strNetIP, l_strNetPort); } else if (l_strDefaultBrand.IndexOf("EPSON") > -1) { OpenEPSONCashBox(l_intPortCOM, l_intBaudRateCOM, l_intDataBitsCOM); //OpenEPSONCashBox(2,9600,8); } } catch { } finally {// 当WMI 服务关闭的时候 } } private void OpenBYCashBox() { bool IsOpen = false; BeiYangOPOS BYOPOS = new BeiYangOPOS(); try { IsOpen = BYOPOS.OpenUSBPort("BYUSB-0"); if (IsOpen) { IntPtr res = BeiYangOPOS.POS_KickOutDrawer(0x00, 100, 80); } } finally { BYOPOS.ClosePrinterPort(); } } private void OpenJBCashBox(string a_strNetIP, string a_strPort) { IPAddress ServeIP = IPAddress.Parse(a_strNetIP); IPEndPoint remoteEP = new IPEndPoint(ServeIP, Int32.Parse(a_strPort)); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(remoteEP); byte[] byteA = new byte[] { 0x1B, 0x70, 0x00, 0x80, 0xFF };//据说是佳博所有版本通用命令 //byte[] byteA = new byte[] { 0x10, 0x14, 0x01, 0x01, 0x03 }; clientSocket.Send(byteA); } finally { clientSocket.Close(); } } private void OpenEPSONCashBox(int a_strPortName, int a_intBaudRate, int a_intDataBits) { System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort(); sp.PortName = "COM" + a_strPortName.ToString(); sp.BaudRate = a_intBaudRate; sp.DataBits = a_intDataBits; sp.ReadTimeout = 2000; sp.Parity = System.IO.Ports.Parity.None; sp.StopBits = System.IO.Ports.StopBits.OnePointFive; try { sp.Open(); byte[] byteA = { 0x1B, 0x70, 0x00, 0x45, 0x45 }; sp.Write(byteA, 0, byteA.Length); System.Threading.Thread.Sleep(100); } finally {//如果端口被占用需要重新设置 sp.Close(); } }
读取配置文件的类
class LocalSet
{
private const string CONFIGFILE = "Config.ini";
//配置文件的文件名
/// <summary>
/// 获取指定配置文件内的指定项目的指定设置的设定值
/// </summary>
/// <param>项目名</param>
/// <param>设置名</param>
/// <param>默认值</param>
/// <param>返回值</param>
/// <param>返回值大小</param>
/// <param>配置文件路径</param>
/// <returns>返回设定值长度</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "GetPrivateProfileStringA")]
private static extern int GetPrivateProfileString([In, MarshalAs(UnmanagedType.LPStr)] string lpApplicationName, [In, MarshalAs(UnmanagedType.LPStr)] string lpKeyName, [In, MarshalAs(UnmanagedType.LPStr)] string lpDefault, [Out, MarshalAs(UnmanagedType.LPStr)] StringBuilder lpReturnedString, uint nSize, [In, MarshalAs(UnmanagedType.LPStr)] string lpFileName);
/// <summary>
/// 设置指定配置文件内的指定项目的指定设置的设定值
/// </summary>
/// <param>项目名</param>
/// <param>设置名</param>
/// <param>设定值</param>
/// <param>配置文件路径</param>
/// <returns>返回设定值长度</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "WritePrivateProfileStringA")]
private static extern int WritePrivateProfileString([In, MarshalAs(UnmanagedType.LPStr)] string lpApplicationName, [In, MarshalAs(UnmanagedType.LPStr)] string lpKeyName, [In, MarshalAs(UnmanagedType.LPStr)] string lpString, [In, MarshalAs(UnmanagedType.LPStr)] string lpFileName);