From b7308bba3d7ffad271ce7fc7a93c8c45d76be87d Mon Sep 17 00:00:00 2001 From: 杨前锦 <1010338399@qq.com> Date: 星期五, 13 六月 2025 17:21:03 +0800 Subject: [PATCH] 优化印尼佳通-硫化胚胎出入库逻辑策略优化 --- HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/PlcHelper.cs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 99 insertions(+), 0 deletions(-) diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/PlcHelper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/PlcHelper.cs new file mode 100644 index 0000000..ec945ed --- /dev/null +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.FJJT/device/PlcHelper.cs @@ -0,0 +1,99 @@ +锘縰sing HH.WCS.Mobox3.FJJT.process; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HH.WCS.Mobox3.FJJT.device +{ + internal class PlcHelper + { + internal static void Receive(string ip, string msg) { + //澶勭悊璁惧淇″彿 + DeviceProcess.Analysis(msg, ip); + } + internal static bool SendHex(string ip, string msg) { + return TcpServer.TcpServerSend(ip, Hex2Bytes(msg)); + + } + internal static void SendAscii(string ip, string msg) { + TcpServer.TcpServerSend(ip, Encoding.ASCII.GetBytes(msg)); + } + + internal static byte[] Hex2Bytes(string hexString) { + hexString = hexString.Replace(" ", ""); + if ((hexString.Length % 2) != 0) + hexString += " "; + byte[] returnBytes = new byte[hexString.Length / 2]; + for (int i = 0; i < returnBytes.Length; i++) + returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); + + return returnBytes; + } + internal static string Hex2Ascii(string hexString) { + hexString = hexString.Replace(" ", ""); + if ((hexString.Length % 2) != 0) + hexString += " "; + byte[] returnBytes = new byte[hexString.Length / 2]; + for (int i = 0; i < returnBytes.Length; i++) + returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); + + return Encoding.ASCII.GetString(returnBytes); + } + + #region 杩涘埗杞崲+CRC + internal static bool CheckCRC(string hex) { + var result = false; + var data = hex.Replace(" ", ""); + if (data.Length % 2 == 0) { + var code1 = data.Substring(data.Length - 4, 4).ToLower(); + var code2 = BitConverter.ToString(CRC16LH(Hex2Bytes(data.Substring(0, data.Length - 4)))).Replace("-", "").Replace(" ", "").ToLower(); + result = code1 == code2; + } + return result; + } + internal static byte[] CRC16LH(byte[] pDataBytes) { + ushort crc = 0xffff; + ushort polynom = 0xA001; + + for (int i = 0; i < pDataBytes.Length; i++) { + crc ^= pDataBytes[i]; + for (int j = 0; j < 8; j++) { + if ((crc & 0x01) == 0x01) { + crc >>= 1; + crc ^= polynom; + } + else { + crc >>= 1; + } + } + } + + byte[] result = BitConverter.GetBytes(crc); + return result; + } + internal static byte[] CRC16HL(byte[] pDataBytes) { + ushort crc = 0xffff; + ushort polynom = 0xA001; + + for (int i = 0; i < pDataBytes.Length; i++) { + crc ^= pDataBytes[i]; + for (int j = 0; j < 8; j++) { + if ((crc & 0x01) == 0x01) { + crc >>= 1; + crc ^= polynom; + } + else { + crc >>= 1; + } + } + } + + byte[] result = BitConverter.GetBytes(crc).Reverse().ToArray(); + return result; + } + + #endregion + } +} -- Gitblit v1.9.1