首先声明此文只适合我这等串口编程新手,也欢迎大神吐槽
参数转换问题:
/// <summary>
/// 数据转换
/// </summary>
/// <param name="values">要转换的byte数组</param>
/// <param name="factor">计算因子</param>
/// <param name="reh">高位字符</param>
/// <returns></returns>
private double ByteConvertDouble(byte[] values,int factor = 0, bool reh = false)
{
string hexStr = string.Empty;
for (int i = 0; i < values.Length; i++)
{
hexStr += GetHexStr(values[i]);
}
if (reh)
hexStr = hexStr.Substring(1);
double temp = Convert.ToInt32(hexStr, 16) * Math.Pow(0.1, factor);
if (reh)
temp *= (-1);
return temp;
}
/// <summary>
/// byte 转十六进制字符
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
private string GetHexStr(byte b)
{
return (b / 16).ToString("X") + (b % 16).ToString("X");
}
取数据时要看其所占的位数
原文:http://www.cnblogs.com/mustangjs/p/4170335.html