I am trying to receive file on arduino sd through serial port (By Serial Port terminal) . From pc am sending file by bytes, and arduino can't receive exactly that file, but when i am sending text (txt file), it is same. Maybe i am recieving not a bytes? maybe strings?
#include "Thermal.h"
#include "SD.h"
#include "SPI.h"
#include <avr/pgmspace.h>
#include "SoftwareSerial.h"
byte incomingByte;
int printer_RX_Pin = 5; // This is the green wire
int printer_TX_Pin = 6; // This is the yellow wire
File myFile;
void setup(){
Serial.begin(9600);
printer.begin();
if (!SD.begin(4)) {
Serial.println("Initialization failed!");
return;
}
//If exist then print
if (SD.exists("2.bin"))
{
Serial.println("Cant print");
}
else
{
printer.wake(); // MUST call wake() before printing again, even if reset
myFile = SD.open("2.bin");
do something....
SD.remove("2.bin"); //Remove after print
Serial.println("File deleted."); //Need to remove after do something
}
}
void loop() {
File dataFile = SD.open("2.bin", FILE_WRITE);
while (Serial.available())
{
dataFile.write(Serial.read());
Serial.flush();
}
dataFile.close();
}
You are saving exactly what you recieve. All data is in bytes all strings are in bytes. If it is not what you expect you are interpreting it incorrectly.
What does thins mean? The Serial Monitor application can not send binary data. So, what application IS sending binary data?
From pc am sending file by bytes,
Some proof of that would be good.
and arduino can't receive exactly that file,
Again, some proof of that would be good.
do something....
Yeah, I'm sure THAT compiled.
dataFile.write(Serial.readBytes());
I have my doubts that THAT compiled, too, since the readBytes() method takes two arguments, and returns the number of bytes actually read. I can't see that writing that information to the file is all that useful.
There is no difference between a binary file and a text file as far as the arduino is concerned, it is all bytes. Your problem must be the send end of things.
I suspect you are not sending true bytes but some form of package or bin hex.
Have you done a byte comparison between what you send and what you recieve by compairing the two files with something like hex dump?
So, you changed your code from something that wouldn't even compile to something that at least compiles. You didn't share that modified code, which still doesn't work. And, yet, you want us to help you determine what the problem is. Well, good luck with that.
PaulS:
So, you changed your code from something that wouldn't even compile to something that at least compiles. You didn't share that modified code, which still doesn't work. And, yet, you want us to help you determine what the problem is. Well, good luck with that.
Sorry, i am posted first wrong code, where i am trying to solve problem, after i am immediately change to first version.
All code
#include "Thermal.h"
#include "SD.h"
#include "SPI.h"
#include <avr/pgmspace.h>
#include "SoftwareSerial.h"
byte incomingByte;
int printer_RX_Pin = 5; // This is the green wire
int printer_TX_Pin = 6; // This is the yellow wire
File myFile;
Thermal printer(printer_RX_Pin, printer_TX_Pin);
void setup() {
Serial.begin(9600);
printer.begin();
if (!SD.begin(4)) {
Serial.println("Initialization failed!");
return;
}
//If exist then print
if (!SD.exists("2.bin"))
{
printer.println("Cant print");
}
else
{
printer.wake(); // MUST call wake() before printing again, even if reset
myFile = SD.open("2.bin");
Serial.print("Printing file...");
printer.printBitmap(384, 385, dynamic_cast<Stream*>(&myFile));
SD.remove("2.bin"); //Remove after print
Serial.println("File deleted.");
}
printer.sleep(); // Tell printer to sleep
printer.wake(); // MUST call wake() before printing again, even if reset
printer.setDefault(); // Restore printer to defaults
}
void loop() {
File dataFile = SD.open("2.bin", FILE_WRITE);
while (Serial.available())
{
dataFile.write(Serial.read());
}
dataFile.close();
}
Grumpy_Mike:
That is not a hex dump comparison of the files.
The two files match until the 88th byte then they go wrong and after 300 bytes the start of the file is sent again.
So either this is something wrong with the sending program or your code. The repeats could indicate that your sending code is starting again.
I notice that your code simply stops and closes the file when there is no more data in the buffer. It could be that your sending program is not sending data continuously and when it pauses you are closing the file. Make it that you only close the file when nothing has been received for say three seconds.
I think it's to, write to the file only after all data have come.
To write to what file? Serial isn't a file. You are not writing to the serial port. There is NO reason to wait for the outgoing serial buffer to be empty, since you never write to it.
But, with serial.flush or without, output is same.
Please., do not be angry, try to explain the thinking
void loop(){
myFile = SD.open("2.bin", FILE_WRITE); //Open file
while (Serial.available()) //do job while serial aviable
{
byte b = Serial.read(); //start to read from serial
Serial.flush(); //wait after all data have come
myFile.write(b); //write data to file
}
myFile.close(); //close file
}
It looks like your problem is with the printer understanding the data in the file. The way I see it, you have two potential problems. One is that the file that you are sending and the file that you are printing is not the same. The other is that the file that you are printing is not being printed correctly. Until you know which problem you are solving, you are not going to make progress solving it.
Take the SD card out of the Arduino. Put it in the PC. Write a file to it. Take it out of the PC. Put it back in the Arduino and start the Arduino. Do you get the right picture printed? If so, the problem is with the file transfer/write to SD card process. If not, the problem is with the file read/sent to printer process.
One thing you need to understand is that serial data transmission is NOT guaranteed. You should NOT be relying on receiving every single byte undamaged. You need to send a series of bytes and a checksum. The Arduino needs to receive the series of bytes and the checksum. It needs to compute the checksum for the bytes it received, and compare that to the checksum it received. If they match, write the bytes to the file, and ask for the next packet. If not, discard them and ask for the same packet again.
There is nothing in your receive process that deals with that sort of issue. There needs to be.