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.
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. 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.