我有一个c#代码,它将十六进制代码写入设备的端口。设备回复了端口,我读了他的回答,我将它转换为十六进制并在文本框中显示它。 所以,现在我必须检查答案中的每一位,并在文本框中显示一条消息或类似的东西。
有人可以帮帮我吗?
提前谢谢。
答案 0 :(得分:2)
按位和您想要检查的位的回复:
Byte response = (Byte)0xFF;
Boolean isBitOneSet = (response & 0x1) > 0;
Boolean isBitTwoSet = (response & 0x2) > 0;
Boolean isBitThreeSet = (response & 0x4) > 0;
或使用右移:
Boolean isBitFiveSet = ((response >> 4) & 0x1) > 0;
if(isBitFiveSet) System.Windows.MessageBox.Show("Five is set");
答案 1 :(得分:1)
如果需要数字的二进制表示,可以使用Convert.ToString()方法 - 它比分别检查每个位更简单有效。这种方法可以转换为二进制和十六进制格式 - 无论你喜欢什么。
答案 2 :(得分:0)
namespace read_display_data
{ 公共部分类Form1:表格 {
SerialPort port = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
port.Open();
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void Sendbutton_Click(object sender, EventArgs e)
{
port.Write(new byte[] { 0x55, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x5D }, 0, 8)
byte[] buffer1 = new byte[] { 0x55, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0x5D };
Log1(LogMsgType.Outgoing, DateTime.Now + " -- " + ByteArrayToHexString(buffer1) + "\n");
}
public enum LogMsgType { Incoming, Outgoing, Normal };
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytes = port.BytesToRead;
byte[] buffer = new byte[bytes];
port.Read(buffer, 0, bytes);
Thread.Sleep(100);
Log(LogMsgType.Incoming, DateTime.Now + " -- " + ByteArrayToHexString(buffer) + "\n");
Log2(LogMsgType.Incoming, DateTime.Now + " -- " + GetInBinaryString(buffer) + "\n");
}
private string ByteArrayToHexString(byte[] buffer)
{
StringBuilder sb = new StringBuilder(buffer.Length * 3);
foreach (byte b in buffer)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
private string GetInBinaryString(byte[] buffer)
{
StringBuilder sb = new StringBuilder(buffer.Length * 8);
foreach (byte b in buffer)
sb.Append(Convert.ToString(b, 2).PadLeft(8, '0').PadRight(5, ' '));
return sb.ToString().ToUpper();
}
private void Log(LogMsgType msgtype, string msg)
{
textBox1.Invoke(new EventHandler(delegate
{
textBox1.SelectedText = string.Empty;
textBox1.AppendText(msg);
textBox1.ScrollToCaret();
}
));
}
private void Log1(LogMsgType msgtype, string msg)
{
textBox2.Invoke(new EventHandler(delegate
{
textBox2.SelectedText = string.Empty;
textBox2.AppendText(msg);
textBox2.ScrollToCaret();
}
));
}
private void Log2(LogMsgType msgtype, string msg)
{
textBox3.Invoke(new EventHandler(delegate
{
textBox3.SelectedText = string.Empty;
textBox3.AppendText(msg);
textBox3.ScrollToCaret();
}
));
}
答案 3 :(得分:0)
费迪南德已经描述了你可以检查个别位的方式。也许这还不够清楚。
因此,要检查每个字节中的每一位,您应该将代码白化为:
StringBuilder message = new StringBuilder();
for (int byteNum = 0; byteNum < bytes.Length; byteNum++)
{
var responseByte = bytes[byteNum];
for (int bitPos = 0; bitPos < 8; bitPos++)
{
message.AppendFormat("Byte {0} bit {1} value is {2}", byteNum, bitPos, (responseByte & (1<<bitPos)) != 0);
}
}
希望现在更清楚了。你晚上唯一想改变的是字节和位顺序。我不确定比特序列如何转换为字节数组:第0位真的可能是字节中的最后一个。