Receiving messages part by part from serial port using c#

大憨熊 提交于 2019-12-31 04:59:06

问题


I am using the below code to receive the messages from serial port using c#

    void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (comPort.IsOpen == true)
        {
            string msg = comPort.ReadExisting();
            MessageBox.Show(msg.Trim());
        }
    }

The problem is, i am getting the messages part by part. Like, if u send "Hello, How are you" I am receiving it word by word. I want that in a single stretch. How can i do ??

Also, is it possible to retrieve the port name from which the application is sending and receiving messages ?


回答1:


By far the easiest way to transmit a fixed amount of data over a serial port in one shot is to prepend the data with a length header. Use a fixed number of bytes for the length (2-4 should suffice) and then read length bytes of data synchronously using the Read method. You can do this asynchronously too, of course, you just need to save the length in a member field somewhere and perform your post-processing as soon as the length is hit.

The next-best way is to append a stop character or word. If it happens to be a newline, then you can use the synchronous SerialPort.ReadLine method. Otherwise, collect the received data in a StringBuilder, keep appending to the StringBuilder until you hit the stop character, then return the contents of that StringBuilder up to (but excluding) the stop character.

If you do not have control over the source data, need to read a fixed amount of data, and don't know what that size is in advance, then your job is going to be considerably more difficult. I think the only way of really dealing with this situation is to wait for a predetermined timeout, at which point you assume that the transmission is over if you haven't received any new data. It's not particularly reliable, which is why most communication protocols use one of the aforementioned designs.

If you think about the way a serial port (or any port) works, by simply pushing bits over a wire, then it should be clear that there's no way to know whether or not a transmission has completed without being able to predict the future. If you're connected to a remote host, i.e. over a modem, and that host disconnects you when the transmission is finished, then you might be able to use the Carrier Detect, but that doesn't sound like the case here.

Hopefully this is not the scenario you're currently in, and either you have some ability to control the source data or the device you're connecting to already uses a length header and/or stop pattern. If it does, definitely use that. Otherwise, you'll probably be stuck using timeouts.




回答2:


   private void MonitorSP_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                System.IO.Ports.SerialPort SP = (System.IO.Ports.SerialPort)sender;

                //Get the ports available in system
                string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();
                string strAvlPortNames = "";
                foreach (string s in theSerialPortNames)
                {
                    strAvlPortNames += s.ToString() + ", ";
                }

                //Read an contruct the message
                Thread.Sleep(1000);
                string msg = SP.ReadExisting();
                string ConstructedMsg = "Port's Found : " + strAvlPortNames + "\n" + "Port Used : " + SP.PortName + "\n" + "Message Received : " + msg;

                if (InvokeRequired)
                {
                    richTextBox1.Invoke(new MethodInvoker(delegate { richTextBox1.Text = ConstructedMsg; }));
                    //Send acknowlegement to sender port
                    SP.Write(SP.PortName);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace.ToString());
            }
        }  



回答3:


Try reading until you hit a CRLF.



来源:https://stackoverflow.com/questions/2604932/receiving-messages-part-by-part-from-serial-port-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!