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

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    7

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

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    156

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    I have tried that and getting this error

    System.FormatException: 'Input string was not in a correct format.'



    Quote Originally Posted by OldGuy2 View Post

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    Ok so now with a little bit of research apparently the data coming out of the ALDL cable is in bytes

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    public string ConvertBytes(Byte[] data)
    {
       string result = "";
       foreach (var datum in data)
       {
          result += String.Format("{0:X2},", datum);
       }
       return result;
    }

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    how do i implement that into my existing code..... Sorry im still new to this

    here is the code im using

    Code:
    using System;
    using System.IO.Ports;
    using System.Windows.Forms;
    
    
    
    
    
    
    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();
    
    
    
    
                var datareceived = sport.ReadExisting();
    
    
                textBox1.AppendText("[" + dtn + "] " + "Received: " + hex + "\n");
    
    
                // Not Used !!!!!!!
                // txtReceive.AppendText("["+dtn+"] "+"Received: " + datareceived + "\n");
    
    
            }
    
    
            private void cmdConnect_Click(object sender, EventArgs e)
            {
                String port = txtPort.Text;
                int baudrate = Convert.ToInt32(cmbbaudrate.Text);
                Parity parity = (Parity)Enum.Parse(typeof(Parity), cmbparity.Text);
                int databits = Convert.ToInt32(cmbdatabits.Text);
                StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), cmbstopbits.Text);
                
                serialport_connect(port, baudrate, parity, databits, stopbits);
                progressBar1.Value = 100;
                toolStripProgressBar1.Value = 100;
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime dt = DateTime.Now;
                String dtn = dt.ToShortTimeString();
                String data = txtDatatoSend.Text;
                sport.Write(data);
                txtReceive.AppendText("[" + dtn + "] " + "Sent: " + data + "\n");
            }
    
    
            private void cmdClose_Click_1(object sender, EventArgs e)
            {
                DateTime dt = DateTime.Now;
                String dtn = dt.ToShortTimeString();
    
    
                if (sport.IsOpen) 
                {
                    sport.Close();
                    cmdClose.Enabled = false;
                    cmdConnect.Enabled = true;
                    txtReceive.AppendText("[" + dtn + "] " + "Disconnected\n");
                    progressBar1.Value = 0;
                    toolStripProgressBar1.Value = 0;
                }
            }
    
    
            private void cmbstopbits_SelectedIndexChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void cmbparity_SelectedIndexChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void cmbdatabits_SelectedIndexChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void label1_Click(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void label6_Click(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void label7_Click(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void toolStripProgressBar1_Click(object sender, EventArgs e)
            {
    
    
            }
    
    
      
    
    
         
    
           
    
    
            private void txtDatatoSend_TextChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
    
            }
    
    
            private void button5_Click(object sender, EventArgs e)
            {
                txtReceive.Clear();
            }
    
    
            private void txtReceive_TextChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void label7_Click_1(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
            {
           
    
    
            }
    
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
    
            private void hidePortSettingsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                cmbparity.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                cmbdatabits.Visible = false;
                cmbstopbits.Visible = false;
               showPortSettingsToolStripMenuItem.Checked = false;
               
            }
            // on click showPortSettings
            private void showPortSettingsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                cmbparity.Visible = true;
                label2.Visible = true;
                label3.Visible = true;
                label4.Visible = true;
                cmbdatabits.Visible = true;
                cmbstopbits.Visible = true;
             
                hidePortSettingsToolStripMenuItem.Checked = false;
            }
    
    
            private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
            {
             
            }
    
    
            private void informationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.ShowDialog();
            }
    
    
            private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
    
            }
    
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
    
            }
    
    
            private void pictureBox1_Click(object sender, EventArgs e)
            {
    
    
            }
        }
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could try

    Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(datareceived);
    string hex = ConvertBytes(bytes);

    datarecieved is the string that sport.ReadExisting() returned. That line makes the string into the type that ConvertBytes() likes, so then you would just pass it.

    I'm only assuming that ASCII is the encoding - please use the right encoding.

  8. #8
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    ok 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

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    byte[] dataRecevied = new byte[dataLength];
    int nbytes = sport.Read(dataRecevied, 0, dataLength);
    string hex = ConvertBytes(dataRecevied);
    Simple enough.

  10. #10
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    ok i have it working so far

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

  11. #11
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    now next thing i need to do is

    separate a spring


    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! what are the codes needed to display this output?
    By Gena Luterte in forum C Programming
    Replies: 2
    Last Post: 06-29-2015, 09:14 AM
  2. Display counts on textBox
    By drkidd22 in forum C# Programming
    Replies: 4
    Last Post: 02-12-2013, 04:04 PM
  3. Help needed with code to display something similar to DIR
    By CharlieCrawt in forum C Programming
    Replies: 19
    Last Post: 12-06-2011, 12:17 AM
  4. Assistance greatly needed to do LCD Display
    By HELPMEPLEASE!!! in forum C Programming
    Replies: 6
    Last Post: 03-28-2009, 05:07 PM
  5. Display Help Needed
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 11:10 PM

Tags for this Thread