[SOLVED] Arduino JPEG over Serial
in
Integration and Hardware
•
7 months ago
Hi all,
I'm desperate for some help here, it seems I've tried everything.
What I'm trying to achieve:
I have an SD card on arduino, with a photo saved on it, for testing.
I'm trying to send that JPEG file through Serial, to processing, and save on my PC.
Sounds easy, except I also use the Serial port for the sending/receiving of commands and values for other methods.
So, what I've done is told Arduino to first send the file size, then tell processing that file transmission will begin, and send the file.
Once sending is complete I then send "EOF" to processing.
In processing, I'm using the serialEvent method to capture data from serial.
In the method I have a series of boolean "switches" controlled by if statements that parse the incoming data.
If it recongises that a photo will be transmitted, it then switches to "data input mode", and receives the data.
Every byte is parsed and is flagged if it is an 'E'.
If so, I then switch again to check for 'O' and 'F'. (This is me recognising the end of file).
Once the EOF has been detected, I then flush and close the output.
The problem:
No matter what method of storing bytes I try, the JPEG is never correct PC side.
I've tried storing each byte at a time,
saving them in a byte array and calling output.write on the whole array or trying saveBytes on the array too.
Here's my code:
void serialEvent(Serial myPort)
{
// read a byte from the serial port:
// myPort.readBytesUntil(',', byteBuffer);
//String inString = new String(byteBuffer);
String inString = null;
byte byteBuffer = 0;
byte tempByte = -1, tempByte2 = -1, tempByte3 = -1;
if(eFound && oFound && !fFound) // if e and o found, look for f.
{
byteBuffer = (byte)myPort.read();
tempByte3 = byteBuffer;
println("3: " + tempByte3);
if(tempByte3 == 70)
{
fFound = true;
println("Found EOF");
}
else //else not found, store original values in proper order.
{
append(photo, (byte)69);
append(photo, (byte)79);
append(photo, tempByte3);
eFound = false;
oFound = false;
fFound = false;
}
}
if(eFound && !oFound && !fFound) // if eFound, look here for 'O'
{
byteBuffer = (byte)myPort.read();
tempByte2 = byteBuffer;
println("2: " + tempByte2);
if(tempByte2 == 79)
{
oFound = true;
}
else
{
append(photo, (byte)69);
append(photo, tempByte2);
eFound = false;
oFound = false;
fFound = false;
}
}
if(receivingPhoto && !eFound && !oFound && !fFound) //first bytes come in here.
{
byteBuffer = (byte)myPort.read();
tempByte = byteBuffer;
println("1: " + tempByte);
if(tempByte == 69) // if 'E' is found, switch modes.
{
eFound = true;
}
else
{
eFound = false;
oFound = false;
fFound = false;
append(photo, tempByte);
}
}
else if(eFound && oFound && fFound && receivingPhoto) //if EOF store data.
{
if(photo.length == photoSize)
{
saveBytes(filename, photo);
//output.write(photo);
//output.flush(); // Writes the remaining data to the file
//output.close(); // Finishes the file
eFound = false;
oFound = false;
fFound = false;
receivingPhoto = false;
println("Done!");
}
else
println("Sizes don't match");
}
else if(!receivingPhoto)
inString = myPort.readStringUntil(',');
// do other stuff with commands...
I'm desperate for some help here, it seems I've tried everything.
What I'm trying to achieve:
I have an SD card on arduino, with a photo saved on it, for testing.
I'm trying to send that JPEG file through Serial, to processing, and save on my PC.
Sounds easy, except I also use the Serial port for the sending/receiving of commands and values for other methods.
So, what I've done is told Arduino to first send the file size, then tell processing that file transmission will begin, and send the file.
Once sending is complete I then send "EOF" to processing.
In processing, I'm using the serialEvent method to capture data from serial.
In the method I have a series of boolean "switches" controlled by if statements that parse the incoming data.
If it recongises that a photo will be transmitted, it then switches to "data input mode", and receives the data.
Every byte is parsed and is flagged if it is an 'E'.
If so, I then switch again to check for 'O' and 'F'. (This is me recognising the end of file).
Once the EOF has been detected, I then flush and close the output.
The problem:
No matter what method of storing bytes I try, the JPEG is never correct PC side.
I've tried storing each byte at a time,
saving them in a byte array and calling output.write on the whole array or trying saveBytes on the array too.
Here's my code:
void serialEvent(Serial myPort)
{
// read a byte from the serial port:
// myPort.readBytesUntil(',', byteBuffer);
//String inString = new String(byteBuffer);
String inString = null;
byte byteBuffer = 0;
byte tempByte = -1, tempByte2 = -1, tempByte3 = -1;
if(eFound && oFound && !fFound) // if e and o found, look for f.
{
byteBuffer = (byte)myPort.read();
tempByte3 = byteBuffer;
println("3: " + tempByte3);
if(tempByte3 == 70)
{
fFound = true;
println("Found EOF");
}
else //else not found, store original values in proper order.
{
append(photo, (byte)69);
append(photo, (byte)79);
append(photo, tempByte3);
eFound = false;
oFound = false;
fFound = false;
}
}
if(eFound && !oFound && !fFound) // if eFound, look here for 'O'
{
byteBuffer = (byte)myPort.read();
tempByte2 = byteBuffer;
println("2: " + tempByte2);
if(tempByte2 == 79)
{
oFound = true;
}
else
{
append(photo, (byte)69);
append(photo, tempByte2);
eFound = false;
oFound = false;
fFound = false;
}
}
if(receivingPhoto && !eFound && !oFound && !fFound) //first bytes come in here.
{
byteBuffer = (byte)myPort.read();
tempByte = byteBuffer;
println("1: " + tempByte);
if(tempByte == 69) // if 'E' is found, switch modes.
{
eFound = true;
}
else
{
eFound = false;
oFound = false;
fFound = false;
append(photo, tempByte);
}
}
else if(eFound && oFound && fFound && receivingPhoto) //if EOF store data.
{
if(photo.length == photoSize)
{
saveBytes(filename, photo);
//output.write(photo);
//output.flush(); // Writes the remaining data to the file
//output.close(); // Finishes the file
eFound = false;
oFound = false;
fFound = false;
receivingPhoto = false;
println("Done!");
}
else
println("Sizes don't match");
}
else if(!receivingPhoto)
inString = myPort.readStringUntil(',');
// do other stuff with commands...
1