帧头:#$%^
帧尾:^%$#
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string strTemp = MyComDevice.ComDevice.ReadTo("#$%^");
string strID = MyComDevice.ComDevice.ReadTo("^%$#");
//display the string
if (App.Current != null)
{
App.Current.Dispatcher.BeginInvoke((Action)delegate
{
try
{
DataRow row1 = MyDT.NewRow();
row1["ID"] = strID;
row1["Name"] = strTemp;
MyDT.Rows.Add(row1);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
});
}
}
catch (Exception)
{
}
}
private byte[] strToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
return returnBytes;
}