Search code examples
c#rfid

Need to know how to put a space between every two characters and a line between the text each time I scan


public partial class Form1 : Form
    {
        public delegate void AddDataDelegate(String myString);
        public AddDataDelegate myDelegate;
        string[] Tags;
        public Form1()
        {
            InitializeComponent();

            SerialPort RFID = new SerialPort();
            RFID.PortName = "COM5";
            RFID.BaudRate = 9600;
            RFID.DataBits = 8;
            RFID.Parity = Parity.None;
            RFID.StopBits = StopBits.One;
            RFID.Open();
            RFID.DataReceived += new        SerialDataReceivedEventHandler(RFID_DataReceived);
            this.myDelegate = new AddDataDelegate(AddDataMethod);
        }

        public void AddDataMethod(String myString)
        {
            textBox1.AppendText(myString);
        }
        public void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort RFID = (SerialPort)sender;
            int buffer = RFID.ReadBufferSize;
            byte[] data = new byte[buffer];
            RFID.Read(data, 0, buffer);
            string s;
            for (int i = 0; i < 18; i++)
            {
                s = Convert.ToString(data[i], 16);
                //Console.WriteLine(Convert.ToString(data[i], 16));
                textBox1.Invoke(this.myDelegate, new Object[] { s });
            }
            //Console.WriteLine(Convert.ToString(i,16));
            //textBox1.Invoke(this.myDelegate, new Object[] { s });
        }

The above prints the data to a text box. How can I create a space every two characters and leave a line between the data from each scan.

Current output after three scans:

00e203027313032180000000605641acff00000000000000e20302731301716606c200001b5ff00000000000000000e203027313000000000001716606c21b5ff000000000


Solution

  • You may append a space after each converted byte and append a new line (Environment.NewLine) after you've converted the entire buffer.

    Please also note that myDelegate should be called after the loop:

    public void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort RFID = (SerialPort)sender;
        int buffer = RFID.ReadBufferSize;
        byte[] data = new byte[buffer];
        RFID.Read(data, 0, buffer);
        string s = "";
        for (int i = 0; i < 18; i++)
        {
            s += Convert.ToString(data[i], 16) + " ";
        }
        s += Environment.NewLine;
        textBox1.Invoke(this.myDelegate, new Object[] { s });
    }
    

    Alternative solution:

    With a little help of LINQ you may format your output string in one go:

    public void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort RFID = (SerialPort)sender;
        int buffer = RFID.ReadBufferSize;
        byte[] data = new byte[buffer];
        RFID.Read(data, 0, buffer);
    
        string s =  string.Join(" ", data.Select(b => b.ToString("x2"))) + Environment.NewLine;
    
        textBox1.Invoke(this.myDelegate, new Object[] { s });
    }
    

    Where

    • data.Select(b => b.ToString("x2")) converts an array of bytes to an array of the bytes' string representations using x2 format.
    • string.Join(" ", ...) concatenates this array of strings onto one string with the elements delimited by a space
    • + Environment.NewLine finally ensures the next data portion will go to a new line.