📄 justinio.cs
字号:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace JustinIO
{
class CommPort
{
public bool Opened = false;
}
class HexCon
{
//--------字节型转换成十六进制字符串---------
public string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 2);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0')); //.PadRight(3, ' '));
return sb.ToString().ToUpper();
}
//--------十六进制字符串转换成字节型----------
public byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
///---------------------CRC校验----------------------
public string CRCCITT(string DataInfo)
{
byte CCittH = 0x10;
byte CCittL = 0x21;
byte CRC16L;
byte CRC16H;
byte temp1;
string StrReturn = "";
byte[] InData = new byte[DataInfo.Length / 2];
for (int i = 0; i < DataInfo.Length; i += 2)
{
InData[(i + 1) / 2] = (HexToDec(DataInfo.Substring(i, 2)));
}
CRC16H = InData[0];
CRC16L = InData[1];
for (int i = 2; i < (DataInfo.Length) / 2; i++)
{
for (int j = 1; j <= 8; j++)
{
temp1 = (byte)(CRC16H | 0x7F);
if (temp1 == 0xFF)
{
temp1 = (byte)(CRC16L | 0x7F);
CRC16H = (byte)(CRC16H & 0x7F);
CRC16H = (byte)(CRC16H * 2);
if (temp1 == 0xFF)
{
CRC16H = (byte)(CRC16H | 0x01);
}
temp1 = (byte)(InData[i] | 0x7F);
CRC16L = (byte)(CRC16L & 0x7F);
CRC16L = (byte)(CRC16L * 2);
if (temp1 == 0xFF)
{
CRC16L = (byte)(CRC16L | 0x01);
}
CRC16L = (byte)(CRC16L ^ CCittL);
CRC16H = (byte)(CRC16H ^ CCittH);
InData[i] = (byte)(InData[i] & 0x7F);
InData[i] = (byte)(InData[i] * 2);
}
else
{
temp1 = (byte)(CRC16L | 0x7F);
CRC16H = (byte)(CRC16H & 0x7F);
CRC16H = (byte)(CRC16H * 2);
if (temp1 == 0xFF)
{
CRC16H = (byte)(CRC16H | 0x01);
}
temp1 = (byte)(InData[i] | 0x7F);
CRC16L = (byte)(CRC16L & 0x7F);
CRC16L = (byte)(CRC16L * 2);
if (temp1 == 0xFF)
{
CRC16L = (byte)(CRC16L | 0x01);
}
InData[i] = (byte)(InData[i] & 0x7F);
InData[i] = (byte)(InData[i] * 2);
}
}
}
StrReturn = CRC16H.ToString("X2") + CRC16L.ToString("X2");
return StrReturn;
}
/// -------------十六进制字母转换位十进制字母--------------
public byte HexToDec(string DataInfo)
{
byte uReturn = 0;
char[] chars = DataInfo.ToCharArray();
byte AItmp = 0;
byte BItem = 0;
if (chars[0] == (char)'A')
{
AItmp = (byte)10 * 16;
}
else
{
if (chars[0] == (char)'B')
{
AItmp = (byte)11 * 16;
}
else
{
if (chars[0] == (char)'C')
{
AItmp = (byte)12 * 16;
}
else
{
if (chars[0] == (char)'D')
{
AItmp = (byte)13 * 16;
}
else
{
if (chars[0] == (char)'E')
{
AItmp = (byte)14 * 16;
}
else
{
if (chars[0] == (char)'F')
{
AItmp = (byte)15 * 16;
}
else
{
AItmp = (byte)(((byte)chars[0] - 48) * 16);
}
}
}
}
}
}
if (chars[1] == (char)'A')
{
BItem = 10;
}
else
{
if (chars[1] == (char)'B')
{
BItem = 11;
}
else
{
if (chars[1] == (char)'C')
{
BItem = 12;
}
else
{
if (chars[1] == (char)'D')
{
BItem = 13;
}
else
{
if (chars[1] == (char)'E')
{
BItem = 14;
}
else
{
if (chars[1] == (char)'F')
{
BItem = 15;
}
else
{
BItem = (byte)(((byte)chars[1] - 48));
}
}
}
}
}
}
uReturn = (byte)(AItmp + BItem);
return uReturn;
}
///--------------校验接收数据包-------------
private bool VerifyDataBag(string sDataBag)
{
int iLen = 0;
string sCrcRev = "", sCrcNow = "";
if (sDataBag.Length % 2 == 0)
{
iLen = sDataBag.Length;
sCrcRev = sDataBag.Substring(iLen - 2, 2) + sDataBag.Substring(iLen - 4, 2);
sCrcNow = CRCCITT(sDataBag.Substring(0, iLen - 4) + "0000");
if (sCrcRev == sCrcNow)
return true;
else
return false;
}
else
{
return false;
}
}
}
//模式类定义
class MobP
{
///* 结构体协议制定单元 */
public Data_LAYER ComData = new Data_LAYER();
public struct Data_LAYER
{
public int sHead; //起始标志 1
public int sOstr; //功能编码 2
public int sAddr; //地址编码 3
public int sNum; //命令编号 4
public int sRes; //应答标志 5
public int sLen; //数据长度 6
public int sCrc; //校验单元 7
public int sEnd; //结束标志 8
}
public MobP()
{
ComData .sHead= 0x7E;
ComData.sOstr = 0x06;
ComData.sAddr = 0x00;
ComData.sNum = 0x00;
ComData.sRes = 0x00;
ComData.sLen = 0x00;
ComData.sCrc = 0x0000;
ComData.sEnd = 0x7F;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -