Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have developed an application to read data from SerialPort in C#. Its working fine. But the data read from SerialPort is different than I expected.

When I read the port using XCTU or Matlab it will gives the data like the following,
XML
00 00 e2 00 40 74 95 07 02 25 14 00 8a 92 00 77 ff

But When I am reading the same data from C# application it gives,
XML
8C3F275A483F

I am expecting the result data of C# application like the result from Matlab and XCTU. I have tried with SerialPort encodings(ASCIIEncoding, Unicode, Latin). But nothing works. Help me. this is my code,
C#
//DataReceived event handler
public event EventHandler<SerialDataEventArgs> NewSerialDataRecieved;
//Serial Port Initialization
SerialPort _serialPort = new SerialPort("COM3",9600,Parity.None,8,StopBits.One);
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_serialPort.Open();

//DataReceived event
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    int dataLength = _serialPort.BytesToRead;
    byte[] data = new byte[dataLength];
    int nbrDataRead = _serialPort.Read(data, 0, dataLength);
    if (nbrDataRead == 0)
        return;
    
    if (NewSerialDataRecieved != null)
        NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}

//class SerialDataEventArgs
public class SerialDataEventArgs : EventArgs
{
    public byte[] Data;
    public SerialDataEventArgs(byte[] dataInByteArray)
    {
        Data = dataInByteArray;
    }
    
}

//printing the read data
string str = BitConverter.ToString(e.Data);
txtData.AppendText(str);//txtData is a TextBox
Posted
Updated 3-Jul-17 9:31am
v5
Comments
[no name] 27-Oct-15 5:50am    
Code?
VR Karthikeyan 27-Oct-15 6:02am    
@pwasser Hi, I have added my code in the question
Alan N 27-Oct-15 6:15am    
Is that code correct? The byte array 'data' read from the port is assigned to a string in SerialDataEventArgs.
VR Karthikeyan 27-Oct-15 6:18am    
Sorry I forgot to mention that, am using BitConverter to convert the data(byte array) to string. Edited in question.
Alan N 27-Oct-15 6:44am    
... and that implies that the SerialDataEventArgs class definition cannot be correct. Please review all of the code posted.

Quote:
I am expecting the result data of C# application like the result from Matlab and XCTU. I have tried with SerialPort encodings(ASCIIEncoding, Unicode, Latin). But nothing works. Help me. this is my code,
The trick is simple, you receive binary data from serial port, don't convert it.
Your data contain some bytes that do not convert in any coding scheme, they are just binary values (00 07 02 14), all depend on the device that send the data. If not device specific, the only possibility is that the data contain some floating point value sent in its binary form.

So until you have clue of what is the data, handle it as a series of bytes, and to display the data, just display each byte as an hexadecimal value.
 
Share this answer
 
Comments
Member 12683722 12-Aug-16 22:01pm    
Hi ppolymorphe, I am a beginner in programming.
I have same problem with VR Karthikeyan.

I have not been success to received binary data from serial then convert to hexadecimal.
Could you help me? What code is should I use to??
Or complete source code maybe.?

Thank you
Patrice T 12-Aug-16 22:44pm    
Open a new question with information on your problem.
This part of VR Karthikeyan's code:
C#
//printing the read data
string str = BitConverter.ToString(e.Data);
txtData.AppendText(str);//txtData is a TextBox
To get the desirable output 00 00 e2 00 ......, change the above block to use string formatter "X" like this:
C#
foreach (byte i in e.Data)
{
    // i is byte value, two hexadecimal digit is enough
    // to hold its value (max 255, hex FF). For illustration
    // space is appended after each byte printout. 
    string str2 = i.ToString("X2").Append(" ");
    txtData.AppendText(str2);
}
Yet this executes slowly due to appending text to txtData which is a textBox. When byte[] is large the speed difference shows. In that case use this technique to improve perceived performance, mainly by moving the .AppendText() operation outside of the loop:
C#
using System.Text;

StringBuilder s = new StringBuilder();
foreach (byte i in e.Data)
{
    s.Append(i.ToString("X2")).Append(" ");
}
txtData.AppendText(s.ToString());
The technique employed StringBuilder to collect conversion results of all the bytes in the array e.Data. When the foreach loop finishes, txtData is appended in one go. To wrap this up, VR Karthikeyan's code can be written like this:
C#
//-----------------------------------------------------------------
// StringBuilder class use this name space to import
//-----------------------------------------------------------------
using System.Text;

//-----------------------------------------------------------------
// The code snapshot, portion to print out the byte array fast
//-----------------------------------------------------------------
txtData.AppendText(OurOwnConverter(e.Data)); //txtData is a TextBox

//-----------------------------------------------------------------
// A formatter function is used to wrap up the converstion
//-----------------------------------------------------------------
string OurOwnConverter(byte[] byteArray)
{
    StringBuilder s = new StringBuilder();
    foreach (byte i in byteArray)
    {
        s.Append(i.ToString("X2")).Append(" ");
    }
    return s.ToString();
}
 
Share this answer
 
v2

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