Hello,
I have a C328 Serial camera connected to my Arduino board. I try to modify an excising piece of code to automatically save the received pictures:
Code:
import processing.serial.*;
Serial myPort;
int i = 1;
int TotalBytes = 0;
String ImageFilename = "Photo_" + year() + "_" + day() + "_" + month() + "-" + hour() + "_" + minute() + "_" + second() + "_" + millis() + ".jpg";
int CharEOF = 126;
void setup()
{
//Set and open serial port
println( Serial.list() );
myPort = new Serial( this, Serial.list()[1], 9600 );
myPort.clear();
}
void draw()
{
// Import Picture
byte[] Photo = {
};
byte[] buffer = new byte[64];
while( myPort.available() > 0 )
{
int readBytes = myPort.readBytes(buffer);
print( "Read " );
print( readBytes );
println( " bytes ..." );
for( int i = 0; i < readBytes; i++ )
{
Photo = append( Photo, buffer[i] );
}
// Save picture to file
if( Photo.length > 0 ) {
print( "Writing to disk " );
print( Photo.length );
println( " bytes ..." );
saveBytes( ImageFilename, Photo );
println( "DONE!" );
//Clear the array
Photo = null; // clear array
// Open Picture
size(640, 480);
PImage C328Image;
C328Image = loadImage(ImageFilename);
image(C328Image, 0, 0);
//Flush serial port
myPort.clear();
}
}
}
The last packet of the picture has a "~" (Chr(126) at the end to indicate the end of file.
I would like to start this piece of code:
Code:
// Save picture to file
if( Photo.length > 0 ) {
print( "Writing to disk " );
print( Photo.length );
println( " bytes ..." );
saveBytes( ImageFilename, Photo );
println( "DONE!" );
//Clear the array
Photo = null; // clear array
// Open Picture
size(640, 480);
PImage C328Image;
C328Image = loadImage(ImageFilename);
image(C328Image, 0, 0);
//Flush serial port
myPort.clear();
}
right before "~" enters the buffer.
I was thinking something like this:
Code:
if (buffer[i] = char(126))
{
// Save picture to file
if( Photo.length > 0 ) {
print( "Writing to disk " );
print( Photo.length );
println( " bytes ..." );
saveBytes( ImageFilename, Photo );
println( "DONE!" );
//Clear the array
Photo = null; // clear array
// Open Picture
size(640, 480);
PImage C328Image;
C328Image = loadImage(ImageFilename);
image(C328Image, 0, 0);
//Flush serial port
myPort.clear();
}
But then I get the following error on the If statement:
Cannot convert from byte to boolean.Because of my poor processing skills I would like to have the communities advise on how to solve it.
Thanks in advance,
Cheers