0
votes

I am trying to read the binary data of a picture from an SD card on an Ethernet shield with an Arduino Uno, then send the binary data over an Ethernet cable to my computer. I get a weird error message when I try and run the code, and I cant figure out why.

#include<SD.h>
#include <SPI.h>
#include <String.h>
#include <Ethernet2.h>
byte mac[]={0xB0,0xCD,0xAE,0x0F,0xDE,0x10};
IPAddress ip(169,254,95,37); //client ip for Andrew-Laptop
//IPAddress ip(169,254,155,102); //client ip for school laptop
IPAddress server(169,254,95,36); //server ip for Andrew-Laptop 
//IPAddress server(169,254,155,101); //server ip for school laptop
EthernetClient client;
int whatToDo=0;
void setup(){
  for(int a=3;a<=7;a++){
    pinMode(a,OUTPUT);
    digitalWrite(a,LOW);
  }
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  if(SD.begin(4)) digitalWrite(7,HIGH);
  Ethernet.begin(mac,ip);
  delay(1000);
  digitalWrite(6,HIGH);
  delay(1000);
  if(client.connect(server, 12345)){
    digitalWrite(5,HIGH);
  }
}
char bufSize[1024];
void loop(){
  if(whatToDo==0){
    File myFile=SD.open("Img.png",FILE_READ);
    while(myFile.available()){
      String msg=myFile.readString();
      int buf=1024;
      char new_msg=msg.toCharArray(bufSize,sizeof(bufSize)); //ERROR LINE
      //client.write(myFile.read());
      client.write(new_msg);
    }
    //client.write(msg);
    whatToDo=1;
  }else{
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(3,LOW);
    delay(500);
    if(whatToDo==1){
      client.write("");
      client.write("Done");
      whatToDo+=1;
    }
  }
}

The error that I am getting is:

SDTestHost:36: error: void value not ignored as it ought to be char new_msg=msg.toCharArray(bufSize,sizeof(bufSize)); exit status 1 void value not ignored as it ought to be

I want to send more than one byte of data at a time, which is the default for client.write(file.read()); and I cant figure out how to change the buffer size

2

2 Answers

0
votes

The toCharArray() method does NOT return a value. The value is returned in the parameters. You want something like:

char buffer[1024];
msg.toCharArray(buffer, 1024);

client.write(buffer, 1024);

You should probably declare the buffer outside of the function, otherwise it will be allocated on the stack.

0
votes

Google brought me here but I assume others are also interested in an example for buffered reads / writes on Arduino with SD.h and Ethernet.h. Maybe the following code helps somebody:

    int bufferSize = 64;
    while(file.available())
    {
      char buffer[bufferSize];
      memset(buffer, '\n', bufferSize);
      file.read(&buffer, bufferSize);
      client->write(buffer, bufferSize);
    }

For me this code snippet works and as there are no full working examples (at least I did not find one) I think this might be useful if someone stumbles over the same question.