In the serial port programming, often involves the conversion of the system, the calculation of the Protocol check code and data format operation, this article sorted out in the actual project may be used in public methods, to share to the needs of friends.
One, the following contains three parts of the content:
1, the transformation of the system:
Mainly includes the serial communication in the comparison of the commonly used 16, byte, compressed BCD code, long, ASCII, float and so on between the conversion method. ConvertHelper.cs Convert/*----------------------------------------------------------------
Copyright (C) BEIJING * * * * * * Technology Co., Ltd.
All rights reserved.
//
FileName: Converthelper
File Feature Description: Conversion
//
//
Create Identity: * * 2008-05-04
//
To modify an identity:
Modify Description:
//
To modify an identity:
Modify Description:
//---------------------------------------------------------------- */
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Namespace LY
{
<summary>
In-process conversion
</summary>
public static Class Converthelper
{
#region in-process conversion
<summary>
Shaping into bytes
</summary>
<param name= "I" ></param>
<returns></returns>
public static byte inttobyte (int i)
{
Return Convert.tobyte (i% 0x100);
}
<summary>
Convert 16 into BCD
</summary>
<param name= "B" > Byte </param>
<returns> return BCD Code </returns>
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 + * R2); 0x12-> 12
}
<summary>
Convert 16 into BCD
</summary>
<param name= "i" > int </param>
<returns> return BCD Code </returns>
public static byte hextobcd (int i)
{
Return HEXTOBCD (Inttobyte (i));
}
<summary>
Double Convert to compressed BCD
</summary>
<param name= double-precision floating-point number in "D" > 0-100 </param>
<returns> return to compressed BCD code </returns>
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);
}
<summary>
Long converts to BCD
</summary>
<param name= "Val" ></param>
<returns></returns>
public static long Tobcd (long Val)
{
Long res = 0;
int bit = 0;
while (Val >= 10)
{
Res |= (val% << bit);
Val/= 10;
Bit + 4;
}
Res |= val << bit;
return res;
}
<summary>
BCD converted to Long
</summary>
<param name= "Vals" ></param>
<returns></returns>
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;
}
<summary>
BCD translates into 16-in-system
</summary>
<param name= "B" ></param>
<returns></returns>
public static byte Bcdtohex (byte b)
{
int r2 = b% 100; b =-> r2 =//123-> r2 = 23
int r1 = r2% 10; r2 =-> R1 = 2
r2 = R2/10;