Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Change COM port settings dynamically

kallileo
2022-02-07
2022-03-15
  • kallileo - 2022-02-07

    Hi,

    I want to be able to change the baud rate of the COM port dynamically in runtime.
    Anyone know how to achieve it?

     
  • kislov - 2022-02-08

    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.

     
  • kallileo - 2022-02-14

    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.

     
  • kislov - 2022-02-14

    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
  • kallileo - 2022-03-11

    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?

    rtsResult            : RTS_IEC_RESULT;
    stComPortSettings    : SysCom.ComSettings;
    hComPort             : RTS_IEC_HANDLE;
    
    **hComPort := ????**
    rtsResult := SysComGetSettings(hCom:= hComPort, pSettings:= ADR(stComPortSettings), pSettingsEx:= 0);
    
     

    Last edit: kallileo 2022-03-11
    • kislov - 2022-03-15

      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

Log in to post a comment.