前一段时间给别人弄串口通讯程序时写的一个小小程序,今天整理硬盘时发现了。放在这里以方便以后查找.
public partial class Form1 : Form

{
public Form1()

{
InitializeComponent();
}


static char[] hexDigits =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

public static string ToHexString(byte[] bytes)

{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)

{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
public static string ToHexString(byte bytes)

{
char[] chars = new char[ 2];
for (int i = 0; i < 1; i++)

{
int b = bytes;
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}

private void button1_Click(object sender, EventArgs e)

{
string[] cs = textBox1.Text.Split(' ');
byte[] bs = new byte[cs.Length];
for (int i = 0; i < cs.Length; i++)

{
Console.Write("0x" + cs[i] + ",");
//bs[i] = Convert.ToByte("0x"+cs[i]);
//Console.WriteLine(Convert.ToByte(cs[i]));

}
}

private void button2_Click(object sender, EventArgs e)

{
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format( "|{0,-10}|",ToHexString(b[i])));
}
Console.WriteLine();
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format( "|{0,-10}|",b[i].ToString()));

}
Console.WriteLine();
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format( "|{0,-10}|",Convert.ToChar(b[i])).ToString());
}


}

byte[] b1 =
{ 0x02, 0x30, 0x35, 0x2F, 0x31, 0x31, 0x2F, 0x31, 0x31, 0x32, 0x31, 0x3A, 0x31, 0x31, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x34, 0x5A, 0x00, 0x7F, 0x7F, 0x00, 0x7F, 0x7F, 0x00, 0x7F, 0x7F, 0x00, 0x00, 0x20, 0x00, 0x6D, 0x02, 0x5A, 0x00, 0x31, 0x02, 0x1C, 0x03, 0x01, 0x01, 0x26, 0x01, 0x48, 0x00, 0x5E, 0x00, 0x15, 0x00, 0x00, 0x03, 0x53, 0x00, 0x10, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x1F, 0x02, 0x08, 0x00, 0x02, 0x00, 0x62, 0x00, 0x7D, 0x02, 0x51, 0x06, 0x4E, 0x20, 0x31, 0x30, 0x2E, 0x39, 0x20, 0x36, 0x2E, 0x30, 0x32, 0x20, 0x20, 0x31, 0x37, 0x37, 0x30, 0x2E, 0x35, 0x34, 0x30, 0x20, 0x38, 0x39, 0x2E, 0x37, 0x20, 0x32, 0x39, 0x2E, 0x34, 0x20, 0x20, 0x33, 0x32, 0x38, 0x20, 0x20, 0x32, 0x32, 0x32, 0x30, 0x2E, 0x31, 0x34, 0x39, 0x2D, 0x2E, 0x2D, 0x2D, 0x2D, 0x2D, 0x2E, 0x2D, 0x2D, 0x2D, 0x20, 0x20, 0x31, 0x2E, 0x36, 0x2D, 0x2D, 0x2D, 0x2E, 0x2D, 0x2D, 0x2D, 0x2D, 0x2E, 0x2D, 0x30, 0x2E, 0x31, 0x35, 0x39, 0x20, 0x35, 0x32, 0x2E, 0x30, 0x20, 0x31, 0x33, 0x2E, 0x30, 0x20, 0x20, 0x39, 0x2E, 0x38, 0x30, 0x2E, 0x32, 0x35, 0x33, 0x20, 0x37, 0x32, 0x2E, 0x31, 0x2D, 0x2D, 0x2D, 0x2E, 0x2D, 0x20, 0x2B, 0x2B, 0x2B, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54, 0x32, 0x54, 0x32, 0x20, 0x20, 0x54, 0x32, 0x54, 0x32, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x07, 0x0F, 0x12, 0x0E, 0x05, 0x01, 0x08, 0x20, 0x3C, 0x40, 0x2F, 0x19, 0x0D, 0x0A, 0x0C, 0x13, 0x1A, 0x26, 0x31, 0x38, 0x3D, 0x41, 0x48, 0x4C, 0x54, 0x5C, 0x64, 0x64, 0x5E, 0x5B, 0x55, 0x47, 0x3A, 0x31, 0x25, 0x1B, 0x15, 0x0E, 0x0A, 0x06, 0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x07, 0x0E, 0x1A, 0x2E, 0x47, 0x5C, 0x64, 0x5D, 0x4D, 0x3C, 0x30, 0x26, 0x1F, 0x18, 0x13, 0x0E, 0x0A, 0x08, 0x07, 0x06, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x11, 0x31, 0x51, 0x60, 0x64, 0x63, 0x58, 0x49, 0x3C, 0x2D, 0x25, 0x20, 0x18, 0x13, 0x11, 0x0D, 0x0C, 0x0E, 0x0C, 0x09, 0x07, 0x09, 0x0B, 0x0B, 0x0A, 0x0C, 0x0D, 0x0B, 0x09, 0x09, 0x0B, 0x0C, 0x0D, 0x0F, 0x11, 0x14, 0x19, 0x1E, 0x07, 0x0F, 0x0F, 0x31, 0x04, 0x31, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x04, 0x20, 0x43, 0x31, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x55, 0x00, 0x00, 0x2A, 0x03 };

byte[] b =
{ 0x02, 0x30, 0x35, 0x2F, 0x31, 0x31, 0x2F, 0x31, 0x31, 0x32, 0x32, 0x3A, 0x31, 0x34, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x5A, 0x00, 0x7F, 0x7F, 0x00, 0x7F, 0x7F, 0x00, 0x7F, 0x7F, 0x00, 0x00, 0x20, 0x00, 0x5C, 0x01, 0x07, 0x00, 0x0B, 0x01, 0x05, 0x03, 0x63, 0x01, 0x63, 0x01, 0x65, 0x00, 0x49, 0x00, 0x38, 0x00, 0x37, 0x02, 0x79, 0x00, 0x11, 0x00, 0x05, 0x00, 0x46, 0x00, 0x06, 0x01, 0x47, 0x00, 0x00, 0x00, 0x69, 0x01, 0x1B, 0x02, 0x3B, 0x06, 0x71, 0x20, 0x20, 0x39, 0x2E, 0x32, 0x20, 0x33, 0x2E, 0x39, 0x31, 0x20, 0x20, 0x31, 0x33, 0x39, 0x30, 0x2E, 0x33, 0x38, 0x39, 0x20, 0x39, 0x39, 0x2E, 0x35, 0x20, 0x33, 0x35, 0x2E, 0x35, 0x20, 0x20, 0x33, 0x35, 0x37, 0x20, 0x20, 0x32, 0x30, 0x31, 0x30, 0x2E, 0x31, 0x38, 0x34, 0x30, 0x2E, 0x30, 0x35, 0x35, 0x30, 0x2E, 0x37, 0x36, 0x31, 0x20, 0x20, 0x31, 0x2E, 0x37, 0x20, 0x20, 0x30, 0x2E, 0x35, 0x20, 0x20, 0x37, 0x2E, 0x30, 0x30, 0x2E, 0x31, 0x33, 0x34, 0x20, 0x34, 0x35, 0x2E, 0x35, 0x20, 0x31, 0x32, 0x2E, 0x38, 0x20, 0x31, 0x30, 0x2E, 0x35, 0x30, 0x2E, 0x32, 0x38, 0x33, 0x20, 0x36, 0x39, 0x2E, 0x39, 0x31, 0x37, 0x37, 0x2E, 0x37, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x04, 0x09, 0x0B, 0x07, 0x02, 0x00, 0x08, 0x28, 0x48, 0x49, 0x2C, 0x11, 0x07, 0x0A, 0x11, 0x13, 0x12, 0x12, 0x15, 0x18, 0x1B, 0x29, 0x3C, 0x4B, 0x54, 0x5E, 0x64, 0x60, 0x5A, 0x4F, 0x42, 0x35, 0x27, 0x1F, 0x17, 0x10, 0x0A, 0x07, 0x05, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x07, 0x0F, 0x1D, 0x33, 0x4C, 0x5E, 0x64, 0x5E, 0x4E, 0x3A, 0x29, 0x1D, 0x18, 0x13, 0x11, 0x0F, 0x0D, 0x0B, 0x08, 0x06, 0x05, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x06, 0x17, 0x35, 0x51, 0x62, 0x64, 0x59, 0x4C, 0x41, 0x35, 0x29, 0x1F, 0x1C, 0x16, 0x10, 0x0D, 0x0B, 0x08, 0x07, 0x06, 0x06, 0x06, 0x05, 0x05, 0x04, 0x04, 0x03, 0x03, 0x04, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x07, 0x0E, 0x13, 0x31, 0x06, 0x31, 0x01, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x04, 0x20, 0x43, 0x31, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x55, 0x00, 0x00, 0x44, 0x03 };
public void DrawLinesPointF(PaintEventArgs e)

{
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format("|{0,-10}|", ToHexString(b[i])));

}
Console.WriteLine();
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format("|{0,-10}|", b[i].ToString()));

}
Console.WriteLine();
for (int i = 0; i < b.Length; i++)

{
Console.Write(string.Format("|{0,-10}|", Convert.ToChar(b[i])).ToString());
}
// Create pen.
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
PointF[] points = new PointF[b.Length];
for(int i=0;i<b.Length-245;i++)

{ points[i] = new PointF(i*6,200- b[i+244]); };
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}

private void Form1_Paint(object sender, PaintEventArgs e)

{
// DrawLinesPointF(e);
}

private void button3_Click(object sender, EventArgs e)

{
serialPort1.Open();
}

private void Form1_Load(object sender, EventArgs e)

{
serialPort1.Close();
}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

{
//byte[] bs = new byte[2048];
//serialPort1.Read(bs, 0, 2048);
//Console.WriteLine(bs);
}

private void button4_Click(object sender, EventArgs e)

{

byte[] bs =
{ 0x6 };
serialPort1.Write(bs,0,1);
}

}