dear developers, i would like to read varaiables stored in a textfile. if it is possible: how to read the textfile with c++ program and where to store the file on the axc f 2152? (NOW I STORED /opt/plcnext/projects/PCWE/Libs/“the .cpp file name”) this is my current program: fstream file_(“modbus settings.txt”); if(file_.is_open()) { int i = 0; while(!file_.eof()&&i;<100) { getline(file_,line_); int n = line_.length(); char char_array[n+1]; strcpy(char_array, line_.c_str());
for (int i = 0; i < n; i++){ textfilearray[i] = char_array[i]; } i++;}
file_.close(); } the include seems not to work or the textfile is stored in the wrong location? “i” doesn’t grow because the programs hangs in a matter of miliseconds. (would expect the programs leaves the loop at i >100) sow how to read the file? kind regards Nick Peeters
Output.zip
Hi Nick, Can you please try:
1. Placing the file somewhere outside the PCWE directory. This directory is used for the PLCnext Engineer project, and there is no guarantee that the contents of this directory (including sub-directories) won't be deleted by PLCnext Engineer during downloads.
2. Hard-coding the complete path to the file you're reading, e.g. /opt/plcnext/modbus.txt. I'm not sure what path will be used as the root for the search, but it may not be the path that the shared object library is in.
If this does not help, you can try adding some log messages to your C++ code (example below) to help see what’s going on. These log messages will appear in the file /opt/plcnext/logs/Output.log this->log.Info("Processing line number {}", i); Another point to keep in mind - if you are doing this from a C++ Program, remember that: a) this program is running on a Task that may have a Watchdog set - so if this processing takes longer than the Watchdog time (which may happen when processing large files), the task will stop. It is recommended that things like file I/O are done in a Component rather than a Program. b) If this processing is done in the Execute method of the Program, you will need some way of stopping this file from being opened and read every time this method is called. Hope this helps. - Martin.
dear developpers, I have to check for the responce of martin, but let’s make clear why to do file I/O. In my application I would like to have some space to store variables which will be in “eeprom” memorie (so variables will have the latest value in case of power loss). so how to change the initual values from within c++ or how to store variables durable the easiest way? (for example error status of secundairy hardware needs to be durable stored , or changing variables from within an hmi page should realy change the initual value) I could defenitly use file I/O to deal with this “problem” (store the values in a .txt file and read the file each first run of the execute() to set the values) but maybe there is a better way? kins regards nick
Hi Nick, One way to achieve this is to declare the retentive variables as Port variables, and decorate them with the “Retain” attribute. In this way, the variable values will be stored in non-volatile RAM. (I am assuming that you are developing a PLCnext Component and/or PLCnext Program, not an application that is independent of the PLCnext runtime) Martin.
Hi, I’m also trying to read a textfile with a C++ program and store that information in AXC F 2152 program. I’ve already tried the things that Martin suggested but nothing happened. So, I would like to know if it’s there any solution. ~Francisco
Hi Francisco, Sorry for the delay. Are you able to post the relevant part of your code, describe what you expect to see when the code executes, and how you can tell that the expected result is not happening? This will make it easier for us to investigate the problem. Thanks!