串口拆分数据

时间:2014-10-23 22:48:43

标签: c# serial-port

在我的代码中,我发送了ResumeLeft,但由于某种原因,文字会像这样拆分:

[COM6] Pause

[COM6] Re
[COM6] sume
[COM6] 

[COM6] Le
[COM6] ft

这是我的代码

    var mySerialPort = new SerialPort
                {
                    BaudRate = int.Parse(nsComboBox1.Items[nsComboBox1.SelectedIndex].ToString()),
                    PortName = nsComboBox2.Items[nsComboBox2.SelectedIndex].ToString(),
                    ReadTimeout = 500,
                    ReadBufferSize = 1024,
                    Parity = Parity.Space,
                    StopBits = StopBits.One
                };
                mySerialPort.Open();
                mySerialPort.DataReceived +=
                    (o, args) =>
                    {
                        string s = mySerialPort.ReadExisting();
                        textBox1.Invoke(
                            new MethodInvoker(() => textBox1.Text += string.Format("[{0}] {1}", mySerialPort.PortName,
                                s) + Environment.NewLine));
//removed the rest for being irrelevant
                    };

我认为这是因为我的选择,但我怎样才能避免拆分文本?

1 个答案:

答案 0 :(得分:1)

尝试使用mySerialPort.ReadLine();它将读取直到新的行值