Hi.
You can use method of Modbus Master: UpdateComParameters.
Keep in mind that you will need to call this method every time the controller is loaded, because when the runtime starts, the values will be overwritten by those set in the project tree.
I had already tried this method, which is called during every PLC cycle but it doesn't seem to affect/change the COM port settings.
Maybe there is something else that needs to be done first.
At first, both in the CODESYS and on the PC, the baudrate is set to 9600 - and the communication works. Then I change the baudrate to 19200 in the CODESYS from IEC-code - the communication stops (because the PC is still 9600). Then I set on PC 19200 - and the communication works again.
Last edit: kislov 2022-03-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I want to be able to change the baud rate of the COM port dynamically in runtime.
Anyone know how to achieve it?
Hi.
You can use method of Modbus Master: UpdateComParameters.
Keep in mind that you will need to call this method every time the controller is loaded, because when the runtime starts, the values will be overwritten by those set in the project tree.
Hi E.Kislov,
I had already tried this method, which is called during every PLC cycle but it doesn't seem to affect/change the COM port settings.
Maybe there is something else that needs to be done first.
eSmart07_RTU.UpdateComParameters(
udiBaudrate:= BaudRate,
usiMedium:= 1,
usiParity:= 2,
usiDataBits:= 8,
usiStopBits:= 1,
xPolarization:= FALSE,
COMPORT:= 1);
BTW you make great Codesys videos on YT.
I think you should use it this way:
eSmart07_RTU.xStop := TRUE;
eSmart07_RTU.Enable := FALSE; // maybe not necessary
eSmart07_RTU.UpdateComParameters(...);
eSmart07_RTU.Enable := TRUE; // maybe not necessary
eSmart07_RTU.xStop := FALSE;
Before using Enable property you should: Device - Plc Settings - Activate diagnostics for devices
Perhaps, Enable is not necessary, and xStop will be enough...
And thanks for the compliment, it's very encouraging.
Last edit: kislov 2022-02-14
Unfortunately it doesn't work.
I want to read the current COM settings so I found the SysComGetSettings function in the SysCom library.
"SysComGetSettings = Get the parameter of the interface specified by handle."
But how do I set the hComPort RTS_IEC_HANDLE to the serial port COM1?
Last edit: kallileo 2022-03-11
Hi. It works for me.
See my video snippet:
Download show_video_snippet
I see Modbus Slave software on my PC.
At first, both in the CODESYS and on the PC, the baudrate is set to 9600 - and the communication works. Then I change the baudrate to 19200 in the CODESYS from IEC-code - the communication stops (because the PC is still 9600). Then I set on PC 19200 - and the communication works again.
Last edit: kislov 2022-03-15