快捷搜索:

SerialPort 串口编程知识整理(二)

在进行串口编程时,常常会涉及到进制转换、谋略协议校验码以及数据款式化的操作,本文收拾了在实际项目中可能会用到的公共措施,分享给必要的同伙。

一、以下包孕了三部分的内容:

1、进制转换:

主要包括了串口通讯中对照常用的16进制、byte、压缩BCD码、long、ASCII、Float等之间的转换措施。

ConvertHelper.cs 进制转换

/*----------------------------------------------------------------

// Copyright (C) 北京奸淫*科技有限公司

// 版权所有。

//

// 文件名:ConvertHelper

// 文件功能描述:进制转换

//

//

// 创建标识:** 2008-05-04

//

// 改动标识:

// 改动描述:

//

// 改动标识:

// 改动描述:

//----------------------------------------------------------------*/

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace LY

{

///

/// 进制转换

///

public static class ConvertHelper

{

#region 进制转换

///

/// 整形转化成字节

///

///

///

public static byte IntToByte(int i)

{

return Convert.ToByte(i % 0x100);

}

///

/// 16进制转化成BCD

///

///

byte

/// 返回BCD码

public static byte HexToBCD(byte b)

{

int r1 = b % 0x10;// b = 0x12 -> r1 = 0x02

int r2 = b / 0x10;// b = 0x12 -> r2 = 0x01

if (r1 > 9)//r1 = 0x0A -> r1 = 0x00

r1 = 0;

if (r2 > 9)//r2 = 0x0A -> r2 = 0x00

r2 = 0;

return Convert.ToByte(r1 + 10 * r2);//0x12 -> 12

}

///

/// 16进制转化成BCD

///

///

int

/// 返回BCD码

public static byte HexToBCD(int i)

{

return HexToBCD(IntToByte(i));

}

///

/// Double转换为压缩BCD

///

///

0-100内的双精度浮点数字

/// 返回压缩BCD码

public static string DoubleToBCD(double d)

{

string[] strs = d.ToString("F2").Split('.');

string temp1 = int.Parse(strs[0]).ToString("D2");

string temp2 = int.Parse(strs[1]).ToString("D2");

return string.Format("{0} {1}", temp1, temp2);

}

///

/// long转换为BCD

///

///

///

public static long ToBCD(long val)

{

long res = 0;

int bit = 0;

while (val >= 10)

{

res |= (val % 10bit);

val /= 10;

bit += 4;

}

res |= valbit;

return res;

}

///

/// BCD转换为long

///

///

///

public static long FromBCD(long vals)

{

long c = 1;

long b = 0;

while (vals > 0)

{

b += ((vals & 0xf) * c);

c *= 10;

vals >>= 4;

}

return b;

}

///

/// BCD转化成16进制

///

///

///

public static byte BCDToHex(byte b)

{

int r2 = b % 100;// b = 12 -> r2 = 12//123 -> r2 = 23

int r1 = r2 % 10;//r2 = 12 -> r1 = 2

r2 = r2 / 10;//r2 = 12 -> r2 =1

return Convert.ToByte(r1 + 0x10 * r2);

}

///

/// BCD转化成16进制

///

///

///

public static byte BCDToHex(int i)

{

return BCDToHex(IntToByte(i));

}

///

/// btye转化成16进制字符

///

///

byte

///

public static string ToHexString(byte b)

{

return Convert.ToString(b, 16);

}

///

/// int转化成16进制字符

///

///

int

///

public static string ToHexString(int num)

{

return Convert.ToString(num, 16);

}

///

/// 16进制字符串转换成字节数组

///

///

///

public static byte[] HexStringToByteArray(string s)

{

s = s.WordStr(" ", "");

byte[] buffer = new byte[s.Length / 2];

for (int i = 0; is.Length; i += 2)

{

buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);

}

return buffer;

}

///

/// 字节数组转换成16进制字符串

///

///

///

public static string ByteArrayToHexString(byte[] data)

{

StringBuilder sb = new StringBuilder(data.Length * 3);

foreach (byte b in data)

{

sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));

}

return sb.ToString().Trim().ToUpper();

}

///

/// ASCII转16进制

///

///

ASCII

///

public static string ASCIIToHex(string ascii)

{

char[] cs = ascii.ToCharArray();

StringBuilder sb = new StringBuilder();

for (int i = 0; ics.Length; i++)

{

string hex = ((int)cs[i]).ToString("X");

sb.AppendFormat("{0} ", hex);

}

return sb.ToString().TrimEnd(' ');

}

///

/// HEX to ASCII

///

///

///

public static string HexToASCII(string hex)

{

if (hex.Length % 2 == 0)

{

int iValue;

byte[] bs;

string sValue = string.Empty;

string result = string.Empty;

int length = hex.Length / 2;

for (int i = 0; ilength; i++)

{

iValue = Convert.ToInt32(hex.Substring(i * 2, 2), 16); // 16进制->10进制

bs = System.BitConverter.GetBytes(iValue); //int->byte[]

sValue = System.Text.Encoding.ASCII.GetString(bs, 0, 1);//byte[]-> ASCII

result += char.Parse(sValue).ToString();

}

return result.PadLeft(length, '0');

}

return string.Empty;

}

///

/// ASCII To Float

///

///

///

public static float ASCIIToFloat(string ascii)

{

if (ascii.Length == 8)

{

byte[] arr = new byte[4];

for (int i = 0; i4; i++)

{

arr[i] = Convert.ToByte(ascii.Substring((3 - i) * 2, 2), 16);

}

float f = BitConverter.ToSingle(arr, 0);

return f;

}

return 0f;

}

///

/// Hex to Float

///

///

///

public static float HexToFloat(string hex)

{

string ascii = HexToASCII(hex);

float f = ASCIIToFloat(ascii);

return f;

}

#endregion

}

}

您可能还会对下面的文章感兴趣: