0

i have designed a program by C# to read the received data from a Machine by serial port. the received data is Hex i want to change it to AscII then Split it to different text box. when it comes to one textBox there is no problem but when i am trying to change it to Ascii or split it, the program stop working

i appreciate any support

the code in one text box works well. my problems are changing to ascii and splitting the received data to several text box

     public void serialPort1_DataReceived(object sender, 
      SerialDataReceivedEventArgs e)
    {
        SerialPort serialPort1 = (SerialPort)sender;
        int numbytes = serialPort1.BytesToRead;


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


        foreach (byte b in rxbytearray)
        {

            hexvalues = (hexvalues + b.ToString("X2")) + " ";

        }

        this.Invoke(new EventHandler(Showdata));
    }

    private void Showdata(object sender, EventArgs e)
    {
        tBoxDataIn.Text = hexvalues + "\r\n";
        char[] chars = hexvalues.ToCharArray();

        for (int i =2; i < 7; i++)
        {

            xy = xy + chars[i];

        }
        string Cpfdecimal = Convert.ToUInt64(xy, 16).ToString();
        textBox1.Text = Cpfdecimal;

    }

    public string ConvertHextoAscii()
    {
        char charValue;
        string stringValue = "";
        string[] hexValuesSplit = tBoxDataIn.Text.Split(' ');
        foreach (string hex in hexValuesSplit)
        {
            // Convert the number expressed in base-16 to an integer.
            int value = Convert.ToInt32(hex, 16);
            // Get the character corresponding to the integral value.
             stringValue = Char.ConvertFromUtf32(value);
            charValue = (char)value;
        }
        textBox1.Text = stringValue;

        return stringValue;
    }

the hex received data: 0D 53 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 45 0D 0D

i want to change it to Ascii and split it.

abd.ch
  • 1
  • 2
  • Stops working as in throws an exception? Whats your error? – DetectivePikachu Aug 08 '19 at 13:43
  • what does it do when it stops working? boiling coffee? ;) no seriously, please be more precise, any exceptions? – Mong Zhu Aug 08 '19 at 13:43
  • what is `xy` ? and where to you call the `ConvertHextoAscii` method? – Mong Zhu Aug 08 '19 at 13:47
  • when i am using the code to receive hex in one textBox it is received well without any problem. – abd.ch Aug 08 '19 at 13:48
  • it is stop working it means, it does not complete receiving data v – abd.ch Aug 08 '19 at 13:49
  • please note that utf 32 does not equal ascii. – B.Letz Aug 08 '19 at 13:54
  • in the hex data you receive is 0D 53 ... . Is 0 one character and D one character and so on?? – B.Letz Aug 08 '19 at 13:57
  • please also look at [this](https://stackoverflow.com/questions/5647328/c-sharp-convert-a-string-of-hex-values-to-hex) – B.Letz Aug 08 '19 at 14:01
  • i have tried so much with this to change the data to ASCII and split them to several TextBox. i will check the link @B.Letz – abd.ch Aug 08 '19 at 14:57
  • *"the hex received data: 0D 53 30 30 ..."* -- You do realize that this data is ASCII code, right? Do you really want to treat this ASCII text as binary data? Maybe you should play with the C code from [this answer](https://stackoverflow.com/questions/57152937/canonical-mode-linux-serial-port/57155531#57155531) but add `tty.c_cc[VEOL] = 0x0d;` (i.e. specify carriage return as a line delimiter). – sawdust Aug 08 '19 at 22:35

0 Answers0