using POSV.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; namespace POSV { public class DaHuaACS15AB { static SerialPortExtension serialPort = null; Timer timer = null; string portName; string baudRate; string dataBits; string stopBits; string parity; string handshake; static decimal _value = 0.00M; public static bool Open(string portName, string baudRate, string dataBits, string stopBits, string parity, string handshake) { serialPort = new SerialPortExtension(); serialPort.ReceiveDataEvent -= DHACSReceiveDataEvent; serialPort.ReceiveDataEvent += DHACSReceiveDataEvent; var result = serialPort.Open(portName, baudRate, "8", "1", "0", "None"); if (result) { serialPort.Send(new byte[1] { 0x50 }); return true; } else { serialPort.Close(); serialPort.DiscardOutBuffer(); return false; } } public static decimal GetWeight(string portName, string baudRate) { _value = 0.00M; Open(portName, baudRate, "8", "1", "0", "None"); System.Threading.Thread.Sleep(500); return _value; } private static void DHACSReceiveDataEvent(object sender, SerialPortEventArgs e) { var bytes = e.ReceivedBytes; try { var _str = System.Text.Encoding.ASCII.GetString(bytes); string[] _val = _str.Split(' ', 'K', 'G'); _value = decimal.Parse(_val[1]) /1000 ; } catch (Exception ex) { } finally { if (serialPort != null) { if (serialPort.IsOpen) { serialPort.Close(); } } } //模拟数据 //var bytes = new byte[11] { 0x02, 0x20, 0x20, 0x30, 0x31, 0x2e, 0x33, 0x34, 0x20, 0x4b, 0x47 }; //var hex = BitConverter.ToString(bytes, 0); //var recv = BitConverter.ToString(bytes).Replace("-", string.Empty); //logger.Info("收到大华ACS数据<{0}>", hex); //Console.WriteLine("Received Data<{0}>:{1} ", e.ReceivedBytes.Length, recv); } } }