显示大于0x7f的字节

时间:2016-01-21 23:00:24

标签: c#-4.0

我想将我的程序连接到串口&显示从串行端口接收的数据。我成功地做到了,但我遇到了问题。

我想将窗口中显示的字符串转换为位流

我关注How could I get the bits from a string in c#?

&安培;我的问题是如何显示像0xAA这样的字节,它大于7f ascii字符的最大值

我正在使用C#语言开发一个程序,我想将它连接到一个串口&为了显示从串口接收到的数据,我成功地做到了,但我有一个问题,我想将窗口中显示的字符串转换为我遵循的位流如何从字符串中获取位C#?

&安培;我的问题是如何显示像0xAA这样的字节,它大于7f ASCII字符的最大值

public partial class mm_dig_ctrl : Form
{
    public mm_dig_ctrl()
    {
        InitializeComponent();
        if (!MySerP.IsOpen)
        {
            MySerP.Open();
            TB_Port_Status.Text = "Port is Opened";
        }
        else
        {
            TB_Port_Status.Text = "Port is Busy";
        }
        MySerP.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceivedHandler);

    }

    public delegate void AddDataDelegate(String rxstring);
    public AddDataDelegate myDelegate;
    private void mm_dig_ctrl_Load(object sender, EventArgs e)
    {

        this.myDelegate = new AddDataDelegate(AddDataMethod);
    }
    public void AddDataMethod(String myString)
    {

        TBRX_Read.AppendText(myString);
    }
    private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        String rxstring,to_show;
        rxstring = MySerP.ReadExisting();
        to_show  = GetBits(rxstring);
        TBRX_Read.Invoke(this.myDelegate, new object[] { to_show });
        //TBRX_Read.Text += rxstring;
    }

    public string GetBits(string input)
    {
        StringBuilder sb = new StringBuilder();
        foreach (byte b in input)
        {
            sb.Append(Convert.ToString(b, 16));
        }
        return sb.ToString();
    }

0 个答案:

没有答案