send() trashing data

I am reading a file and writing the contents to a port using send(). The data is corrupted - missing data on the start and extra junk on the end. Here is my code, could someone tell me what I am doing wrong?

char buffer[16384];
long i, iRead;

send(socket, “This works!”, 10, 0);

for(i = 0; i < size; i += iRead)
{
iRead = fread(buffer, 1, sizeof(buffer)-16, fp);
if( iRead <= 0 ) break;
//buffer[iRead] = ‘\0’;
//printf(“%s”, buffer); // Test to see if data correct
send(socket, (const char *)buffer, iRead, 0);
}

P.S. The fread() is returning the correct data. Also this is NetOS7.4.

Thanks,
-Erik

You did not say what platform you doing your development work on, but there is an excellent “Serial Driver Sample” application available under the Connect Core 9P-9360 platform.

I would recommend looking at this for some guidance. This sample application demonstrates sending data from one serial port (TEST_COM0) to another serial port(TEST_COM1) of the NET+OS development board. The data is then stored in a file (data.c).

Let me know if this example doesn’t answer your questions.

Scott

Digi updated the forums to stop spammers from posting.

>>send(socket, “This works!”, 10, 0);
you’re only sending 10 bytes out of the 11 in the string, so you’re losing a byte you think you should have.

Try throwing ‘static’ in front of char buffer[16384], i.e.:

static char buffer[16384];

It could be your buffer is too large and is getting corrupted because of a stack overflow.

Other than those two things, I don’t see anything wrong with the snippet you sent. If the above doesn’t fix it, I’d have to see the actual code (or a simplified example that reproduces the problem).

Scott,
Thanks for your reply.
I posted to the ConnectME forum, I am using a ConnectME 9210. Serial ports aren’t an issue, I am working with a Ethernet port that is messing up.

-Erik

P.S. The tag used to work a long time ago. Digi must have “updated” the forum software and broken it.

Thanks for the reply.

I finally solved it - a rookie mistake. In a string before the code snippit I was sending a string, but stupidly put a sizeof() for the length. All the extra NULLs messed up the web browsers (both IE and mozilla). Switched to a strlen() and everything works.

-Erik