MICRO SD PRINTING

I printing to MICRO SD card using myFile.print and everything works fine printing text("ABCD") or uint8_t variables but I cannot get it to print char.

Serial.print prints the character OK; it is 02608ab1 but myFile.print prints it to the SD card as 000000x(last digit changes).

Attached is code with unnecessary items deleted.

I also could not find a good way to get tmp to print outside the "PRINT HEX " function so excuse my conversion from tmp to serialNumH

What is difference between Serial.print and myFile.print

Thanks
Bill

RFIDF_COINBOX_arduino.ino (9.36 KB)

That code won't even compile. The write_data() method is defined in the middle of loop(). Post some code that actually compiles.

serialNumH is not a string. Expecting to deal with it as a string is going to result in unmet expectations. A string is a NULL terminated array of chars. Your array of chars is NOT NULL terminated. Therefore, it is not a string. Do NOT pass it to a function that expects a string, such as myFile.print().

Paul:

Thanks for reply; attached is complete code.

How do I add the null terminator???

RFIDF_COINBOX_READER_CLOCK_SD_troubleshooting_4.ino (12.3 KB)

How do I add the null terminator???

First, there needs to be room for it.

char serialNumH[910]; //WJL-card serial number HEX

Then, add:

  serialNumH[9] = '\0';

after

  serialNumH[8] = tmp[8];

I m ade the changes but I get the same output.

Attached is the SD card file. The field for serialNumH seems to be printing out the "starting" field.

Again; you help is much appreciated

Bill

TEST.TXT (149 Bytes)

I m ade the changes

But didn't post the code. The code in the initial post did not even compile. Have you fixed that? How?

Seeing your serial output would be useful, too.

Please start posting your code using the code tags.post your code here.
Place the code between the two bracketed words which show up when you hit the </> icon in the tool bar.

Attached are code, SD card file and serial monitor capture.

This code records a RFID card swipe, each card swipe is a line of text on the SD card.

RFIDF_COINBOX_READER_CLOCK_SD_troubleshooting_4.ino (12.3 KB)

Attached are code, SD card file and serial monitor capture.

Meatloaf sings that two out of three aint bad. One out of three is, though.

Sorry:

thought they all were attached!??????

TEST_2.TXT (194 Bytes)

test_txt_serial_output.txt (1.42 KB)

  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(lcdtxPin, OUTPUT);

You told the SoftwareSerial instances that these were their pins. Quit f**king with them.

  pinMode(SS, OUTPUT);                                    //added for SD card

Where is SS defined?

  // 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);

You need to close it before you open it again.

  char idx;

Useless.

  while (rfidRead(rfidData, ADDR_Serial) != 0);
  Serial.println();
  PrintHex(rfidData, 4);   // The rfidData string should now contain the tag's serial number, so display it on the Serial Monitor
  Serial.println();
  Serial.println(rfidData[3]);
  serialNum = rfidData[3];
  Serial.println(serialNumH);
  Serial.flush();

  Serial.print(F("Reading tag's Type:\n"));
  while (rfidRead(rfidData, ADDR_Type) != 0); // Read the data
    
  PrintHex(rfidData, 4);   // The rfidData string should now contain the data from the Counter address(3),

PrintHex() is a stupid name for a function that more than that. Why does that function copy data to serialNumH? The second function call clearly is NOT copying the RFID tag's serial number to serialNumH.

There is only ONE place where PrintHex() should be copying data to serialNumH, not 4.

  Serial.print(tmp);

Anonymous printing sucks.

  Serial.print("tmp = [");
  Serial.print(tmp);
  Serial.println("]");

conveys so much more information.

Now, your complaint was that char data is not written correctly to the file. I see no proof of that. Which char data is not written correctly?

pinMode(SS,OUTPUT) is part of the adafruit SD library so it is probably defined in their *.h or *.cpp fille.

Added another myFile.clse(); but it doesn't change anything

char idx; came from the PARALLAX RFID code so I assumed it was necessary.

Don't quite understand your statment "There is only ONE place where PrintHex() should be copying data to serialNumH, not 4." Can you explain,please.

The problem is that the PrintHex function prints tmp correctly to the monitor using Serial.print within the PrintHex function, this is 023cc45c in the serial monitor file that was attached.

tmp is declared in the PrintHex function so I cannot print it to the SD card in the printData function; I created another char called serialNumH and if I print that to the SD card using MYfile.print but it prints 0000000x to the SD card. x changes with each card swipe and it is the "remaining" variable from that card swipe.

Both tmp and serialNumH print correctly using Serial.print().

How do I get the printHex function output to print correctly to the SD card using myFile.print()?????

you call rfidRead() twice, and read into the same rfidData. The first time with ADDR_Serial, and the second time with the ADDR_Type.

When you call it the first time, you appear to get the correct serial number into serialNumH. The second call obviously does not read the same data and it is overwriting the first set.

you call rfidRead() twice

4 or more times, actually.

Don't quite understand your statment "There is only ONE place where PrintHex() should be copying data to serialNumH, not 4." Can you explain,please.

From loop():

  while (rfidRead(rfidData, ADDR_Serial) != 0);
  Serial.println();
  PrintHex(rfidData, 4);   // The rfidData string should now contain the tag's serial number, so display it on the Serial Monitor

And store it in serialNumH.

From loop():

  while (rfidRead(rfidData, ADDR_Type) != 0); // Read the data
    
  PrintHex(rfidData, 4);   // The rfidData string should now contain the data from the Counter address(3),

and shit on the value stored in serialNumH.

From prePay():

  while (rfidRead(rfidData, ADDR_Counter) != 0); // Read the data  
  PrintHex(rfidData, 4);   // The rfidData string should now contain the data from the Counter address(3),

and shit on the value stored in serialNumH.

From payPerVisit():

   while (rfidRead(rfidData, ADDR_Counter) != 0); // Read the data  
   PrintHex(rfidData, 4);   // The rfidData string should now contain the data from the Counter address(3),

and shit on the value stored in serialNumH.

prePay() and payPerVisit() call write_data(), AFTER calling PrintHex() and shitting on serialNumH.

tmp is declared in the PrintHex function so I cannot print it to the SD card in the printData function

The latter is true. The former need not be. Not that it matters. Only one of the 4 places where PrintHex is called should be copying data to serialNumH.

Frankly, PrintHex() is a stupid name, since the function does not print anything. Get rid of the three incorrect calls (the 2nd one in loop() and the ones in prePay() and payPerVisit()) and rename the function to something that describes what it ACTUALLY does.

Both tmp and serialNumH print correctly using Serial.print().

ONLY after the first call. But that is NOT when you actually write the data to the SD card. Prove it to yourself. Print tmp and serialNumH IN PrintHex().

Got it!

It works now
Thanks for your help

Bill

It works now

Yay!