| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This Example shows how to use a FileStream to check the binary generation timestamp whenever a Component is reinitialized.
| Description | Value |
|---|---|
| Controller | AXC F 2152 |
| FW | 2025.0 |
| SDK | 2025.0 |
| PLCnext Engineer | 2025.0 |
During the Setup of the PLCnext Runtime our PLM component is loaded and the Initialize, LoadConfig , etc. are executed. This application writes a at compiletime generated localtime string TIMESTAMP into a file.
This way, whenever the PLC Starts it will first load the last generated file, compare it to the Timestamp of the current binary and then overwrite the Binary.
Here is some example output this Component will create if you download some changes to the running PLC.
24.02.21 14:40:47.921 FileStreamExample.FileStreamExampleComponent INFO - ---Initialize - Compile on Wed Feb 24 14:14:21 2021
24.02.21 14:40:47.923 FileStreamExample.FileStreamExampleComponent INFO - ---LoadConfig
24.02.21 14:40:47.924 FileStreamExample.FileStreamExampleComponent INFO - --- Read from file: Wed Feb 24 14:28:59 2021
24.02.21 14:40:47.926 FileStreamExample.FileStreamExampleComponent INFO - Last Date: Wed Feb 24 14:28:59 2021
24.02.21 14:40:47.927 FileStreamExample.FileStreamExampleComponent INFO - Current Date: Wed Feb 24 14:14:21 2021
24.02.21 14:40:47.929 FileStreamExample.FileStreamExampleComponent WARN - --- New Binary has been loaded!
24.02.21 14:40:47.932 FileStreamExample.FileStreamExampleComponent INFO - --- SetupConfig
24.02.21 14:40:47.935 FileStreamExample.FileStreamExampleComponent INFO - --- File:logs/TestFile.txt of Length:24 createdvoid FileStreamExampleComponent::WriteToFile(String textToWrite) {
try {
// Create a FileStream that force creates a file.
Arp::System::Commons::Io::FileStream stream(filePath,
Arp::System::Commons::Io::FileMode::Create);
// String
Arp::String str = textToWrite;
try {
// Write String
if (stream.CanWrite())
stream.Write((Arp::byte*) str.CStr(), str.Length(), 0, str.Length());
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Write to File. Error message: {0}.",
e.GetMessage());
}
//Get File Length
size_t filelen = stream.GetLength();
log.Info("--- File:{0} of Length:{1} created", filePath, filelen);
stream.Close();
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Create File:{0}", e.GetMessage());
}
}String FileStreamExampleComponent::ReadFromFile() {
try {
Arp::System::Commons::Io::FileStream stream(filePath,
Arp::System::Commons::Io::FileMode::Open);
//init string
String str((String::size_type) stream.GetLength(),
(String::CharType) '0');
try {
// Read String
if (stream.CanRead()) {
stream.Read((Arp::byte*) str.CStr(), stream.GetLength(), 0, stream.GetLength());
log.Info("--- Read from file: " + str);
}
}
//Here you could catch all exceptions separately
catch (const Arp::Exception &e) {
log.Error("--- Cannot read: {0}.", e.GetMessage());
str = e.GetMessage();
}
stream.Close();
return str;
} catch (Arp::System::Commons::Io::NotExistException &e) {
// Example of catching InvalidPathException
log.Error("--- File does not exist. Message: {0}.", e.GetMessage());
nofile = true;
return e.GetMessage();
} catch (const Arp::Exception &e) {
log.Error("--- Cannot Open File. Error message: {0}.", e.GetMessage());
return e.GetMessage();
}
}There are a lot of exceptions that you should handle when working with files. There are Function Specific Exceptions like a NotExistException that might indicate that something went wrong during the last shutdown.
But also common exceptions like Commons::Io::OutOfSpaceExceptions. These might indicated that the whole application should better stop.
File or IO activities can take quite a long time and might inhibit your RealTime operation. That is why it is generally recommended to move all file operaitons you need into threads.
| Back | FazBrowse Home | New Git URL |