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

Two serial devices in parallel

GiorgioT
2016-11-01
2016-11-06
  • GiorgioT - 2016-11-01

    Hello,

    I'm struggling to figure out a way to communicate with two serial devices in codesys v3.5 SP9 + BBB rev C.

    I use both uart2(ttyO2) and uart4(ttyO4) ports. I can successfully communicate using only one uart port by appending the following line in /etc/CODESYSControl.cfg:

    [SysCom]
    Linux.Devicefile=/dev/ttyO
    portnum := COM.SysCom.SYS_COMPORT1;
    

    I use the example project available in the store: SerialComWithVisu with the following call to the FB Serial_Line

    FirstSerialLine(
          slMode:= SL_READWRITE, 
          udiPort:= 1, //tty02 ??? 
          udiBaudrate:= 115200,
          paParity:= COM.PARITY.NONE,
          sbStopBits:= COM.STOPBIT.ONESTOPBIT,
          udiByteSize := 8);
    

    Everything works fine. I can write and read out of ttyO2.

    Then i added the uart4 (ttyO4). I wired Tx4 -> Rx2 and Tx2 ->Rx4. I tested the communication with a python script and i was able to read and write from both serial devices.

    Following the thread http://forum.codesys.com/viewtopic.php?f=21&t=5699&start=15, i left unchanged the CODESYSControl.cfg and i added a second call to the FB setting 2 as COM2

    SecondSerialLine(
          slMode:= SL_READWRITE, 
          udiPort:= 2, 
          udiBaudrate:= 115200,
          paParity:= COM.PARITY.NONE,
          sbStopBits:= COM.STOPBIT.ONESTOPBIT,
          udiByteSize := 8);
    

    Both ports udiPort 1 and 2 open with no errors and the write function returns successfully. The read function instead always returns empty string. Moreover if i execute ```

    cat /dev/ttyO2 or 4

    ``` no output is returned as no one is actually writing in that file. I tried to change the udiPort value but ANY value i type gives the same result.
    What is the right way to handle two serial devices in parallel in codesys ?? Should i edit the CODESYSControl.cfg file to declare the second port too ? if yes, how ?

    Thank you.

     
  • eschwellinger

    eschwellinger - 2016-11-03

    hi,
    have you tried
    FirstSerialLine( slMode:= SL_READWRITE, udiPort:= 3, //ttyO2
    and
    FirstSerialLine( slMode:= SL_READWRITE, udiPort:= 5, //ttyO4

    another one posibility is to set symbolic links for your ports... in Linux.

    BR
    Edwin

     
  • GiorgioT - 2016-11-03

    Hi,

    It works like a charm now. I used udiPort 3 and 5 as you suggested!

    Leaving the CODESYSControl.cfg unchanged as i wrote it in my last post, The mapping between tty and COM in codesys is of the type: ttyOx -> COM(x+1), isn't it ? Hence, why must the udiPort be equal to 1, if i use only one uart port (e.g. ttyO2) ? What is the meaning of ```

    portnum := COM.SysCom.SYS_COMPORT1;

    ``` in CODESYSControl.cfg when two or more ports are used ?

    Sorry to bother you with all these questions but i'd like to understand better how the serial mapping works. It may be also useful to others.

    Thank you.

     
  • eschwellinger

    eschwellinger - 2016-11-06

    Hi,

    you could remove this line, make no sense.
    portnum := COM.SysCom.SYS_COMPORT1;

    only:

    [SysCom]Linux.Devicefile=/dev/ttyO
    is needed.

    BR
    Edwin

     

Log in to post a comment.