How to connect serial devices ?

PLC
2014-04-02
2024-01-06
<< < 1 .. 3 4 5 6 > >> (Page 5 of 6)
  • bizer - 2020-04-01

    Hello together,

    I'm facing some issue with the internal connection (UART) of the Raspberry 3 and Codesys 3.5.15.SP4.
    If I read everything right internal interface GPIO 14/15 should work with RS 485.

    • Activate serial via raspi-config.
    • Unable bluethooth:
      $ echo 'dtoverlay=pi3-disable-bt' | sudo tee -a /boot/config.txt
      $ sudo systemctl disable hciuart
      Removed /etc/systemd/system/multi-user.target.wants/hciuart.service
      $ sudo reboot

    • add in sudo nano /etc/CODESYSControl.cfg
      Linux.Devicefile=/dev/ttyAMA or ttyS
      portnum := COM.SysCom.SYS_COMPORT1

    With AMA -->Modbus Master is green, but Slave doesn't connect...
    With ttyS--> Modbus Master is red...
    Communication via USB-RS485-Adapter works...

    Any hints or tipps what could be modified?

    Regards
    Benedikt

     
  • bizer - 2020-04-10

    No one on this topic?

     
  • eschwellinger

    eschwellinger - 2020-04-10

    Hi,
    remove if there is an section in /etc/CODESYSControl.cfg and add change it here in:
    /etc/CODESYSControl_Users.cfg

    [SysCom]
    Linux.Devicefile.1=/dev/ttyAMA

    if USB com port should be your Com1 in CODESYS...

    BR
    Edwin

     
  • bizer - 2020-04-12

    Hi Edwin, thanks a lot!!!

    I deleted the SysCom entry in /etc/CODESYSControl.cfg and added in /etc/CODESYSControl_Users.cfg:
    [SysCom]
    Linux.Devicefile.1=/dev/ttyAMA
    -->Modbus Master stays red

    If I add Linux.Devicefile=/dev/ttyAMA (without .1)
    -->Modbus Master gets green but still Slaves stays red/yellow

    Under USB everything works fine...Any thoughts?

     
  • johnhastech

    johnhastech - 2020-04-14

    Hi there,

    I've been reading through this thread and have tried most solutions to try and achieve RS232 comms via USB adapter. The difference is the hardware I am using is an HMI from ifm Electronic (Codesys SP15 Patch1), running Linux version 4.14.98-yocto-standard.

    I've tried many variations of the CODESYScontrol.cfg and CODESYScontrol_User.cfg files for SysCom settings but none delivered a good result. The Linux commands to read and write using the USB adapter seem to work fine, it just seems that the link for Codesys is not successful. The latest firmware of the device actually already includes the SysCom configuration in the CODESYSControl.cfg, but there are multiple copies in different file locations - perhaps a problem?

    Using 'dmesg' I can see that the USB ports are assigned as ttyUSB0, ttyUSB1 and ttyUSB2, and that the FDTI serial device has been recognised and mounted properly.

    I was originally using the SysCom library, but have used the SerialComWithVisu example to make sure it wasn't my code that was incorrect. Neither the example or my code using SysCom.lib can make a connection. I seem to get a valid handle from SysComOpen but SysComSetSettings and SysComRead are returning an error (RTS_IEC_RESULT = 1). Although I assume the SysComRead is returning an error because of invalid COM settings. I have attached my COM settings - please tell me if they are wrong.

    The code that I wrote with the SysCom library works no problem on an embedded PLC from ifm, so I'm confident in the settings and the code - I just think there is something I need to do on the Linux side for the HMI.

    Summary of set-up:

    - ifm CR1059 display
    - Brainboxes US101 RS232 adapter connected to USB0 of CR1059
    - Windows PC with Hercules for simulating RS232 device (read/write)
    

    If you need further details I'll happily share them.

    Best regards,

    John

     
  • eschwellinger

    eschwellinger - 2020-04-15

    Hi John,
    which runtime version does this CR1059 have?
    (goto plcshell -> rtsinfo

    BR
    Edwin

     
  • johnhastech

    johnhastech - 2020-04-15

    Hi Edwin,

    Thank you for your quick response!

    My RTS version is 3.5.15.10.

    Best regards,

    John

     
  • johnhastech

    johnhastech - 2020-04-15

    Hi Edwin,

    My CODESYSControl.cfg file was:

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

    But now I have removed the ';'

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

    This seems to work now, but the data I am receiving is not right - this could be my USB adapter or hardware set-up, but if you (or anyone else in the community!) have any pointers then that would be very helpful!

    I am usuing Hercules to send:

    'Hello'
    

    But I am receiving this in my Codesys application:

    'ÿ'
    

    I am also not receiving any data from the Codesys Application, despite the SysComWrite function returning 0 (no error). Perhaps I have a wiring problem?

    Best regards,

    John

     
  • eschwellinger

    eschwellinger - 2020-04-15

    Baudrate?

     
  • victorherma - 2020-06-17

    Hello,

    I am trying to do a project with Raspberry Pi 4 and Codesys that includes a USB / Modbus adapter, which works perfectly, and a SIM7600E device connected to the GPIO because I need to send information by SMS from the program.
    With this last device I am having problems. I've prepared a python script that works fine from the Raspberry (even with the Codesys app running), but it doesn't from the Codesys.
    Another test script, which turns on an LED in the GPIO when it should send an SMS, works without problems from Codesys, so I imagine the problem must be that Codesys cannot communicate with the UART due to lack of some configuration.
    I have reviewed countless forum posts and am unable to find a solution.
    This is a screenshot of the CODESYSConfig.cfg. I made this configuration for the USB / Modbus adapter.

    Is another silyl configuration required for the UART?
    Any suggestions where the problem could come from?
    Thank you .

     
  • eschwellinger

    eschwellinger - 2020-06-17

    hi,
    since 3.5SP15 you could do it that way:

    /etc/CODESYSControl_Users.cfg

    [SysCom]
    Linux.Devicefile.1=/dev/ttyUSB0
    Linux.Devicefile.2=/dev/ttyS0

    I assume that you access the GPIO by:
    /dev/ttyS0

    In CODESYS later your USB serial is Com1 and GPIO Uart Com2

    BR
    Edwin

     
  • victorherma - 2020-06-18

    Hi Edwin,
    Thank you very much for the help and I hope you have enough patience to assist me ;-).
    I have modified the /etc/CODESYSControl_User.cfg file as directed.
    I understand that I must include in my CODESYS project, in the device tree, a USB device that is associated with / dev / ttyS0 through the COM2 address, but the devices that I can add are of type Modbus_COM_Port
    Are these equivalent to a COM device?
    Should I add a Modbus_Serial_Device or Modbus_Master_Device with a Modbus_Eslave_Device afterwards?
    Regards.
    Víctor

     
  • eschwellinger

    eschwellinger - 2020-06-19

    Hi,
    yes... but here in the config.

    BR
    Edwin

     
  • victorherma - 2020-07-06

    Hello again, Edwin.
    I feel like I need a little more help, because I still can't send an SMS from the application created with Codesys. I hope you can give me some other clue.
    I finally bought the IIoT library from the Codesys store and installed it on Codesys V3.5 SP16.
    I use the SendSMS function to try to send an SMS, but without success (error 5013?).
    I don't know if I need to use any of the other COM port activation functions, or the modem, etc. (Unfortunately I cannot find a user guide or detailed help for this library, its functions and an example).
    As an illustration, I attach the Python script that works correctly and sends the SMS from the Raspberry Pi console.
    Thank you very much.
    Regards,
    Victor.

     
  • eschwellinger

    eschwellinger - 2020-07-06

    Hi,
    if your modem support the follwoing at commands:
    Used AT-Commands
    AT+CMGS: Send Message
    AT+CMGR: Read Message
    AT+CMGL: List Messages
    AT+CMGD: Delete Message
    AT+CMGF: Message Format
    AT+CPMS: Preferred Message Storage
    AT+CSCA: Service Center Address
    AT+CMEE: Error Message Format
    AT+CREG: Network registration

    it might work.
    You need to add the comport here:

    /etc/CODESYSControl_Users.cfg

    [SysCom]
    Linux.Devicefile.1=/dev/ttyS0

    That's it... example project comes with the SMS package.
    Regards
    Edwin

     
  • victorherma - 2020-07-09

    Hi Edwin.
    Yes, the modem supports AT commands, but where do I write them? In a POU? How do I indicate the COM port of the UART?
    When I use the example included in the SMS Service SL library, modified for the raspberry, the TimeOut runs out on the first initialization of the modem and it goes into error. I suspect it has to do with the ModemFb.itfConnectionType

     
  • eschwellinger

    eschwellinger - 2020-07-09

    Check the modem documentaion... To Figuren Out which device on the Pi /dev/tty.... Ist used

     
  • TPTSys - 2020-07-10

    I'm having some trouble with the serial communication. I am using raspberry pi on rasbian with V3.5.16.0.

    The com port appears to start properly, and I even notice some activity on the usb serial port (it is connected to an arduino), but all reads and writes appear to be empty (despite reporting successful).

    cat /dev/ttyACM0 lists no activity.

    If i start the arduino serial port monitor first, before initializing the com port in codesys, everything works perfectly. All activity shows up in cat /dev/ttyACM0 and the arduino code responds (i have it flipping the LED when receiving a message).

    udiPort = 1
    Baud rate is 115200
    parity= None
    one stop bit
    udi timout 0
    udibytesyze=8
    udibinary=0

    stty -F /dev/ttyACM0:
    speed 115200 baud; line =0;
    min=1; time = 0;
    -brkint -icrnl -imaxbel
    -opost -onlcr
    -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

    CODESYSControl.cfg has:
    Linux.Devicefile=/dev/ttyACM

    Any other ideas?

     
  • TPTSys - 2021-02-26

    Sure! You might notice whenever you start the serial monitor in the arduino IDE the sketch reboots. Well I discovered the same thing happens when the serial port is opened on the raspberry pi in codesys. For some reason, if read or write commands are sent before the sketch restarts it's serial port again everything is lost.

    The solution is to open the port, then wait a bit (my program waits for 1 second). After 1 second, you can read/write and everything will work.

    Here is a portion of code I used, adapted from some other examples i found on the web for serial communication via codesys and raspberry pi.

    A couple of libraries used here
    CAA SerialCom 3.5.15.0
    CAA Types 3.5.13.0

    VAR
        (* Settings to communicate with the COM Port *)
        aCom1Params : ARRAY [1..7] OF COM.PARAMETER;
        como1 : COM.Open;
        comw1 : COM.Write;
        sWrite : STRING;
        szRead : CAA.SIZE;
    end_var
    
    
    CASE iState OF
        0:  //The parameters are set for the COM Port
            aCom1Params[1].udiParameterId := COM.CAA_Parameter_Constants.udiPort;
            aCom1Params[1].udiValue := 1; // the correct Port should be adapted
            aCom1Params[2].udiParameterId :=COM.CAA_Parameter_Constants.udiBaudrate;
            aCom1Params[2].udiValue := 57600;
            aCom1Params[3].udiParameterId :=COM.CAA_Parameter_Constants.udiParity;
            aCom1Params[3].udiValue := INT_TO_UDINT(COM.PARITY.NONE);
            aCom1Params[4].udiParameterId := COM.CAA_Parameter_Constants.udiStopBits;
            aCom1Params[4].udiValue := INT_TO_UDINT(COM.STOPBIT.ONESTOPBIT);
            aCom1Params[5].udiParameterId := COM.CAA_Parameter_Constants.udiTimeout;
            aCom1Params[5].udiValue := 0;
            aCom1Params[6].udiParameterId := COM.CAA_Parameter_Constants.udiByteSize;
            aCom1Params[6].udiValue := 8;
            aCom1Params[7].udiParameterId := COM.CAA_Parameter_Constants.udiBinary;
            aCom1Params[7].udiValue := 0;
    
            //The first Port is opened with the given parameters
            como1(xExecute := TRUE, usiListLength:=UINT_TO_USINT(SIZEOF(aCom1Params)/SIZEOF(COM.PARAMETER)),pParameterList:= ADR(aCom1Params));
    
            //wait a while before starting the write.
            tonComo1Done(IN:=como1.xDone,pt:=T#1000MS);
    
            IF tonComo1Done.Q THEN
                iState := 10;
            END_IF
    
            IF como1.xError THEN
                xCom1OpenError := TRUE;
                iState := 1000;
            END_IF
        10: //Write Data
            sWrite:='Hello World';
            comw1(xExecute:=True, como1.hcom, pBuffer:=ADR(sWrite), szSize:=Standard.LEN(sWrite);
            iState:=11;
        11: //Monitor the write process for completion
            comw1(xExecute:=FALSE);
            IF comw1.xError THEN
                xCom1WriteError := TRUE;
            END_IF
    
            IF comw1.xDone THEN
                iState := 15;
            END_IF
        1000:
            iState:=0;
            como1(xExecute:=false);
            comw1(xExecute:=false);
    else
        iState:=0;
    end_case
    

    I hope you get everything working! Let me know how it works out. This was a hard topic to find, most serial communication conversation revolves around using the GPIO pins. Using the USB port is very convenient since it powers the arduino and has the already active built in serial communication available.

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3
    Talk.ru: 5
    Talk.ru: 7

  • f-und-e - 2021-03-11

    Hello there, I have the strong feeling that I am going insane. I am trying to implement RS232 reading via an USB adapter on a RevPi (Raspberry). It worked perfectly fine until now. I was able to read the port but now it is not doing anything but giving all kinds of error codes (which are documented here: https://help.codesys.com/webapp/Errors;product=CmpErrors2_Itfs;version=3.5.15.0) but hardly ever the same one.

    I enabled the ttyUSB2 (which is used in my case) in the CODESYScontrol.cfg. I tried enabling it in the CODESYScontrol_User.cfg, I tried "LinuxDevicefile.1=/dev/ttyUSB2" and the line "portnum := COM.SysCom.SYS_COMPORT1" - absolutely nothing works.

    Here is my minimal implementation which used to work perfectly:

    PROGRAM PRG
    VAR
        bOpenCom : BOOL := TRUE;
        hCom: RTS_IEC_HANDLE := RTS_INVALID_HANDLE;
        csComSettings: COM_Settings;
        Result: RTS_IEC_RESULT;
    
        dwRead: UDINT;
        strRead: STRING;
        bReadCom: BOOL;
    
        bFlag: BOOL := TRUE;
        i: INT := 0;
        strInput:  ARRAY [0..200] OF STRING;
    
    END_VAR
    
    
    
    IF bOpenCom THEN
        bOpenCom := FALSE;
        // Open COM port 1
        hCom := SysComOpen(sPort:= SYS_COMPORT1 , pResult:= ADR(Result));
        IF hCom <> RTS_INVALID_HANDLE THEN
            // Configure the connection
            csComSettings.byParity := SYS_NOPARITY;
            csComSettings.byStopBits := SYS_ONESTOPBIT;
            csComSettings.sPort := SYS_COMPORT1;
            csComSettings.ulBaudrate := SYS_BR_9600;
            csComSettings.ulBufferSize := 0;
            csComSettings.ulTimeout := 10;
            Result := SysComSetSettings(hCom:= hCom, pSettings:= ADR(csComSettings) , pSettingsEx:= 0);
        END_IF
    
    
    END_IF
    
    IF hCom <> RTS_INVALID_HANDLE AND bReadCom THEN
        IF bFlag = TRUE THEN
            bFlag := FALSE;
            FOR i := 0 TO 200 BY 1 DO
                dwRead := SysComRead(hCom:= hCom,   pbyBuffer:= ADR(strRead),   ulSize:= 1, ulTimeout:= 100,    pResult:= ADR(Result));
                strInput[i] := strRead;
            END_FOR
        END_IF
    END_IF
    

    Even the SerialCom example from Codesys does not work. I am using the SysCom lib, because the CAA lib did not work at all for me. Any help is appreciated, thank you very much!

     
    • Ingo

      Ingo - 2021-03-11

      Hi! Sounds like your problem is a few layers below your application. So if
      it worked already, and now doesn't. I assume it is either a problem of:

      • the hardware
      • the USB cable
      • the linux driver
      • the CODESYS configuration

      To exclude the first two, you could test the adapter on another PC or
      device.

      To see if the linux driver works at all, you may test it when CODESYS is
      not running, with minicom or s.th. similar.

      In the CODESYS configuration I can only imagine, that:

      • the whole file is corrupted, and can't be read anymore (you should see
        this in the log)
      • the device name is not correct

      Good luck!

      Cheers,
      Ingo

       
      • f-und-e - 2021-03-15

        Hey Ingo, I am sorry for the late reply!

        I already ruled out the hardware and the cable part. I am able to read the data using cat /dev/ttyUSB0 - the driver seems to be working. It is no issue with my laptop aswell.
        The Codesys file seems correct aswell, I even rewrote it and compiled a completely new file... Maybe it is not the way to do it via Codesys directly - maybe it is easier to read the input with another device and send it to Codesys via some protocol (OPC UA, MQTT idk..). I need it to be as tight as possible on the long run.

        Cheers

         
<< < 1 .. 3 4 5 6 > >> (Page 5 of 6)

Log in to post a comment.