Chisty (Customer) asked a question.

Can someone guide me with the code on how to upload/write data in the micro SD card in the P1AM-100(ProOpen) PLC?
I am not sure how and what libraries to use the SD Card as a Data logger through the P1AM-100
I added a code that I tried but didn't work

#include <P1AM.h>

#include <SPI.h>

#include <SD.h>

 

File myFile;

 

void setup() {

 // Open serial communications and wait for port to open:

 Serial.begin(9600);

 

  

 while (!Serial) {

  ; // wait for serial port to connect. Needed for native USB port only

 }

 

while (!P1.init()) {

  ; //wait for modules to sign on

 }

 

 Serial.print("Initializing SD card...");

 

 if (!SD.begin(A3)) {

  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 datalogger test.txt...");

  myFile.println("testing 1, 2, 3. /n Testing Datalogger");

  // close the file:

  myFile.close();

  Serial.println("done.");

 } else {

  // if the file didn't open, print an error:

  Serial.println("error opening test.txt");

 }

 

//  re-open the file for reading:

 myFile = SD.open("test.txt");

 if (myFile) {

  Serial.println("test.txt:");

 

  // read from the file until there's nothing else in it:

  while (myFile.available()) {

   Serial.write(myFile.read());

  }

  // close the file:

  myFile.close();

 } else {

  // if the file didn't open, print an error:

  Serial.println("error opening test.txt");

 }

}

 

void loop() {

 // nothing happens after setup

}

 


  • Garry (Customer)

    Hi Chisty,

    I just used Productivity Blocks to get a quick sample.

    Productivity Blocks Card read append

    1. #include <SD.h>
    2.  
    3. #define _PBVAR_1_CPU_SDCS_PIN 28
    4.  
    5. void __ProBlocksSDWrite(String fileName, String writeString, boolean newLine, int chipSelect) {
    6. File writeFile = SD.open(fileName, FILE_WRITE);
    7. if (writeFile){
    8. writeFile.print(writeString);
    9. if (newLine){
    10. writeFile.println("");
    11. }
    12. }
    13. writeFile.close();
    14. }
    15.  
    16. String _PBVAR_2_String_Variable = "" ;
    17. String __ProBlocksSDRead(String fileName, int seekTo, int length, int chipSelect) {
    18. char result[length];
    19. File readFile = SD.open(fileName);
    20. int index = 0;
    21. if (!readFile.seek(seekTo)){
    22. return "";
    23. }
    24. while (readFile.available() && (index < length)){
    25. result[index++] = readFile.read();
    26. }
    27. readFile.close();
    28. return result;
    29. }
    30.  
    31. void setup()
    32. {
    33. SD.begin(_PBVAR_1_CPU_SDCS_PIN);
    34.  
    35.  
    36. }
    37.  
    38. void loop()
    39. {
    40. __ProBlocksSDWrite("file.txt", "Message", true, _PBVAR_1_CPU_SDCS_PIN);
    41. _PBVAR_2_String_Variable = __ProBlocksSDRead("file.txt", 0, 0, _PBVAR_1_CPU_SDCS_PIN) ;
    42. }
    43.  

    I hope this helps you out.

    Regards,

    Garry

     

    https://accautomation.ca/series/productivity-open-arduino-compatible-industrial-controller/

    Expand Post
    • Chisty (Customer)

      Thank You Garry for your kind help, I am going to try this out.

      • Garry (Customer)

        No Problem. This is one of the reasons that I like ProductivityBlocks. This will give you some basic code as a start.

        Garry

  • FACTS_AdamC (AutomationDirect)

    Hey Chisty,

     

    I believe your issue is probably that you have " if (!SD.begin(A3)) {" instead of " if (!SD.begin(SDCARD_SS_PIN)) {"

     

    That function selects the chip select pin for the SD card. A3 is not routed to the SD card on our board, so it won't properly control it.

     

    For a quick check if everything is working, use the example under SD>cardinfo in the Arduino IDE. Make sure to change the chip select pin in that example as well.

     

    Thanks

    Adam

     

     

    Expand Post