123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using uint16_t = System.UInt16;
- using uint8_t = System.Byte;
- using int16_t = System.Int16;
- using int8_t = System.SByte;
- using System.Windows.Threading;
- namespace WpfTest1.ComAgent
- {
- public unsafe static class Extension
- {
- public static ushort cal_crc(byte[] ptr)
- {
- ushort crc = 0;
- byte num = 0;
- int len = ptr.Length;
- while (len-- != 0)
- {
- for (byte i = 128; i != 0; i = (byte)((int)i / 2))
- {
- if ((crc & 0x8000u) != 0)
- {
- crc = (ushort)(crc * 2);
- crc = (ushort)(crc ^ 0x1021u);
- }
- else
- {
- crc = (ushort)(crc * 2);
- }
- if ((ptr[num] & i) != 0)
- {
- crc = (ushort)(crc ^ 0x1021u);
- }
- }
- num = (byte)(num + 1);
- }
- return crc;
- }
- private static byte[] pack0x10(byte[] source)
- {
- List<byte> list = new List<byte>();
- for (int i = 0; i < source.Length; i++)
- {
- if (source[i] == 16)
- {
- list.Add(16);
- }
- list.Add(source[i]);
- }
- return list.ToArray();
- }
- private static byte[] unpack0x10(byte[] dest)
- {
- List<byte> list = new List<byte>();
- for (int i = 0; i < dest.Length; i++)
- {
- if (dest[i] == 16)
- {
- i++;
- }
- list.Add(dest[i]);
- }
- return list.ToArray();
- }
- public unsafe static byte[] copyz(ref byte* begin, int size)
- {
- byte[] list = new byte[size];
- for (int i = 0; i < size; i++)
- {
- list[i] = *begin++;
- }
- return list;
- }
- public static bool bEquals(this byte[] lhs, byte[] rhs)
- {
- if (lhs.Length != rhs.Length)
- {
- return false;
- }
- for (int i = 0; i < lhs.Length; i++)
- {
- if (lhs[i] != rhs[i])
- {
- return false;
- }
- }
- return true;
- }
- public static bool bEquals(this byte[] lhs, byte[] rhs, int len)
- {
- if (lhs.Length < len || rhs.Length < len)
- {
- return false;
- }
- for (int i = 0; i < len; i++)
- {
- if (lhs[i] != rhs[i])
- {
- return false;
- }
- }
- return true;
- }
- public static T[] pack<T>(this T first, params T[] other)
- {
- T[] t = new T[other.Length + 1];
- t[0] = first;
- for (int i = 0; i < other.Length; i++)
- {
- t[i + 1] = other[i];
- }
- return t;
- }
- public static void shutdown(this ISerialPortAdapter sp)
- {
- try
- {
- sp?.Close();
- sp?.Dispose();
- }
- catch
- {
- }
- }
- public static void send(this ISerialPortAdapter sp, byte[] data)
- {
- sp.Write(data, 0, data.Length);
- }
- public static void read(this ISerialPortAdapter sp, byte[] data, int len)
- {
- int cursor = 0;
- while ((cursor += sp.Read(data, cursor, len - cursor)) < len)
- {
- }
- }
- public static byte b(this int i)
- {
- return (byte)i;
- }
- public static void addRange<T>(this List<T> list, T[] buff, int len)
- {
- for (int i = 0; i < len; i++)
- {
- list.Add(buff[i]);
- }
- }
- public static byte[] buff(this int i)
- {
- return BitConverter.GetBytes(i);
- }
- public static byte[] buff(this uint i)
- {
- return BitConverter.GetBytes(i);
- }
- public static byte[] buff(this byte i)
- {
- return i.pack();
- }
- public static byte[] buff(this ushort i)
- {
- byte[] buff = BitConverter.GetBytes(i);
- Array.Reverse(buff);
- return buff;
- }
- public static byte[] buff(this short i)
- {
- byte[] buff = BitConverter.GetBytes(i);
- Array.Reverse(buff);
- return buff;
- }
- public static byte[] packData(this byte[] inputData)
- {
- byte[] data = pack0x10(inputData);
- byte[] data_size = ((ushort)(data.Length + 2)).buff();
- List<byte> buff = new List<byte>();
- buff.Add(16);
- buff.Add(2);
- buff.AddRange(data_size);
- buff.AddRange(data);
- buff.AddRange(cal_crc(data_size.add(data)).buff());
- buff.Add(16);
- buff.Add(3);
- return buff.ToArray();
- }
- public static T[] add<T>(this T[] obj, T[] other)
- {
- T[] list = new T[obj.Length + other.Length];
- for (int j = 0; j < obj.Length; j++)
- {
- list[j] = obj[j];
- }
- for (int i = 0; i < other.Length; i++)
- {
- list[i + obj.Length] = other[i];
- }
- return list;
- }
- public unsafe static byte[] unpackData(this byte[] inputData)
- {
- byte[] buff = null;
- fixed (byte* ptr_constraint = inputData)
- {
- byte* ptr = ptr_constraint;
- if (!copyz(ref ptr, 2).bEquals(new byte[2] { 16, 2 }))
- {
- throw new ComException("Invaild package head.");
- }
- ushort length = BitConverter.ToUInt16(copyz(ref ptr, 2).Reverse().ToArray(), 0);
- buff = copyz(ref ptr, length - 2);
- ushort ucrc = BitConverter.ToUInt16(copyz(ref ptr, 2), 0);
- #if !IGNCRC
- if (cal_crc(buff) != ucrc)
- {
- throw new ComException("CRC check failed.");
- }
- #endif
- if (!copyz(ref ptr, 2).bEquals(new byte[2] { 16, 3 }))
- {
- throw new ComException("Invaild package foot.");
- }
- }
- return buff;
- }
- public static T[] subArray<T>(this T[] array, int begin, int len)
- {
- T[] arr = new T[len];
- for (int i = 0; i < len; i++)
- {
- arr[i] = array[begin + i];
- }
- return arr;
- }
- public static void async(this object any, Action act)
- {
- any.async(act, App.Current.Dispatcher);
- }
- public static void async(this object any, Action act, Dispatcher dispatcher)
- {
- dispatcher.Invoke(act);
- }
- public static string print16(this byte[] buff)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < buff.Length; i++)
- {
- sb.AppendFormat("{0:x2} ", buff[i]);
- }
- return sb.ToString();
- }
- }
- }
|