1
votes

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

1

1 Answers

1
votes

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.