0

我有 ac# 代码,它将十六进制代码写入设备的端口。设备回复端口,我阅读了他的答案,将其转换为十六进制并显示在文本框中。所以,现在我必须检查答案中的每一位,并在文本框中显示一条消息或类似的东西。

有人可以帮我吗?

先感谢您。

4

4 回答 4

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");
于 2012-09-28T10:53:50.827 回答
1

如果您需要数字的二进制表示,则可以使用Convert.ToString()方法 - 它比单独检查每个位更简单有效。此方法可以转换为二进制和 HEX 格式 - 无论您喜欢什么。

于 2012-09-28T11:00:04.053 回答
0

Ferdinand 已经描述了检查单个位的方式。也许它还不够清楚。

因此,要检查每个字节中的每一位,您应该像这样将代码变白:

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 位真的可能是字节中的最后一位。

于 2012-10-01T12:56:09.447 回答
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();

        }
        ));


    }
于 2012-10-01T11:07:14.237 回答