using HslCommunication; using HslCommunication.Core; using Serilog; using Sln.Iot.PLC; using Sln.Iot.Repository.service; using Sln.Iot.Serilog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sln.Iot.Business { public class TestBusiness { private Timer _timer; private readonly PLCConnect _plc = PLCConnect.Instance; private readonly SerilogHelper _log = SerilogHelper.Instance; public TestBusiness() { _timer = new Timer(TimerCallback, null, 0, 1000); } /// /// RFID01上料提升机数据处理流程业务刷新 /// /// public void TimerCallback(object? state) { try { //var res = _plc.ReadBytes(_plc.DeltaInstance0, "D200", 9); //var bytes = res.Content.Reverse().ToArray(); //ushort[] shorts = new ushort[9]; //if (bytes != null && bytes.Length != 0) //{ // shorts[0] = BitConverter.ToUInt16(bytes, 0); // shorts[1] = BitConverter.ToUInt16(bytes, 2); // shorts[2] = BitConverter.ToUInt16(bytes, 4); // shorts[3] = BitConverter.ToUInt16(bytes, 6); // shorts[4] = BitConverter.ToUInt16(bytes, 8); // shorts[5] = BitConverter.ToUInt16(bytes, 10); // shorts[6] = BitConverter.ToUInt16(bytes, 12); // shorts[7] = BitConverter.ToUInt16(bytes, 14); // shorts[8] = BitConverter.ToUInt16(bytes, 16); //} //for (int i = 0; i < 9; i++) //{ // _log.Info($"D20{i * 2}数值[{shorts[i].ToString()}]"); //} //读取托盘码和产品码 OperateResult trayBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D5000", 10); OperateResult pordBytesResult = _plc.ReadBytes(_plc.DeltaInstance3, "D8000", 60); //转换托盘码 string traycode = Encoding.ASCII.GetString(trayBytesResult.Content); _log.Info("RFID读数" + traycode); byte[] prodBytes = pordBytesResult.Content; string[] prodcode = new string[6]; //分割转换产品码 for (int i = 0; i < 6; i++) { prodcode[i] = Encoding.ASCII.GetString(AscIIReverseTostring(prodBytes[(i * 20)..(i * 20 + 20)])); _log.Info($"扫码枪读数{i + 1}是{prodcode[i]}"); } } catch (Exception ex) { _log.Error("隧道炉温度读取失败", ex); } } public byte[] AscIIReverseTostring(byte[] bytes) { if (bytes.Length % 2 != 0) { return new byte[0]; } for (int i = 0; i < bytes.Length / 2; i++) { byte temp = bytes[i * 2]; bytes[i * 2] = bytes[i * 2 + 1]; bytes[i * 2 + 1] = temp; } return bytes; } } }