1

I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. Now I want to convert it to a hex string. If it's a hex string we can cut parts of the string and decode them seperately. how can i do this?

The following is the code:

using System;
using System.IO.Ports;
using System.Threading;
public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;
    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 1000;
        _serialPort.WriteTimeout = 1000;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }

    public static void Read()
    {
        while (_continue)
        {

            try
            {             


                string message = _serialPort.ReadLine();
                Console.WriteLine(message);

            catch (TimeoutException) { }
        }
    }

    public static string SetPortName(string defaultPortName)
    {
        string portName;

            portName = "COM6";

        return portName;
    }

    public static int SetPortBaudRate(int defaultPortBaudRate)
    {
        string baudRate;


        baudRate = "9600";

        return int.Parse(baudRate);
    }

    public static Parity SetPortParity(Parity defaultPortParity)
    {
        string parity;

        parity = "None";

        return (Parity)Enum.Parse(typeof(Parity), parity);
    }

    public static int SetPortDataBits(int defaultPortDataBits)
    {
        string dataBits;

        dataBits = "8";

        return int.Parse(dataBits);
    }

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        string stopBits;

        stopBits = "One";

        return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        string handshake;

        handshake = "None";

        return (Handshake)Enum.Parse(typeof(Handshake), handshake);
    }
}
4
  • why you reposted? you would have edited that post only. Show how you are receiving and how you want to convert it to?? Commented Jun 2, 2011 at 12:50
  • possible duplicate of C# binary data conversion to string Commented Jun 2, 2011 at 12:53
  • Just so you know. All data you receive is binary. This is the reason using code without understanding it is a bad idea. Commented Jun 2, 2011 at 13:04
  • when i use _SerialPort.ReadLine() and then console.write it I get a screen of random characters and symbols that dont make sense. What i want is take that binary data packets convert them to a hex string and then manipulate it. Commented Jun 2, 2011 at 13:09

2 Answers 2

3

Try this

    string Data = "123";
    string hex = "";
    foreach (char c in Data)
    {
        hex += String.Format("{0:x2}", (byte)c);
    }

hex contains string as you wanted

Sign up to request clarification or add additional context in comments.

Comments

0

Look at the BitConverter.ToString() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.