1

I'm using serialport in c# and my code is like this:

FileStream MyFile = new FileStream(strFileDestination, FileMode.Append);
BinaryWriter bwFile = new BinaryWriter(MyFile);
bwFile.Write(serialPort1.ReadExisting());
bwFile.Close();
MyFile.Close();

when I use

bwFile.Write(serialPort1.ReadByte());

instead of

bwFile.Write(serialPort1.ReadExisting());

, writing speed in file decreases from about 130 KB/s to 28 KB/s and when I use

bwFile.Write((byte)serialPort1.ReadByte());

, writing speed decreases to 7 KB/s. I want to know how Can I write in to file like the third command and have the speed 130 KB/s.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Mohammad
  • 87
  • 1
  • 9
  • maybe do `serialPort1.ReadByte()` in a loop? – M.kazem Akhgary Aug 16 '15 at 14:54
  • Yes. I use this command in a loop. the loop is like this: while Logger==true{/*the code I posted*/} and Logger is a variable that let my program to write in a file. by pressing a button logger changes to false – Mohammad Aug 16 '15 at 15:04

1 Answers1

-1

Have you considered simply using a stream to write the data? I don't think you are actually using the extra features that BinaryWriter offers over Stream.Write.

Simply call the CopyTo() method

Stream destination = new FileStream(...)
MyFile.CopyTo(destination);

Which under the hood is calling the following code:

byte[] buffer = new byte[bufferSize];
int read;
while ((read = serialPort1.Read(buffer, 0, buffer.Length)) != 0)
{
    destination.Write(buffer, 0, read);
}

Have a look at this post for further information: https://stackoverflow.com/a/411605/283787

Community
  • 1
  • 1
openshac
  • 4,966
  • 5
  • 46
  • 77
  • Thank you. I have tried using simply Filestream. but the data in the file is not received completely. I mean somewhere in the file, some data are not the real data that the sender sends to my laptop. by the way, my laptop doesn't have any serial port and the device which sends to my laptop, is using a usb cable and I'm using virtual COM. – Mohammad Aug 18 '15 at 13:34
  • Thank you. I have tried using simple Filestream. but the data in the file is not received completely. I mean somewhere in the file, some data are not the real data that the sender sends to my laptop. by the way, my laptop doesn't have any serial port and the device which sends to my laptop, is using a usb cable and using virtual COM. anyway I couldn't use the code you write above. I got compile error Can you explain the code more please. (My English isn't well. sorry if I had mistake in sentences. and I didn't understand meaning of sentence"Which under the hood is calling the following code") – Mohammad Aug 18 '15 at 13:40
  • What is the compile error you get? For a full explanation have a look at the link I posted, there are more details there. – openshac Aug 18 '15 at 13:45
  • It worked. the problem was "Read" word. when I used SerialPort1.Read(buffer, 0, buffer.length) it worked. (which SerialPort1 is the port name I defined in the code.) Thanks. – Mohammad Aug 24 '15 at 12:32
  • That's great to hear. If you could mark the answer as the solution that would be great. Thanks – openshac Aug 24 '15 at 15:39