串口字节到ASCII

时间:2018-11-12 14:00:47

标签: c#

我正在从c#上的串行端口接收字节,并且将它们存储在字节数组中,然后将它们制成字符串,所以现在我需要将字节转换为ASCII,我该怎么做?这是我的代码

void DataReceivedHandler(对象发送者,SerialDataReceivedEventArgs e)     {

    string bytestr = "";

    int numbytes = serialPort1.BytesToRead;
    byte[] rxbytearray = new byte[numbytes];



    for (int i = 0; i < numbytes; i++)
    {
        rxbytearray[i] = (byte)serialPort1.ReadByte();

    }

    string hexvalues = "";
    foreach (byte b in rxbytearray)
    {
        if (b != '\r')
            hexvalues = hexvalues + (b.ToString()) + " ";



    }       //  hexvalues = richTextBox1.Text;

    Thread.Sleep(500);

    MessageBox.Show(hexvalues);



}

2 个答案:

答案 0 :(得分:3)

Encoding.ASCII.GetString(byteArray);

答案 1 :(得分:0)

我会这样:

use std::cell::RefCell;

struct Wrapper {
    data: RefCell<struct_from_c_t>,
}

impl Wrapper {
    fn do_thing(&self) {
        unsafe { ffi_fn(&mut *self.data.borrow_mut()) }
    }
}