How to receive Hexadecimal information via serial in C#

Asked

Viewed 153 times

1

Better starting an application that receives via would be the data of an interface in Hexadecimal, however, when the data arrive, they are not shown in a format Hexa, it seems to me that they are being converted to ASCII . For example: the data is sent in the format by the interface 0B 01 10 2D 0D DC F2 and the application is inserted in the listbox -?? .

Is there any way to filter this and show the data in the listbox as the interface sent?

Below is the part of the code that is responsible for receiving the data.

//************** DELAGATE DA THREAD *********************// public void serialCom_DataReceived(Object Sender, Serialdeceiventargs and)
{ this. Invoke(new Eventhandler(this.Recepcaoserial)); }

    //************** RECEPÇÃO DE DADOS *********************//
    public void RecepcaoSerial(object s, EventArgs e)
    {

        Thread.Sleep(800);
        bufferRx += serialCom.ReadExisting();

        byte[] rawBytes = System.Text.Encoding.ASCII.GetBytes(bufferRx);
        string str = BitConverter.ToString(rawBytes);
        str = BitConverter.ToString(rawBytes).Replace("-", " ");

        lstDadosEnviados.Items.Add(str);
        lstDadosEnviados.SelectedIndex = lstDadosEnviados.Items.Count - 1;
        lstDadosEnviados.SelectedIndex = -1;

        bufferRx = null;

    }
  • From personal experience: Dealing with hexadecimal in C# is a pain in the ass, always a little problem or other. If possible, I recommend converting to decimal or use string.

  • Good morning Francisco. It’s really complicated, rsr. But there has to be some way to do it...

  • Francisco, the data leaves the correct interface "0B 01 10 2D 0D DC F2" but when I put an acesspoint in the line of the bufferRx variable, I get, v u0001 u0010- r??.

  • Hexadecicmal is very quiet in C#, one of the simplest to do. The question is how you receive the information, are bytes, what is normal, I find it strange to be different from this, or if it is a text that in this case form hexadecimals. Show us how the information arrives. If a text arrives 0B 01 there’s no way to change this to bytes, it’s a horrible thing to do.

  • Good morning bigown, the data comes out of the interface in hexa even, 0B 01 10 2D 0D DC F2, but when I receive this data in the variable, bufferRx, they are presented like this, v u0001 u0010- r??

  • Dear friends, after breaking my head a little, I was able to receive the data in the correct way. I changed the code I posted earlier to the code with the solution. I thank you all for your help.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.