Log File in a USB Drive

Hi All,

I would like to create a .txt or .csv logfile in a USB drive that I would leave connected on a X20CP0411 or X20CP0483. Any examples, directions or guidance will be appreciated.

1: I want to record event and data for each duration the PLC runs, which means if the PLC is shutdown abruptly, I won’t be able to close the file properly.

2: I see the commands for creating, opening and closing files in ST but what is the path for the connected flash drive?


The AsUSB Library can be used to work with the USB Interfaces.
There are Examples which can help you to start.
AsUSB: Examples

If it is possible you should Buffer your Data and only open/close the File while writing. The Buffer can be placed in Persistend Memory.

I have no expierience what happens due to power loss on writing. Lets see what the others know.


In addition to the AsUSB library samples there is a more comprehensive example, which can also be directly imported to your project available here.

1 Like


i a have small program that write 2 variables to an usb drive
maybe it can help you a bit

Logging.zip (3.0 KB)

i also have a attached a version with some libraries
DataloggingToUsb.zip (169.8 KB)



Thanks Tommi, Michael and Sven, I will try it out and post back