Need to display Hexadecimal in a textbox

nick007

Member
Joined
Nov 11, 2017
Messages
8
Programming Experience
Beginner
This is the code im using and it displays text in a text box


private void sport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
DateTime dt = DateTime.Now;
String dtn = dt.ToShortTimeString();

txtReceive.AppendText("["+dtn+"] "+"Received: "+sport.ReadExisting()+"\n");
}

I need it to display Hexadecimal like below

F2,9E,1,0,79,0,0,0,B0,0,0,40,1,7,7,1,A4,3,1D,0,0,0,0,3,FF,1,
 
What exactly are you reading? ReadExisting retrieves a String but if you want to display hexadecimal output then you would want Bytes. You should probably look at the Read method that allows you to populate a Byte array. Once you have a Byte array, you can create a comma-delimited, hexadecimal String like so:
string.Join(",", myByteArray.Select(b => b.ToString("X")))
 
Im reading a Serial port

here is a bit more of my code

namespace SerialPort
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cmdClose.Enabled = false;
            foreach (String s in System.IO.Ports.SerialPort.GetPortNames()) 
            {
                txtPort.Items.Add(s);
            }
        }


        public System.IO.Ports.SerialPort sport;


        public void serialport_connect(String port, int baudrate , Parity parity, int databits, StopBits stopbits) 
        {
            DateTime dt = DateTime.Now;
            String dtn = dt.ToShortTimeString();


            sport = new System.IO.Ports.SerialPort(
            port, baudrate, parity, databits, stopbits);
            try
            {
                sport.Open();
                cmdClose.Enabled = true;
                cmdConnect.Enabled = false;
                txtReceive.AppendText("[" + dtn + "] " + "Connected\n");
                sport.DataReceived += new SerialDataReceivedEventHandler(sport_DataReceived);
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); }
        }

        private void sport_DataReceived(object sender, SerialDataReceivedEventArgs e) 
        {
            DateTime dt = DateTime.Now;
            String dtn = dt.ToShortTimeString();


            txtReceive.AppendText("["+dtn+"] "+"Received: "+sport.ReadExisting()+"\n");
        }
 
I know that you're reading FROM a SerialPort. You wouldn't have a DataReceived event handler for a SerialPort otherwise. What I was asking is what you are actually reading, i.e. what is the data? If it's not text then reading it as text is inappropriate to begin with. If it is text then you have two choices. You can either read the raw binary data as you would not non-text or you can read the text and then convert that to binary data. The latter is slightly easier because you can stick to calling ReadExisting but it's rather inefficient to convert the raw binary data to text first, which ReadExisting does, and then convert it back to binary data. Using Read avoids the double conversion but your code will need to be a bit more complex.
 
Im using an ALDL cable and reading data from a vehicle ODB port.

you can see in the image the data that is being displayed.

I am a newby to programming and not what im getting from the cable

capture.jpg
 
Im using an ALDL cable and reading data from a vehicle ODB port.

you can see in the image the data that is being displayed.

I am a newby to programming and not what im getting from the cable
If you don't know what data you're going to receive from the vehicle ODB port you can't program for it, so what does the vehicle documentation say it sends?
 
ok im using this code below

C#:
  private void sport_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            String dtn = dt.ToShortTimeString();


            int dataLength = sport.BytesToRead;
            byte[] dataRecevied = new byte[dataLength];
            int nbytes = sport.Read(dataRecevied, 0, dataLength);
            if (nbytes == 0) return;


            string data = BitConverter.ToString(dataRecevied);






            textBox1.AppendText("[" + dtn + "] " + "Received: " + data + "\n");


            // Not Used !!!!!!!
           // txtReceive.AppendText("[" + dtn + "] " + "Received: " + result + "\n");




        }


im getting the hex data i want but i have a dash between the numbers like this

2C-30-30-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-2C-32-44-0D-0A


how can i remove the dash and replace with a comma like this

F2,70,28,09,01,00,00,07,D4,01,14,6E,6E,6E,6E,6E,00,58,00,2D,2D,2D,2D,2D,2D,2D,2D,2D,2D
 
i Could not get yours to work

but i used this one which had the dashes between

string data = BitConverter.ToString(dataRecevied);

then updated to this one...


string data = BitConverter.ToString(dataRecevied).Replace("-", "");

seem to be working.. in a virtual port test..

tomorrow i will check it on the hardware and see how i go.

Thankyou so much for your help so far :)
 
Back
Top Bottom