2

i study C# Serial. i write this code to receive Data. when i run this code and another device sent data only once, but the program receives the data twice or more than three times. how can i fix this problem?

There's still a lot I don't know. Please explain it easily. I spent a week because I couldn't solve this problem.... :(

 private void MainForm_Load(object sender, EventArgs e)//main form
        {
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(EventDataReceived);
            CheckForIllegalCrossThreadCalls = false;
                       ........
                       ........
                       ........
        }
                       ........
                       ........
                       ........

void EventDataReceived(object sender, SerialDataReceivedEventArgs e)//this is receiving data method
        {

            int size = serialPort1.BytesToRead;// assign size of receive data to 'int size'

            byte[] buff = new byte[size];// array who assign receiving data(size = int size)
            serialPort1.Read(buff, 0, size);//assign receive data to 'buff'

            string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
            for (int i = 0; i < size; i++)
            {
                tb_rx.Text = tb_rx.Text + " \r\n " + hexData;
                Thread.Sleep(1000);//When I first encountered the problem, I added it because I thought the interval was too short. But I don't think this is the solution.
            }
        }
BBBD
  • 43
  • 1
  • 6
  • 1
    what symptom are you seeing that makes you think you're seeing the data multiple times? also: what was the return value of `Read`? (you are usually meant to retain that info, and use it; although *in theory* we know `BytesToRead` bytes are available, that's not something I would usually use other than deciding between sync vs async IO) – Marc Gravell Dec 10 '21 at 07:56
  • 1
    `CheckForIllegalCrossThreadCalls = false;` - don't do that. Look into Control.Invoke. – JeffRSon Dec 10 '21 at 08:08
  • @MarcGravell another device send " 7E 7E 04 01 0F" once but i receive "7E 7E 04 01 0F\r\n7E 7E 04 01 0F\r\n7E 7E 04 01 0F" – BBBD Dec 10 '21 at 08:12
  • This might be a useful read: [If you must use System.IO.Ports.SerialPort](https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport) – JonasH Dec 10 '21 at 10:08
  • The following may be helpful: https://stackoverflow.com/questions/65957066/serial-to-usb-cable-from-a-scale-to-pc-some-values-are-just-question-marks/65971845#65971845 and https://stackoverflow.com/questions/69943909/unable-to-decode-serial-port-data/69946343#69946343 (this one is in VB.NET) – Tu deschizi eu inchid Dec 10 '21 at 11:16

1 Answers1

2

This code is fishy:

      string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
      for (int i = 0; i < size; i++)
      {
           tb_rx.Text = tb_rx.Text + " \r\n " + hexData;
           Thread.Sleep(1000);//When I first encountered the problem, I added it because I thought the interval was too short. But I don't think this is the solution.
       }

BitConverter.ToString(byte[]) already converts the byte array to a sequence of hex strings. In the for loop, you then add this hexData string to tb_rx (probably a textbox) for each byte received. So depending on the number of bytes you receive at once, you do get duplicate output. Just change that to:

string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
tb_rx.Text = tb_rx.Text + " \r\n " + hexData;

PMF
  • 14,535
  • 3
  • 23
  • 49