Can I convert the Data Received from serial port to only number without spaces and symbol?

StackOverflow https://stackoverflow.com/questions/22379788

  •  14-06-2023
  •  | 
  •  

Question

Data received output http://postimg.org/image/5hvqgosc7/

This is the string display of RFID tag number. Is it really this one shows up for all rfid or can I change it to display numbers only without spaces and symbols?

or is there a problem in my code.

This is my code:

 private void Form1_Load(object sender, EventArgs e)
    {
        cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Thesis\Desktop\Projects\Employees.accdb;Persist Security Info=True";
        cmd.Connection = cn;
        this.myDelegate = new AddDataDelegate(AddDataMethod);
        rfidbox.Enabled = true;

        string[] ports = SerialPort.GetPortNames();
        foreach (string port in ports)
        {
            SerialPort mySerialPort = new SerialPort(port);
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.ReadTimeout = 3000;
            mySerialPort.Handshake = Handshake.None;

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();
        }



    }

    private void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string s = sp.ReadExisting();

        rfidbox.Invoke(this.myDelegate, new Object[]{s});

    }

    public void AddDataMethod(String myString)
    {
        rfidbox.AppendText(myString);


    }

Thanks for help in advance.

Was it helpful?

Solution

I would assume that your data has a binary part after the few bytes whith ASCII values. You probably need to consult the documentation of your hardware for the structure of the data. If parts are binary you need to use binary read routines like SerialPort.ReadByte() n times and assemble a string manually from bytes. The bytes after the string need to be interpreted according to the meaning; watch for potential endianness issues with binary integers, if any are contained in the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top