How do I send a file via the serial port in C#?

Posted on 2011-05-09
Medium Priority
Last Modified: 2012-05-11
I am trying to read a file and send it via the serial port.  I can send a few bytes, but not a lot.  

It seems to send really quickly (about a second) compared to using a terminal program (about a minute).  and the device at the other end does not receive it properly.

I have connected a Tx Rx loopback adapter to the port, and it does loop back a small text file (so it does basically work), but for a large one only a line of  bytes get through.  I am worried that the send data is overflowing the port buffer, and that some data is getting lost.

There is no hardware or software protocol being used as flow control (in hyperterminal I would select none for flow control).

FileStream inStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(inStream);
int nBytesToRead = (int)inStream.Length;
sp.Write(br.ReadBytes(nBytesToRead), 0, nBytesToRead);
textBox.AppendText("\n[ Sending " + nBytesToRead.ToString() + " bytes. ]\n");

Open in new window

should I be sending smaller numbers of bytes at a time?  if so how would I do that?  or do I need to clear the buffer or something?
Question by:dazzler77
  • 2
LVL 11

Expert Comment

ID: 35720431
Hi, you should be able to get the buffer size by:
int bufferSize = sp.WriteBufferSize;

So in that case try doing:

           int totalTransfered = 0;

            while (totalTransfered < nBytesToRead)
                int numOfByteToTransfer = Math.Min(bufferSize, nBytesToRead - totalTransfered);
                sp.Write(br.ReadBytes(numOfByteToTransfer), 0, numOfByteToTransfer);
LVL 11

Accepted Solution

saragani earned 1000 total points
ID: 35720450

 int totalTransfered = 0;

            while (totalTransfered < nBytesToRead)
                int numOfByteToTransfer = Math.Min(bufferSize, nBytesToRead - totalTransfered);
                sp.Write(br.ReadBytes(numOfByteToTransfer), 0, numOfByteToTransfer);
              totalTransfered  += numOfByteToTransfer;

We really don't wanna have an infinite loop :-)

Author Closing Comment

ID: 35725448
Thank you this seemed to fix the sending problem.  At least lots more  bytes are getting through now.  I think this is the solution.  Now a lot more of the file is being sent correctly.  IT still doesn't work 100% but I think that is because I have display issues ...
Think I need to send via a separate thread so I can update the display at the same time while I am waiting, as there is data being received as confirmation of sending the file.

Featured Post

[Video] Oticon Case Study

Open office environments can create the dynamics for innovation, but they also bring some challenges. With over 1,000 employees in an open office, Oticon needed a solution that would preserve the environment while mitigating disruptive background noises.

Watch how they did it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question