OK, that created the file, but left file empty with 0 bytes.
Any other ideas, my brain hurts.
Also, JPEG is Big-Endian and my CentOS is Little-Endian, so I was trying to
see if reversing the char would correct, but have been unsuccessful so far.
Not sure if this is correct path to fix problem or if writing to disk is problem.
Open to any suggestions, thanks.
---------- Post updated at 11:09 AM ---------- Previous update was at 10:49 AM ----------
You can't mix fopen and write() calls. fopen belongs with fwrite, open belongs with write.
Your first code wasn't wrong, either. It looks like it should write one more byte onto the end of the file.
I think what's wrong is the fact that you're writing single bytes. Are you sure your packets are single bytes? That seems really odd! You might be throwing out 99% of the packet.
Of course you are. Anything chunk of data whatsoever can be represented as 8-bit bytes. But that doesn't mean you're getting only one byte!
You already know 2 different ways. The difference is that, instead of using them to write 1 byte, you use them to write more.
Do you actually know that? Do you know your packets are even all the same size? You shouldn't have to guess -- something, somewhere in your code must know the size of the packet you received, but you're either not keeping that result or not using it...
Unfortunately we can't see your code from here. Please post it.
I certainly hope it would affect the jpeg image size, right now they're only going to be a tiny fraction of the size they should be.
I don't have documentation that is the problem and I am forced to try and understand how to get this JPEG image correct.
Documentation does say image size is
0xNNMMZZYY - Image Size 32 bits
The data for example gives me:
NN=0
MM=0
ZZ=10
YY=64
I know how many data bytes total from the formula I calulated.
((zz - 1) * 259) + 256 + YY = DataSize for individual packets
Don't know what the formula means, but it works, so I tried to append
the bytes one by one, but got a fuzzy image and I know the camera works
because the older pascal code works and uses a Blockwrite function:
Blockwrite(MyFileHandle, byte, 1, BytesRead);
which writes byte by byte in a loop, similar to the way I first explained and tried to emulate.
You don't need to research or guess the number of bytes. The operating system is quite probably telling you how many bytes you got from the camera, but you're not using it, or using it incorrectly. The parts of the code you've shown us, so far, have nothing to with reading the data, just writing it. For that matter, it's run in a loop which you've never shown us, which might have things wrong in it too.
None of the information on your corrupted output file will help tell us what's wrong in code we've never seen.
In short: We can't fix your code because you didn't show it to us. Please post your code. All of it.
code is to long to put, but I think problem is with bitstream and way it constructs jpeg.
using fread to fwrite from on jpeg to another jpeg works, but
I am using read from serial device and using fwrite to jpeg file,
which seems to be causing the stream problem.
basics of main code to grab packets as they come in from serial:
void parse(unsigned char *buff, size_t ss){
// puts it into a buffer and sends each byte to fwrite
for(i=0;i < ss ; i++){
b = buff;
// case statement here
//when packet has data from jpeg send
BlockW(&b);
}
}
That's a bit more helpful. You've opened the device with O_NDELAY, this means read might sometimes return -1, telling you to try again later. When this happens your code will still write something, even though it shouldn't.
You have to use read() for handling a device file.
int fd = open(DEVICE, O_RDWR | O_NOCTTY |O_NDELAY);
int out=open("outfile", O_CREAT|O_WRONLY, 0660);
while(1)
{
ssize_t off=0;
ssize_t res = read(fd,buf, BUFSIZE);
// Ignore when read didn't get anything
if(res < 0)
continue;
else if(res == 0) // should your app expect EOF?
{
fprintf(stderr, "EOF?\n");
break;
}
fprintf(stderr, "Read %d bytes\n", (int)res);
// buf[res] = 0; why null-terminate what's not a string?
//parse(buf,res); we're not in pascal anymore
// We can probably write everything at once. when we don't,
// just write the bits that didn't get written yet.
while(off < res)
{
ssize_t w=write(outfd, buf+off, res-off);
if(w > 0)
off += w;
else
{
perror("write error");
break;
}
fprintf(stderr, "Wrote %d bytes\n", (int)w);
}
}
close(out);
close(fd);
In summary: Always check the return value of everything.