簡體   English   中英

有什么方法可以直接將串行端口傳入的數據保存到文件中?

[英]Is there any way to directly save serial port incoming data into file?

我需要將整個傳入的串行端口數據保存到文件中。 有人建議使用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中,但我認為出了點問題。 如我之前所說,它保存字節數組的空索引。 我該如何改善呢?還是有更好的方法將傳入的串行端口數據保存到文件中?

 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM