This is a small project that allows communication between a Codesys-based PLC and a PC.
The project is based on transferring data from the PLC to the PC via Network Variables (Network Variable Lists / NVL). This is a basic Codesys functionality that allows you to transfer data to another PLC (or with this project to a PC) with minimal effort using the UDP protocol.
The project allows you to generate variable objects based on Codesys configuration files and receive data from the PLC. In addition, it is possible to automatically generate a table in a PostgreSQL or Sqlite3 database and populate it as you receive data.
configuration file
<GVL>
<Declarations><![CDATA[VAR_GLOBAL
sinus, cosine: REAL;
some_word : WORD;
integer : INT;
my_time : TIME;
my_date : DATE;
my_string20 : STRING(20);
my_array : ARRAY[0..4] OF INT;
END_VAR]]></Declarations>
<NetvarSettings Protocol="UDP">
<ListIdentifier>1</ListIdentifier>
<Pack>True</Pack>
<Checksum>False</Checksum>
<Acknowledge>False</Acknowledge>
<CyclicTransmission>True</CyclicTransmission>
<TransmissionOnChange>False</TransmissionOnChange>
<TransmissionOnEvent>False</TransmissionOnEvent>
<Interval>T#500ms</Interval>
<MinGap>T#20ms</MinGap>
<EventVariable>
</EventVariable>
<ProtocolSettings>
<ProtocolSetting Name="Broadcast Adr." Value="192.168.56.35" />
<ProtocolSetting Name="Port" Value="1202" />
</ProtocolSettings>
</NetvarSettings>
</GVL>
Copy this file to CodesysNetVar/external
The project is configured through environment variables or an .env
file.
The .env file must be located in CodesysNetVar/settings
. The database password must be in the form of
file with no extension CNV_STORAGE___PASSWORD
, inside there should be only the password, no spaces, indents, etc. file
must be in the folder CodesysNetVar/settings/secrets
.
.env
### Settings
## network
CNV_NETWORK___LOCAL_IP="192.168.56.35"
CNV_NETWORK___LOCAL_PORT="1202"
## storage
CNV_STORAGE___DB_TYPE="postgresql" #or 'sqlite3'
CNV_STORAGE___IP_OR_PATH="127.0.0.1" #or '/db.sqlite'
CNV_STORAGE___PORT="5432"
CNV_STORAGE___LOGIN="postgres"
CNV_STORAGE___DB_NAME="mydb"
CNV_STORAGE___TABLE_NAME_PREFIX="nvl" #prefix for table in DB
##nvl_files
CNV_NVL___PATHS='["external/exp.gvl"]'
##logger
CNV_LOGGER___LEVEL_IN_STDOUT="INFO" #or DEBUG, INFO, WARNING, ERROR or Comment this string for disable stdout
CNV_LOGGER___LEVEL_IN_FILE="INFO" #or DEBUG, INFO, WARNING, ERROR or Comment this string for disable filelog
CNV_LOGGER___FILE_ROTATE="1 MB" # see format in loguru
install dependencies
poetry install
Run
poetry run python main.py
CodesysNetVar/external
and in
.env
the parameter CNV_NVL___PATHS='["external/exp1.gvl", "external/exp2.gvl"]'
must list these files