using JwKdsV.Core.HttpApi; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Management; using System.Net; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.IO; using System.Runtime.InteropServices; using System.Web; using JwKdsV.Entity.OrderPackage; using System.Media; using System.Speech.Synthesis; namespace JwKdsV.Core.Utils { public class HsPrinter { [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniOpenCom(string lpCom, int BaudRate); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniOpenUsb(); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniOpenLpt(string lptPort); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniConnectNetPortByIp(string ip, int port = 9100, int Timeout = 5000); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniClose(int fs); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniRead(int fs, byte[] readBuffer, int readSize); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniWrite(int fs, byte[] sendBuffer, int bufferSize); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniInitNetSev(); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int unisetRelativeHorizontalPrintPosition(int fs, int nL, int nH); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniinitializePrinter(int fs); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniprintAndFeedLine(int fs); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniprintBarcode(int fs, int m, string content);// [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniselectCutPagerModerAndCutPager(int fs, int m); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniPrintImg1d76(int fs, string buf, int m); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int unisendRealtimestatus(int fs, int n); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int unicreatCashboxContorlPulse(int fs, int m, int t1, int t2); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniTwobarCodes(int fs, string strTrxData, int nZoomIn = 7, int nErrorLevel = 1, int bCut = 0); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int unisetBarcodeWidth(int fs, int n); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int unisetBarcodeHeight(int fs, int n); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniselectHRICharacterPrintPosition(int fs, int n); [DllImport("JsPrinterDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] private static extern int uniselectHRIFont(int fs, int n); //private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); //string str; //string strOne; //int ip_exist = 0; //[DllImport("kernel32")] //private static extern int GetPrivateProfileString( // string lpAppName, // string lpKeyName, // string lpDefault, // StringBuilder lpReturnedString, // int nSize, // string lpFileName); //public string ContentReader(string area, string key, string def) //{ // StringBuilder stringBuilder = new StringBuilder(1024); // GetPrivateProfileString(area, key, def, stringBuilder, 1024, str); // return stringBuilder.ToString(); //} //[DllImport("kernel32")] //private static extern long WritePrivateProfileString( // string mpAppName, // string mpKeyName, // string mpDefault, // string mpFileName); //string kds_print_ip1 ; //string kds_print_ip2 ; //string kds_print_ip3 ; //string kds_print_ip4 ; public static int fs = 0; public static int bret = 0; public static int net_ok = 0; //增加另外一个打印机,在同一台电脑上。 public static int fs_1 = 0; public static int bret_1 = 0; public static int net_ok_1 = 0; public static string[] Desk_ChuPing_Number_Data = { "A00", "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09","A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "B01", "B02", "B03", "B05", "B06", "B08", "B09","B10", "B11", "B12", "B13", "B15", "B16", "B18","C01", "C02", "C03", "C05", "C06", "C08", "C09","C10", "C11", "C12", "C13", "C14", "C15","C16", "C17", "C18" }; public static int Hs_Enable_Printer(string ip, string desk_no, string product_name, string quantity, string order_type, string order_no, string model) { try { if (bret == 0)//Yao 2023-06-15 add bret = uniInitNetSev();// if (bret == 1) { for (int i = 0; i < 3; i++) { if (fs <= 0) { bret = uniInitNetSev(); fs = uniConnectNetPortByIp(ip, 9100, 5000); } if (fs > 0) break; else { System.Threading.Thread.Sleep(140); bret = uniInitNetSev();// fs = uniConnectNetPortByIp(ip, 9100, 5000); System.Threading.Thread.Sleep(280); } } //if (fs <= 0) if (fs <= 0) { MessageBox.Show("请检查打印机电源或网络1!"); } //else // uniinitializePrinter(fs); } else { MessageBox.Show("请检查打印机电源或网络2!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } // if (fs > 0) { int sec_fs = uniinitializePrinter(fs); if (sec_fs <= 0) { bret = uniInitNetSev();// fs = uniConnectNetPortByIp(ip, 9100, 5000); uniinitializePrinter(fs); } if (fs <= 0) { MessageBox.Show("请检查打印机电源或网络3!"); return 0; } Byte[] data = { 0x1D, 0x21, 0x12 }; Byte[] data1 = { 0x1D, 0x21, 0x11 }; string strBuff = " 出品单 \r\n"; unisetRelativeHorizontalPrintPosition(fs, 180, 00); // 构建小票内容 uniWrite(fs, data, data.Length); byte[] sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); uniprintAndFeedLine(fs); uniprintAndFeedLine(fs); uniWrite(fs, data1, data1.Length); int index = order_no.IndexOf('T'); if (order_no.IndexOf('T') != -1) { strBuff = "微信堂食";//微信堂食 } else if (order_no.IndexOf('D') != -1) { strBuff = "微信外带";//微信外带 } else strBuff = order_type; //sb.AppendLine(strBuff); //sb.AppendLine(order_no); //sb.AppendLine(desk_no); //sb.AppendLine(DateTime.Now.ToString()); //sb.AppendLine("-------------------"); //sb.Append(product_name); //sb.Append(" "); //sb.AppendLine(quantity); //string str = sb.ToString(); //sarry = Encoding.Default.GetBytes(str); //uniWrite(fs, sarry, sarry.Length); //uniselectCutPagerModerAndCutPager(fs, 66); //unisetRelativeHorizontalPrintPosition(fs, 180, 00); //Byte[] data = { 0x1D, 0x21, 0x12 }; //uniWrite(fs, data, data.Length); //string strBuff = " "; ////Yao 2023-06-22 增加做微信堂食和微信外带显示,根据单号的T和D开头做区别 //int index = order_no.IndexOf('T'); //if (order_no.IndexOf('T') != -1) //{ // strBuff = "微信堂食" + "\r\n";//微信堂食 //} //else if (order_no.IndexOf('D') != -1) //{ // strBuff = "微信外带" + "\r\n";//微信外带 //} //else // strBuff = " " + order_type + "\r\n"; //byte[] sarry = Encoding.Default.GetBytes(strBuff); //sarry = Encoding.Default.GetBytes(strBuff); //uniWrite(fs, sarry, sarry.Length); //Byte[] data1 = { 0x1D, 0x21, 0x11 }; //uniWrite(fs, data1, data1.Length); //strBuff = " 出品单 \r\n"; //sarry = Encoding.Default.GetBytes(strBuff); //uniWrite(fs, sarry, sarry.Length); //uniWrite(fs, data, data.Length); //uniprintAndFeedLine(fs); //uniprintAndFeedLine(fs); strBuff = order_no + "\r\n";//单号 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); uniWrite(fs, data1, data1.Length); strBuff = desk_no + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); strBuff = DateTime.Now.ToString() + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); strBuff = "-------------------\r\n"; uniWrite(fs, Encoding.Default.GetBytes(strBuff), strBuff.Length); strBuff = product_name + " " + quantity + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); unisetBarcodeHeight(fs, 80); uniprintBarcode(fs, 73, "01234567890"); uniselectCutPagerModerAndCutPager(fs, 66); return 1; } else return 0; } /// kds打印整单出品单 public static int Hs_ChuPing_Printer(string ip, ServiceOrder order) { try { //int bret = uniInitNetSev();//Yao 2023-06-15 test if (bret == 0)//Yao 2023-06-15 add bret = uniInitNetSev();// if (bret == 1) { for(int i=0;i<3;i++) { if (fs <= 0) { bret = uniInitNetSev(); fs = uniConnectNetPortByIp(ip, 9100, 5000); } if (fs > 0) break; else { System.Threading.Thread.Sleep(140); bret = uniInitNetSev();// fs = uniConnectNetPortByIp(ip, 9100, 5000); System.Threading.Thread.Sleep(280); } } //if (fs <= 0) if (fs <= 0) { MessageBox.Show("请检查打印机电源或网络1!"); } //else // uniinitializePrinter(fs); } else { MessageBox.Show("请检查打印机电源或网络2!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } // if (fs > 0) { int sec_fs = uniinitializePrinter(fs); if(sec_fs <= 0) { bret = uniInitNetSev();// fs = uniConnectNetPortByIp(ip, 9100, 5000); uniinitializePrinter(fs); } if (fs <= 0) { MessageBox.Show("请检查打印机电源或网络3!"); return 0; } //unisetRelativeHorizontalPrintPosition(fs, 180, 00); //Byte[] data = { 0x1D, 0x21, 0x12 }; //uniWrite(fs, data, data.Length); //string strBuff = " "; //if (order.OrderNo[0] == 'T') // strBuff = "微信堂食" + "\r\n";//微信堂食 //else if (order.OrderNo[0] == 'D') // strBuff = "微信外带" + "\r\n";//微信外带 //else // strBuff = " "+ order.OrderType + "\r\n";//堂食 //byte[] sarry = Encoding.Default.GetBytes(strBuff); //uniWrite(fs, sarry, sarry.Length); //Byte[] data1 = { 0x1D, 0x21, 0x11 }; //uniWrite(fs, data1, data1.Length); //strBuff = " 出品单 \r\n"; //sarry = Encoding.Default.GetBytes(strBuff); //uniWrite(fs, sarry, sarry.Length); //uniWrite(fs, data, data.Length); //uniprintAndFeedLine(fs); //uniprintAndFeedLine(fs); //strBuff = "单号:" + order.OrderNo + "\r\n";//单号 //sarry = Encoding.Default.GetBytes(strBuff); //uniWrite(fs, sarry, sarry.Length); unisetRelativeHorizontalPrintPosition(fs, 180, 00); Byte[] data = { 0x1D, 0x21, 0x12 }; uniWrite(fs, data, data.Length); string strBuff = " 出品单 \r\n"; byte[] sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); Byte[] data1 = { 0x1D, 0x21, 0x11 }; uniWrite(fs, data1, data1.Length); uniprintAndFeedLine(fs); uniprintAndFeedLine(fs); if (order.OrderNo[0] == 'T') strBuff = "微信堂食" + "\r\n";//微信堂食 else if (order.OrderNo[0] == 'D') strBuff = "微信外带" + "\r\n";//微信外带 else strBuff = order.OrderType + "\r\n";//堂食 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); strBuff = "单号:" + order.OrderNo + "\r\n";//单号 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); uniWrite(fs, data1, data1.Length); strBuff = "桌号:" + order.TableNo + "\r\n";//桌号 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); strBuff = DateTime.Now.ToString() + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); strBuff = "-------------------\r\n"; uniWrite(fs, Encoding.Default.GetBytes(strBuff), strBuff.Length); for (int j = 0; j < order.Items.Count; j++) { strBuff = order.Items[j].ProductName + " " + order.Items[j].Quantity + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs, sarry, sarry.Length); } unisetBarcodeHeight(fs, 80); uniprintBarcode(fs, 73, "01234567890"); uniselectCutPagerModerAndCutPager(fs, 66); return 1; } else return 0; } /// /// 打印单独外带部份 /// /// /// public static int Hs_WaiDai_Printer(string ip, ServiceOrder order) { try { if (bret_1 == 0)//Yao 2023-06-15 add bret_1 = uniInitNetSev();// if (bret_1 == 1) { for (int i = 0; i < 3; i++) { if (fs_1 <= 0) { bret_1 = uniInitNetSev(); fs_1 = uniConnectNetPortByIp(ip, 9100, 5000); } if (fs_1 > 0) break; else { System.Threading.Thread.Sleep(140); bret_1 = uniInitNetSev();// fs_1 = uniConnectNetPortByIp(ip, 9100, 5000); System.Threading.Thread.Sleep(280); } } if (fs_1 <= 0) { MessageBox.Show("请检查打印机电源或网络1!"); } } else { MessageBox.Show("请检查打印机电源或网络2!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } // if (fs_1 > 0) { int sec_fs = uniinitializePrinter(fs_1); if (sec_fs <= 0) { bret_1 = uniInitNetSev();// fs_1 = uniConnectNetPortByIp(ip, 9100, 5000); uniinitializePrinter(fs_1); } if (fs_1 <= 0) { MessageBox.Show("请检查打印机电源或网络3!"); return 0; } unisetRelativeHorizontalPrintPosition(fs_1, 180, 00); Byte[] data = { 0x1D, 0x21, 0x12 }; uniWrite(fs_1, data, data.Length); string strBuff = " 出品单 \r\n"; byte[] sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); Byte[] data1 = { 0x1D, 0x21, 0x11 }; uniWrite(fs_1, data1, data1.Length); uniprintAndFeedLine(fs_1); uniprintAndFeedLine(fs_1); strBuff = order.OrderType + "\r\n";//堂食 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); strBuff = "单号:" + order.OrderNo + "\r\n";//单号 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); //增加桌号的前缀A/B/C 4-7-14-17不作显示 if (char.IsDigit(order.TableNo[0])) { int number = Convert.ToInt16(order.TableNo); if (number <= 54) strBuff = "桌号:" + Desk_ChuPing_Number_Data[number] + "\r\n";//桌号 else strBuff = "桌号:" + "\r\n";//桌号 } else if (char.IsLetter(order.TableNo[0]) && (order.TableNo[0] == 'A' || order.TableNo[0] == 'B' || order.TableNo[0] == 'C')) { strBuff = "桌号:" + order.TableNo + "\r\n";//桌号 } //strBuff = "桌号:" + order.TableNo + "\r\n";//桌号 sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); strBuff = DateTime.Now.ToString() + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); strBuff = "-------------------\r\n"; uniWrite(fs_1, Encoding.Default.GetBytes(strBuff), strBuff.Length); for (int j = 0; j < order.Items.Count; j++) { strBuff = order.Items[j].ProductName + " " + order.Items[j].Quantity + "\r\n"; sarry = Encoding.Default.GetBytes(strBuff); uniWrite(fs_1, sarry, sarry.Length); } unisetBarcodeHeight(fs_1, 80); uniprintBarcode(fs_1, 73, "01234567890"); uniselectCutPagerModerAndCutPager(fs_1, 66); return 1; } else return 0; } public static void Play_JiaoHao_Sound(ServiceOrder order) { //SpeechSynthesizer speaker = new SpeechSynthesizer(); //speaker.SetOutputToDefaultAudioDevice(); //speaker.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 2, System.Globalization.CultureInfo.CurrentCulture); //speaker.Rate = -2; //speaker.Volume = 100; //string strBuff = " "; //strBuff = "请"+ order.OrderNo + "号取餐"; //speaker.SpeakAsync(strBuff); //speaker.SpeakAsync(strBuff); string path = string.Format("wav\\{0}.wav", order.OrderNo);//order.TableNo //string soundPath = bs.ToString(); string soundPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (File.Exists(soundPath)) { SoundPlayer simpleSound = new SoundPlayer(soundPath); simpleSound.Play(); Thread.Sleep(4000); simpleSound.Play(); } } public static void Play_JiaoHao_Sound_Queue(string line) { SpeechSynthesizer speaker = new SpeechSynthesizer(); speaker.SetOutputToDefaultAudioDevice(); speaker.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 2, System.Globalization.CultureInfo.CurrentCulture); speaker.Rate = -2; speaker.Volume = 100; string strBuff = " "; strBuff = "请" + line + "号取餐";//"请" + order.OrderNo + "号取餐" speaker.SpeakAsync(strBuff); speaker.SpeakAsync(strBuff); //if (line[0] == 'T') // sourceDic.Add("@营业模式@", "微信堂食" + "");//"堂吃" "微信堂食" + "\r\n";//微信堂食 //else if (line[0] == 'D') // sourceDic.Add("@营业模式@", "微信外带" + "");//微信外带 //else // sourceDic.Add("@营业模式@", order.OrderType + "");//"堂吃" //string path = string.Format("wav\\{0}.wav", line);//order.TableNo ////string soundPath = bs.ToString(); //string soundPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); //if (File.Exists(soundPath)) //{ // SoundPlayer simpleSound = new SoundPlayer(soundPath); // simpleSound.Play(); // Thread.Sleep(4000); // simpleSound.Play(); //} } public static void Play_JiaoHao_Sound_TD(string line) { string path = null; string soundPath = null; string path_T = null; string soundPath_T = null; string path_D = null; string soundPath_D = null; string stringWithoutFirstChar = ""; //line = "T002"; //line = "D002"; if (line[0] == 'T') { path_T = string.Format("wav\\{0}.wav", line[0]);//order.TableNo soundPath_T = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path_T); stringWithoutFirstChar = line.Substring(1); if (int.TryParse(stringWithoutFirstChar, out int number)) { path = string.Format("wav\\{0}.wav", number);//order.TableNo } else { number = 0; path = string.Format("wav\\{0}.wav", number);//order.TableNo } soundPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (File.Exists(soundPath) && File.Exists(soundPath_T)) { SoundPlayer simpleSound_T = new SoundPlayer(soundPath_T); SoundPlayer simpleSound = new SoundPlayer(soundPath); simpleSound_T.Play(); Thread.Sleep(320); simpleSound.Play(); Thread.Sleep(4000); simpleSound_T.Play(); Thread.Sleep(320); simpleSound.Play(); } } else if (line[0] == 'D') { path_D = string.Format("wav\\{0}.wav", line[0]);//order.TableNo soundPath_D = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path_D); // stringWithoutFirstChar = line.Substring(1); if (int.TryParse(stringWithoutFirstChar, out int number)) { path = string.Format("wav\\{0}.wav", number);//order.TableNo } else { number = 0; path = string.Format("wav\\{0}.wav", number);//order.TableNo } //path = string.Format("wav\\{0}.wav", number);//order.TableNo soundPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (File.Exists(soundPath) && File.Exists(soundPath_D)) { SoundPlayer simpleSound_D = new SoundPlayer(soundPath_D); SoundPlayer simpleSound = new SoundPlayer(soundPath); simpleSound_D.Play(); Thread.Sleep(320); simpleSound.Play(); Thread.Sleep(4000); simpleSound_D.Play(); Thread.Sleep(320); simpleSound.Play(); } } else { if (int.TryParse(line, out int number)) { path = string.Format("wav\\{0}.wav", number);//order.TableNo } else { number = 0; path = string.Format("wav\\{0}.wav", number);//order.TableNo } //path = string.Format("wav\\{0}.wav", line);//order.TableNo soundPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (File.Exists(soundPath)) { SoundPlayer simpleSound = new SoundPlayer(soundPath); simpleSound.Play(); Thread.Sleep(4000); simpleSound.Play(); } } } } }