如何使用textbox通过serialport(rs232)/ rs422发送26字节十六进制值 [英] how to send 26 byte hex value using textbox through serialport(rs232)/rs422
本文介绍了如何使用textbox通过serialport(rs232)/ rs422发送26字节十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用textbox通过serialport发送26字节十六进制值(rs232)/ rs422
解决方案
从转换字符串的角度来看这个十六进制格式到byte []并将byte []转换为十六进制格式字符串,要求可选地使用十六进制字节用短划线分隔的格式:私人 静态 字符串 dash = - ;
private static byte [] ByteAryFromHexString( string hx)
{
// < span class =code-comment>有破折号?删除它们
if (hx.Contains(dash))hx = hx.Replace(dash, );
// 不必要?
hx = hx.ToUpperInvariant( );
byte [] result = new byte [hx.Length / 2 ];
for ( int i = 0 ; i < result.Length; i ++)
{
result [i] = Convert.ToByte(hx。子串(i * 2, 2 ), 16 );
}
返回结果;
}
// 'withDash参数控制破折号是否在结果中:默认情况下设置为'false
private static string HexStringFromByteArray( byte [] bytes, bool withDash = < span class =code-keyword> false )
{
var result = BitConverter.ToString(bytes);
if (withDash) return result;
return (result.Replace(dash, 跨度>));
}测试:
string hx1 = @ AA-55-55-55-15-55-6A-AA-FF-FF-00-00-FF-FF-00-10-00-06 -08-8F-00-E2-A0-80-08-D3\" 跨度>;
string hx2 = @ AA55555515556AAAFFFF0000FFFF00100006088F00E2A08008D3\" 跨度>;
private void 测试()
{
// 在下一行设置断点
// 并单步执行代码
byte [] b1 = ByteAryFromHexString(hx1);
byte [] b2 = ByteAryFromHexString(hx2);
// b1和b2的值是否相同?
string enc1 = Encoding.UTF8.GetString(b1);
string enc2 = Encoding.UTF8.GetString(b2);
bool i1 = enc1 == enc2;
string s1 = HexStringFromByteArray(b1, true );
string s2 = HexStringFromByteArray(b2, false );
// s1和s2不应该相同!
bool i2 = s1 =!= s2;
}注意:十六进制格式字符串到字节[]转换是一个地方.NET并没有真正提供真正快速的内置转换。关于实现这一目标的最快方法存在很多争论:您可以在StackOverFlow上的线程上找到各种方法的广泛讨论和性能测量,如下所示:[ ^ ]。
how to send 26 byte hex value using textbox through serialport(rs232)/rs422
解决方案
Looking at this from the point of view of conversion of a string in hex format to byte[] and conversion of byte[] to hex format string with the requirement to optionally use a format where hex bytes are separated by dashes:private static string dash = "-"; private static byte[] ByteAryFromHexString(string hx) { // has dashes ? delete them if (hx.Contains(dash)) hx = hx.Replace(dash, ""); // unnecessary ? hx = hx.ToUpperInvariant(); byte[] result = new byte[hx.Length / 2]; for (int i = 0; i < result.Length; i++) { result[i] = Convert.ToByte(hx.Substring(i*2,2), 16); } return result; } // the 'withDash parameter controls whether dashes are in the result : it's set to 'false by default private static string HexStringFromByteArray(byte[] bytes, bool withDash = false) { var result = BitConverter.ToString(bytes); if (withDash) return result; return (result.Replace(dash, "")); }Test:
string hx1 = @"AA-55-55-55-15-55-6A-AA-FF-FF-00-00-FF-FF-00-10-00-06-08-8F-00-E2-A0-80-08-D3"; string hx2 = @"AA55555515556AAAFFFF0000FFFF00100006088F00E2A08008D3"; private void Test() { // set break-point on next line // and single-step through the code byte[] b1 = ByteAryFromHexString(hx1); byte[] b2 = ByteAryFromHexString(hx2); // do b1 and b2 have identical values ? string enc1 = Encoding.UTF8.GetString(b1); string enc2 = Encoding.UTF8.GetString(b2); bool i1 = enc1 == enc2; string s1 = HexStringFromByteArray(b1, true); string s2 = HexStringFromByteArray(b2, false); // s1 and s2 should not be the same ! bool i2 = s1 =!= s2; }Note: Hex format String to Byte[] conversion is one place .NET doesn't really offer a really fast built-in conversion. There is a great deal of debate about the fastest way to achieve that: you can find extensive discussions, and performance measurements, of various methods on threads on StackOverFlow like this one: [^].
这篇关于如何使用textbox通过serialport(rs232)/ rs422发送26字节十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文