Help Needed C# - Need to display Hexadecimal in a textbox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Help Needed C# - Need to display Hexadecimal in a textbox

  1. #1
    Join Date
    Nov 2017
    Posts
    6

    Help Needed C# - Need to display Hexadecimal in a textbox

    This is the code im using and it displays text in a text box

    Code:
    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,
    Last edited by 2kaud; November 12th, 2017 at 04:14 AM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,580

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    What format does the data arrive in?

  3. #3
    Join Date
    Nov 2017
    Posts
    6

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    Hi Arjay,
    Im really not sure

    How would i check this


    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

    Attachment 35041


    this is the code im using

    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");
            }
    Last edited by 2kaud; November 12th, 2017 at 04:15 AM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'. Cheers!

    PS. Issue with attachment has been reported]
    Last edited by 2kaud; November 12th, 2017 at 04:22 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  5. #5
    Join Date
    Nov 2017
    Posts
    6

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    Quote Originally Posted by Arjay View Post
    What format does the data arrive in?
    Ok so now with a little bit of research apparently the data coming out of the ALDL cable is in bytes

  6. #6
    Join Date
    Nov 2017
    Posts
    6

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    ok guys,

    im using this code below

    Code:
      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

  7. #7
    Join Date
    Nov 2017
    Posts
    6

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    Ok i have it all working now


  8. #8
    Join Date
    Nov 2017
    Posts
    6

    Re: Help Needed C# - Need to display Hexadecimal in a textbox

    now next thing i need to do is

    separate a spring

    F2,98,1,4,5,7E,79,37,C,0,0,0,0,1,0,0,0,0,0,4,6B,48,17,F4,0,C8,0,0,FF,C0,3C,0,5,3,0,0,0,5,DC,0,0,0,0, 0,1,50,79,7C,7E,7E,7E,4,0,60,80,78,5,40,80,C0,FF,5,5A,64,5A,5,4,1C,2


    I need to extract all the different colour sections and put them into variables so i can display them as textnow next thing i need to do is

    separate a spring

    F2,98,1,4,5,7E,79,37,C,0,0,0,0,1,0,0,0,0,0,4,6B,48,17,F4,0,C8,0,0,FF,C0,3C,0,5,3,0,0,0,5,DC,0,0,0,0, 0,1,50,79,7C,7E,7E,7E,4,0,60,80,78,5,40,80,C0,FF,5,5A,64,5A,5,4,1C,2


    I need to extract all the different colour sections and put them into variables so i can display them as text

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)