Hi, dear gentlemen and ladies.
I’m developing a program that reads a file contained in an SD card via Arduino and prints the data of this file in hexadecimal format. At the same time, I am trying to create a program in Processing that receives this hexadecimal data from Arduino and writes a binary file.
But my program in Processing, is giving me an error of type “NumberFormatException: for input String […]”. And this error, does not allow me to create my binary file. Below is my Arduino code.
//27 march 2019
// sd card hex dump
// based on Lucario448's code in #2 here https://forum.arduino.cc/index.php?topic=605875
//
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
File file;
void setup()
{
Serial.begin(115200);
for (int seconds = 10; seconds >= 0; seconds--)
{
delay(1000);
}
openFileOnSD();
//remember to edit the file name into the function <<<<<<<<<<<<<<<<
if (file) dumpTheOpenedFile();
delay (5);
//Serial.write(file.read());
} //setup
void loop()
{
} //loop
void openFileOnSD()
{
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present ******************");
// don't do anything more:
return;
}
// Serial.println("Card initialized.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
file = SD.open("picture.jpg"); //<<<<<<<<<<<<<<<<<<< set the name here <<<<<<<<<<<<<<<
//printDirectory(file, 0);
if (file)
{
delay(150);
}
// if the file isn't open, pop up an error:
else
{
Serial.println("Error opening file... check the name ******************");
}
}
void dumpTheOpenedFile()
{
// Assuming the file is already opened for reading.
const byte bytesPerRow = 16; // 4 as minimum
char hexVal[9];
byte buffer[bytesPerRow];
file.seek(0);
// Typing file's content
while (file.available()) {
byte amount = file.read(buffer, bytesPerRow);
//Print hex values
for (byte i = 0; i < amount; i++)
{
sprintf(hexVal, "%02X", buffer[i]);
Serial.print(hexVal);
}
Serial.println(); // Next line
} // End of while loop
Serial.println("END");
file.close();
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
for (unsigned int counter = 0; counter < 5; counter++)
{
delay(1000);
}
} //dumpTheOpenedFile
Arduino prints the hex data in this way:
E4F6DEDEFEF6D34900624A4A1A524049
B8FCF6FEFCF6FE9C005852465852C69C
F7FFC6F6FEC6F67A007B42027A42027A
38BCF6F6F6F6FE5C001852525252465C
END
Now, this is my Processing code:
import processing.serial.*;
Serial myPort;
String val;
PrintWriter output;
void setup()
{
String portName = "COM4";
myPort = new Serial(this, portName, 115200);
output = createWriter("C:\\Users\\DRSF\\Documents\\pic.bin");
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readString();
delay(500);
}
println(val); //print it out in the console
if ( val!= null && val!="END") {
String serialArdu;
serialArdu = val;
hexToBinary(serialArdu);
output.println(serialArdu);
}
if (val=="END"){
output.flush();
// Writes the remaining data to the file
output.close(); // Finishes the file
}
}
String hexToBinary(String hex) {
int i = Integer.parseInt(hex,16);
String bin = Integer.toBinaryString(i);
return bin;
}
I have tried to convert the values to long, following a suggestion I saw in this same forum, but I still have the same errors.
I created a similar program in .NET. This program prints the data coming from the Arduino into a (pseudo) serial monitor and converts them successfully into a binary file. I would like to accomplish the same thing with Processing.
Any help will be appreciated.
*Edit: I’m Using Processing 3.4 IDE and Arduino 1.0.5 r2 in a Windows 7 Ultimate 64 bits machine.
Thanks in advance.