Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone, i am new to C#, i am getting Output :
7E 45 00 FF FF 00 01 0A 00 93 00 00 01 00 00 01 00 02 17 B5 70 20 7E

I am using following below Code. I need to display only 17 B5 elements. what should i do ? Thank you !



C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form

    {
        public Form1()
        {

            InitializeComponent();
            serialPort1.Open();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //configuring the serial port
            serialPort1.PortName = "COM15";
            serialPort1.BaudRate = 115200;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);


        }

        public enum MessageType { Incoming, Outgoing, Normal, Warning, Error };

            private byte[] HexToByte(string msg)
        {
            //remove any spaces from the string
            msg = msg.Replace(" ", "");
            //create a byte array the length of the
            //divided by 2 (Hex is 2 characters in length)
            byte[] comBuffer = new byte[msg.Length / 2];
            //loop through the length of the provided string
            for (int i = 0; i < msg.Length; i += 2)
                //convert each set of 2 characters to a byte
                //and add to the array
                comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);
            //return the array
            byte[] newbuffer = comBuffer;
            return comBuffer;
        }


        private string ByteToHex(byte[] comByte)
        {
            //create a new StringBuilder object
            StringBuilder builder = new StringBuilder(comByte.Length * 3);
            //loop through each byte in the array
            foreach (byte data in comByte)
                //convert the byte to a string and add to the stringbuilder
                builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
            //return the converted value
            return builder.ToString().ToUpper();
        }


        #region DisplayData

        private void DisplayData(MessageType type, string msg)
        {
           richTextBox1.Invoke(new EventHandler(delegate
            {
                richTextBox1.SelectedText = string.Empty;


                richTextBox1.AppendText(msg);


                richTextBox1.ScrollToCaret();


            }));
        }
        #endregion


        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            int bytes = serialPort1.BytesToRead;
            //create a byte array to hold the awaiting data
            byte[] comBuffer = new byte[bytes];
            //read the data and store it
            serialPort1.Read(comBuffer, 0, bytes);
            //display the data to the user
            DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");


        }
Posted
Comments
Sergey Alexandrovich Kryukov 31-Dec-12 1:29am    
What's the problem? Display the element you want...
—SA
ontheline89 31-Dec-12 1:31am    
I dont know, what need to be change in above code to display that specific elements.
Sergey Alexandrovich Kryukov 31-Dec-12 1:40am    
Then I don't know what you possibly may not know. If you display all elements, it means that you know how to display any of them...
—SA
Jibesh 31-Dec-12 1:36am    
or is that the value at the given index ie 19 & 29 want to display?
ontheline89 31-Dec-12 1:38am    
Right now i am getting this large output
7E 45 00 FF FF 00 01 0A 00 93 00 00 01 00 00 01 00 02 17 B5 70 20 7E

I just need to extract 17 and B5 from above output.

How do you know you need to display 17 B5.

just modify the code
DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");


and write that logic.
or express the logic i will help you
 
Share this answer
 
Comments
ontheline89 31-Dec-12 1:45am    
I just need to display values at Index 19 and 20. Whatever the values are at index 19 and 20.
Jibesh 31-Dec-12 1:50am    
If you have a questions from OP do not post it as a solution. use Comment to the question link to get more details about the problem. thanks
Update your display method like below. get the substring from the msg

C#
string substring = string.Empty;
if( message.Length > 54)
 substring = msg.Substring(54, 4); 
richTextBox1.AppendText(substring);
 
Share this answer
 
Comments
ontheline89 31-Dec-12 1:53am    
ok thank you so much for your solution.
I am trying your solution now.
Thanks once again.
Jibesh 31-Dec-12 1:54am    
you welcome. good day to you.
ontheline89 31-Dec-12 1:56am    
thanks
good day to you too.
ontheline89 31-Dec-12 2:02am    
Jibesh just one thing more,
Why you have used values 54 and 4 in your solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900