0

我需要将整个传入的串行端口数据保存到一个文件中。有人建议使用File.WriteAllBytes,但它需要创建字节数组的所有空索引。

        Array.Resize(ref Read_Data2, Read_Data2.Length + incoming.Length);

        public void SaveData(byte[] incoming)
        {
            for (int i = 0; i < incoming.Length; i++)
            {
                Read_Data2[x] = incoming[i];
                ++x;
            }


            File.WriteAllBytes("C:\\Test3.text", Read_Data2);
        }

我使用该方法将所有传入字节保存到 Read_Data2 但我认为有问题。正如我之前所说,它保存了字节数组的空索引。我该如何改善这一点,或者有没有更好的方法将传入的串行端口数据保存到文件中?

4

1 回答 1

-1
 private void mySerialPort_DataReceived(System.Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

{

                SerialPort spL = (SerialPort)sender;
                byte[] bytesToRec = new byte[spL.byteToRead];
                Int64 bytes = spL.Read(bytesToRec, 0, bytesToRec.Length);
                navSerialPort.DiscardInBuffer();
                navSerialPort.DiscardOutBuffer();
                string filepath = @"" + textBox1.Text + "\\" + "KMTU_" +DateTime.Now.ToString("MMM-d-yyyy") + ".hex";
                FileStream LogFile = new FileStream(filepath, FileMode.Create);
                LogFile.Write(bytesToRec, 0, bytesToRec.Length);
                 LogFile.Close();
}
于 2013-10-19T07:09:37.203 回答