• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1261
  • Last Modified:

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

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");
inStream.Close();

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?
0
dazzler77
Asked:
dazzler77
  • 2
1 Solution
 
saraganiCommented:
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);
            }
0
 
saraganiCommented:
Sorry...

 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 :-)
0
 
dazzler77Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now