[英]Is there any way to directly save serial port incoming data into file?
I need to save whole incoming serial port data into a file. 我需要将整个传入的串行端口数据保存到文件中。 Some people suggest to use File.WriteAllBytes
but it takes the all empty indexes of created byte array. 有人建议使用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);
}
I use that method to save all incoming bytes into Read_Data2 but i think something is wrong. 我使用该方法将所有传入的字节保存到Read_Data2中,但我认为出了点问题。 It saves the empty indexes of byte array as i said before. 如我之前所说,它保存字节数组的空索引。 How can i improve this or is there any better way to save the incoming serial port data into file? 我该如何改善呢?还是有更好的方法将传入的串行端口数据保存到文件中?
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.