简体   繁体   中英

ReadExisting serial port to bytes using C#

I am trying to use ReadExisting method under the serial port. the method return for me a string. however I want to convert this data into bytes.

the sender sends me bytes witout encoding. however when I am trying to use the ReadExisting method and convert it to bytes I am not getting the exact bytes. (closer but no all of them are translate it correctly.

I tried to use get bytes in Encoding (tried UTF8,ASCII and others) however didn't find the correct one. how can I know which encoding it does?

 private void _serialPort_DataReceived(object sender, 
 SerialDataReceivedEventArgs e)
    {
        int BytesToRead = _serialPort.BytesToRead;
        if (BytesToRead > 1)
        {

            string tmpExist = _serialPort.ReadExisting();
            SerialInfo _SerialInfo = new SerialInfo();

            byte[] tmpData = Encoding.ASCII.GetBytes(tmpExist); // 
 System.Text.Encoding.ASCII.GetBytes(tmpExist);
 }

Thanks

I believe the serial port .Encoding property will get you what you are after. It has been a few years, but I think that was it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM