#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
File myFile;
SoftwareSerial mySerial(2, 3); // TX, RX
void setup()
{
// Open port for communication
Serial.begin(115200);
mySerial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
Serial.println(mySerial.read());
myFile.write(mySerial.read());
}
// close the file:
myFile.close();
Serial.println("done.");
}
void loop()
{
if (mySerial.available())
Serial.write(mySerial.read()); // if it receives a message, displays it on the serial monitor
}
However my sd card txt file displays an unknown character.
#include <SoftwareSerial.h> //Used for transmitting to the device
SoftwareSerial softSerial(2, 3); //RX, TX
SoftwareSerial mySerial(5, 6); // TX, RX
#include "SparkFun_UHF_RFID_Reader.h" //Library for controlling the M6E Nano module
RFID nano; //Create instance
void setup()
{
Serial.begin(115200);
mySerial.begin(115200);
while (!Serial); //Wait for the serial port to come online
if (setupNano(38400) == false) //Configure nano to run at 38400bps
{
Serial.println(F("Module failed to respond. Please check wiring."));
while (1); //Freeze!
}
nano.setRegion(REGION_OPEN); //Set to North America
nano.setReadPower(2300); //5.00 dBm. Higher values may caues USB port to brown out
//Max Read TX Power is 27.00 dBm and may cause temperature-limit throttling
Serial.println(F("Press a key to begin scanning for tags."));
while (!Serial.available()); //Wait for user to send a character
Serial.read(); //Throw away the user's character
nano.startReading(); //Begin scanning for tags
}
void loop()
{
if (nano.check() == true) //Check to see if any new data has come in from module
{
byte responseType = nano.parseResponse(); //Break response into tag ID, RSSI, frequency, and timestamp
if (responseType == RESPONSE_IS_KEEPALIVE)
{
Serial.println(F("Scanning"));
}
else if (responseType == RESPONSE_IS_TAGFOUND)
{
//If we have a full record we can pull out the fun bits
int rssi = nano.getTagRSSI(); //Get the RSSI for this tag read
long freq = nano.getTagFreq(); //Get the frequency this tag was detected at
long timeStamp = nano.getTagTimestamp(); //Get the time this was read, (ms) since last keep-alive message
byte tagEPCBytes = nano.getTagEPCBytes(); //Get the number of bytes of EPC from response
Serial.print(F(" rssi["));
Serial.print(rssi);
Serial.print(F("]"));
Serial.print(F(" freq["));
Serial.print(freq);
Serial.print(F("]"));
Serial.print(F(" time["));
Serial.print(timeStamp);
Serial.print(F("]"));
Serial.print(F("]"));
Serial.println();
mySerial.print(" rssi");
mySerial.print(rssi);
mySerial.print(F("]"));
mySerial.print(F(" freq["));
mySerial.print(freq);
mySerial.print(F("]"));
mySerial.print(F(" timestamp["));
mySerial.print(timeStamp);
mySerial.println(F("]"));
delay(1000);
}
else if (responseType == ERROR_CORRUPT_RESPONSE)
{
Serial.println("Bad CRC");
}
else
{
//Unknown response
Serial.print("Unknown error");
}
}
}
//Gracefully handles a reader that is already configured and already reading continuously
//Because Stream does not have a .begin() we have to do this outside the library
boolean setupNano(long baudRate)
{
nano.begin(softSerial); //Tell the library to communicate over software serial port
//Test to see if we are already connected to a module
//This would be the case if the Arduino has been reprogrammed and the module has stayed powered
softSerial.begin(baudRate); //For this test, assume module is already at our desired baud rate
while (softSerial.isListening() == false); //Wait for port to open
//About 200ms from power on the module will send its firmware version at 115200. We need to ignore this.
while (softSerial.available()) softSerial.read();
nano.getVersion();
if (nano.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
{
//This happens if the baud rate is correct but the module is doing a ccontinuous read
nano.stopReading();
Serial.println(F("Module continuously reading. Asking it to stop..."));
delay(1500);
}
else
{
//The module did not respond so assume it's just been powered on and communicating at 115200bps
softSerial.begin(115200); //Start software serial at 115200
nano.setBaud(baudRate); //Tell the module to go to the chosen baud rate. Ignore the response msg
softSerial.begin(baudRate); //Start the software serial port, this time at user's chosen baud rate
delay(250);
}
//Test the connection
nano.getVersion();
if (nano.msg[0] != ALL_GOOD) return (false); //Something is not right
//The M6E has these settings no matter what
nano.setTagProtocol(); //Set protocol to GEN2
nano.setAntennaPort(); //Set TX/RX antenna ports to 1
return (true); //We are ready to rock
}
takes one character out of the serial receive-buffer and then prints it.
After printing the character is gone forever.
If you want this character to be printed and safed to your SD-card
You have to store the character in a variable and then use the variable for all operations
above setup declare
char myCh;
inside your function
myCh = mySerial.read(); // take character out of serial receivebuffer AND store into a variable
Serial.println(myCh); // use variable to print the character
myFile.write(myCh); // use variable to store the character on SD-card
Hi Stefan, i tried implementing your soln into my program. However the txt output is still the same.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
char myCh;
File myFile;
SoftwareSerial mySerial(2, 3); // TX, RX
void setup()
{
// Open port for communication
Serial.begin(115200);
mySerial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
myCh= mySerial.read();
if (myFile) {
Serial.print("Writing to test.txt...");
Serial.println(myCh);
myFile.write(myCh);
}
// close the file:
myFile.close();
Serial.println("done.");
}
void loop()
{
if (mySerial.available())
Serial.write(myCh); // if it receives a message, displays it on the serial monitor
}