using System; using System.IO.Ports; namespace Common { public class LedHelper { private static int _baudRate = 2400; private static string _portName = "COM2"; /// <summary> /// 串型端口对象 /// </summary> private static SerialPort _serialPort; /// <summary> /// 波特率 /// </summary> public static int BaudRate { get { return _baudRate; } set { _baudRate = value; } } /// <summary> /// 端口 /// </summary> public static string PortName { get { return _portName; } set { _portName = value; } } /// <summary> /// 清空屏幕 /// </summary> public static void ScreenClean() { Open(); Clear(); Close(); } /// <summary> /// 显示单价 /// </summary> /// <param name="price"></param> public static void ShowUnitPrice(string price) { Open(); UnitPrice(price); Close(); } /// <summary> /// 显示应收 /// </summary> /// <param name="money"></param> public static void ShowSum(string money) { Open(); Sum(money); Close(); } /// <summary> /// 显示实际收钱 /// </summary> /// <param name="money"></param> public static void ShowRealMoney(string money) { Open(); CollectMoney(money); Close(); } /// <summary> /// 找零 /// </summary> /// <param name="money"></param> public static void ShowOddChange(string money) { Open(); OddChange(money); Close(); } /// <summary> /// 全暗 /// </summary> public static void ShowNone(string money) { Open(); GetBlack(money); Close(); } /// <summary> /// GetSum 总价 /// </summary> /// <param name="value">The value.</param> private static void Sum(string value) { //clear(); NewData(value); GetSum(); } /// <summary> /// GetUnitPrice 单价 /// </summary> /// <param name="value">The value.</param> private static void UnitPrice(string value) { //clear(); NewData(value); GetUnitPrice(); } /// <summary> /// GetCollectMoney 实收 /// </summary> /// <param name="value">The value.</param> private static void CollectMoney(string value) { //clear(); NewData(value); GetCollectMoney(); } /// <summary> /// GetOddChange 找零 /// </summary> /// <param name="value">The value.</param> private static void OddChange(string value) { //clear(); NewData(value); GetOddChange(); } /// <summary> /// 全黑 /// </summary> /// <param name="value"></param> private static void GetBlack(string value) { //clear(); NewData(value); AllBlack(); } /// <summary> ///全暗.是找零 全价 GetCollectMoney 这几个灯 /// </summary> private static void AllBlack() { // serialPort1.WriteLine(@"s0"); _serialPort.Write(@"s0"); } /// <summary> /// 清屏. /// </summary> private static void Clear() { //serialPort1.WriteLine("f"); _serialPort.Write(((char)(12)).ToString()); } private static void GetUnitPrice() { //serialPort1.WriteLine(@"s1");//其实有数据的那个空格 _serialPort.Write(@"s1");//其实有数据的那个空格 } private static void GetSum() { _serialPort.WriteLine(@"s2"); } private static void GetCollectMoney() { _serialPort.WriteLine(@"s3"); } private static void GetOddChange() { _serialPort.WriteLine(@"s4"); } private static void NewData(string data) { //serialPort1.WriteLine(@"QA" + data); _serialPort.Write(@"QA" + data + ((char)(13)).ToString()); } /// <summary> /// 打开串口,可能抛出异常 /// </summary> private static void Open() { try { if (_serialPort == null) { _serialPort = new SerialPort(); _serialPort.PortName = _portName; _serialPort.BaudRate = _baudRate; } if (!_serialPort.IsOpen) { _serialPort.Open(); } } catch (Exception ex) { throw new Exception("打开串口失败!错误信息为:" + ex.Message); } } /// <summary> /// 关闭串口 /// </summary> private static void Close() { if (_serialPort != null && _serialPort.IsOpen) _serialPort.Close(); } } }