Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# display serialport
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 !
 

 
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 30-Dec-12 20:15pm
Comments
Sergey Alexandrovich Kryukov at 31-Dec-12 1:29am
   
What's the problem? Display the element you want...
—SA
ontheline89 at 31-Dec-12 1:31am
   
I dont know, what need to be change in above code to display that specific elements.
Sergey Alexandrovich Kryukov at 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 at 31-Dec-12 1:36am
   
or is that the value at the given index ie 19 & 29 want to display?
ontheline89 at 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.
jibesh at 31-Dec-12 1:40am
   
but can you make sure that you want only the 17 followed by B5 combination? and this value did not come any where in the middle?
ontheline89 at 31-Dec-12 1:43am
   
I dont need that combination. i just need to display elements at index 19 and 20 as values are changing in every minute at serial port.
ontheline89 at 31-Dec-12 1:41am
   
yes i want to display values at given indexes 19 and 20.
I dont know, where i need to change my code for it.
jibesh at 31-Dec-12 1:49am
   
Check my updated solution. I think i said the wrong index 19 and 20 including the space and other char it will start from 54. please double check the index with your own data.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
ontheline89 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Update your display method like below. get the substring from the msg
 
string substring = string.Empty;
if( message.Length > 54)
 substring = msg.Substring(54, 4); 
richTextBox1.AppendText(substring);
  Permalink  
Comments
ontheline89 at 31-Dec-12 1:53am
   
ok thank you so much for your solution.
I am trying your solution now.
Thanks once again.
jibesh at 31-Dec-12 1:54am
   
you welcome. good day to you.
ontheline89 at 31-Dec-12 1:56am
   
thanks
good day to you too.
ontheline89 at 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)

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100