We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Send image from arduino to processing
Page Index Toggle Pages: 1
Send image from arduino to processing (Read 1217 times)
Send image from arduino to processing
Apr 13th, 2010, 2:41pm
 
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
Page Index Toggle Pages: 1