using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
|
namespace TProduct.Link.Kedi
|
{
|
/// <summary>
|
/// 科迪 SI
|
/// </summary>
|
public class SiKdMsgHelper
|
{
|
/// <summary>
|
/// 构建查询类
|
/// </summary>
|
/// <param name="startRegSet"></param>
|
/// <param name="numRegSet"></param>
|
/// <returns></returns>
|
public static byte[] BuildQueryMsg(int startRegSet, int numRegSet)
|
{
|
/*
|
发送 02 30 31 30 30 30 30 36 03 35 41
|
返回 02 30 30 30 30 30 34 30 30 30 31 30 30 03 34 38
|
|
解析发送
|
02 30 固定
|
31 30 30 30 开始地址
|
30 36 数量*2
|
03 //固定
|
35 41 校验
|
|
解析返回
|
02
|
30 30 30 30 //3412
|
30 34 30 30
|
30 31 30 30
|
03
|
34 38
|
|
out(0) = &H2 '读取固定位
|
out(1) = &H30
|
|
out(2) = &H31 '1000+起始地址*2 转ASCII 5*2=10 对应A +1000 =100A 转 31 30 30 41
|
out(3) = &H30
|
out(4) = &H30
|
out(5) = &H30
|
|
out(6) = &H30 '寄存器个数 读一个的话 都要*2 读10个 20 就是14 就是31 34
|
out(7) = &H36
|
|
out(8) = &H3 '固定位
|
*/
|
|
|
|
byte[] commandSend = new byte[11];
|
commandSend[0] = 0x02;
|
commandSend[1] = 0x30;
|
|
//开始位 '1000+起始地址*2 转ASCII 5*2=10 对应A +1000 =100A 转 31 30 30 41
|
var bStartRegSet = ToByte2(startRegSet * 2);
|
var sStartRegSet = BitConverter.ToString(bStartRegSet, 0, bStartRegSet.Length);
|
var split_sStartRegSet = sStartRegSet.Split('-');
|
var h_startRegtSet = ToAsciiByte(split_sStartRegSet[1]);
|
var l_startRegtSet = ToAsciiByte(split_sStartRegSet[0]);
|
commandSend[2] = 0x31;
|
commandSend[3] = h_startRegtSet[0];
|
commandSend[4] = l_startRegtSet[1];
|
commandSend[5] = l_startRegtSet[0];
|
|
//寄存器个数 读一个的话 都要*2 读10个 20 就是14 就是31 34
|
var bNumRegSet = ToByte2(numRegSet * 2);
|
var sNumRegSet = BitConverter.ToString(bNumRegSet, 0, bNumRegSet.Length);
|
var split_sNumRegSet = sNumRegSet.Split('-');
|
var l_numRegSet = ToAsciiByte(split_sNumRegSet[0]);
|
commandSend[6] = l_numRegSet[1];
|
commandSend[7] = l_numRegSet[0];
|
|
commandSend[8] = 0x03;
|
long num = 0;
|
for (int i = 1; i <= 8; i++)
|
{
|
num += commandSend[i];
|
}
|
long hex_h; long hex_l;
|
CreateCRC(num, out hex_h, out hex_l);
|
|
commandSend[9] = Convert.ToByte(hex_h);
|
commandSend[10] = Convert.ToByte(hex_l);
|
|
return commandSend;
|
}
|
|
|
/// <summary>
|
/// 抽取值
|
/// </summary>
|
/// <param name="byteMessage"></param>
|
/// <param name="valveIndex">从0开始</param>
|
/// <returns></returns>
|
public static int AbstactByteValue(IEnumerable<byte> byteMessage, int valveIndex)
|
{
|
int m1, m2, m3, m4;
|
m1 = byteMessage.ElementAt(1 + valveIndex * 4) - 48;
|
m2 = byteMessage.ElementAt(2 + valveIndex * 4) - 48;
|
m3 = byteMessage.ElementAt(3 + valveIndex * 4) - 48;
|
m4 = byteMessage.ElementAt(4 + valveIndex * 4) - 48;
|
|
m1 = (m1 / 16) * 10 + (m1 % 16);
|
m2 = (m2 / 16) * 10 + (m2 % 16);
|
m3 = (m3 / 16) * 10 + (m3 % 16);
|
m4 = (m4 / 16) * 10 + (m4 % 16);
|
|
if (m1 >= 11) m1 = m1 - 1;
|
if (m2 >= 11) m2 = m2 - 1;
|
if (m3 >= 11) m3 = m3 - 1;
|
if (m4 >= 11) m4 = m4 - 1;
|
|
return m3 * (16 ^ 3) + m4 * (16 ^ 2) + m1 * 16 + m2;
|
}
|
|
private static byte[] ToByte4(int u)
|
{
|
byte[] b = new byte[4];
|
b[0] = (byte)(u);
|
b[1] = (byte)(u >> 8);
|
b[2] = (byte)(u >> 16);
|
b[3] = (byte)(u >> 24);
|
|
return b;
|
}
|
private static byte[] ToByte2(int u)
|
{
|
byte[] b = new byte[2];
|
b[0] = (byte)(u);
|
b[1] = (byte)(u >> 8);
|
|
return b;
|
}
|
|
private static byte[] ToAsciiByte(string b)
|
{
|
byte[] bb = new byte[2];
|
bb[0] = ToCharByte(b[1]);//反过来
|
bb[1] = ToCharByte(b[0]);
|
return bb;
|
}
|
public static byte[] IntToBytes4(int value)
|
{
|
byte[] src = new byte[4];
|
src[3] = (byte)((value >> 24) & 0xFF);
|
src[2] = (byte)((value >> 16) & 0xFF);
|
src[1] = (byte)((value >> 8) & 0xFF);
|
src[0] = (byte)(value & 0xFF);
|
return src;
|
}
|
public static byte[] IntToBytes2(int value)
|
{
|
byte[] src = new byte[2];
|
src[1] = (byte)((value >> 8) & 0xFF);
|
src[0] = (byte)(value & 0xFF);
|
return src;
|
}
|
/// <summary>
|
/// 按 3412的结构把b换算
|
/// </summary>
|
/// <param name="b"></param>
|
/// <returns></returns>
|
public static byte[] ToAsciiByte4(int val)
|
{
|
byte[] b_arr = new byte[2];
|
b_arr[0] = (byte)((val >> 8) & 0xFF);
|
b_arr[1] = (byte)(val & 0xFF);
|
|
var sss = BitConverter.ToString(b_arr, 0, b_arr.Length);
|
|
return new byte[4] {
|
ToCharByte(sss[3]),
|
ToCharByte(sss[4]),//2 是 -
|
ToCharByte(sss[0]),
|
ToCharByte(sss[1]) };
|
}
|
|
|
public static byte ToCharByte(char b)
|
{
|
switch (b)
|
{
|
case '0':
|
return 0x30;
|
case '1':
|
return 0x31;
|
case '2':
|
return 0x32;
|
case '3':
|
return 0x33;
|
case '4':
|
return 0x34;
|
case '5':
|
return 0x35;
|
case '6':
|
return 0x36;
|
case '7':
|
return 0x37;
|
case '8':
|
return 0x38;
|
case '9':
|
return 0x39;
|
case 'A': //A
|
case 'a':
|
return 0x41;
|
case 'B'://B
|
case 'b':
|
return 0x42;
|
case 'C'://C
|
case 'c':
|
return 0x43;
|
case 'D'://D
|
case 'd':
|
return 0x44;
|
case 'E'://E
|
case 'e':
|
return 0x45;
|
case 'F'://F
|
case 'f':
|
return 0x46;
|
}
|
|
return 0x30;
|
}
|
|
/// <summary>
|
/// 产生校验码
|
/// </summary>
|
/// <param name="num"></param>
|
/// <param name="hex_h"></param>
|
/// <param name="hex_l"></param>
|
public static void CreateCRC(long num, out long hex_h, out long hex_l)
|
{
|
/*
|
If num >= 256 Then num = num Mod 256
|
hex_h = num \ 16: hex_l = num Mod 16
|
If hex_h >= 10 Then hex_h = hex_h + 1: hex_h = (hex_h \ 10) * 16 + (hex_h Mod 10)
|
If hex_l >= 10 Then hex_l = hex_l + 1: hex_l = (hex_l \ 10) * 16 + (hex_l Mod 10)
|
hex_h = 48 + hex_h: hex_l = 48 + hex_l
|
*/
|
|
if (num >= 256)
|
{
|
num = num % 256;
|
}
|
|
hex_h = num / 16;
|
hex_l = num % 16;
|
|
|
if (hex_h >= 10)
|
{
|
hex_h = hex_h + 1;
|
hex_h = (hex_h / 10) * 16 + (hex_h % 10);
|
}
|
if (hex_l >= 10)
|
{
|
hex_l = hex_l + 1;
|
hex_l = (hex_l / 10) * 16 + (hex_l % 10);
|
}
|
hex_h = 48 + hex_h;
|
hex_l = 48 + hex_l;
|
}
|
|
}
|
}
|