Codesys service disables serial communication

NiekW
2018-01-26
2018-01-29
  • NiekW - 2018-01-26

    Hi all,

    I'm very new to codesys (and plc programming), so I'm not only looking for a solution but some explanation would be very welcome

    I've installed codesyscontrol service on my Pi, and followed some simple tutorial for making a LED blink with webvisu. (this works fine)
    I also have an RFID module attached to the Pi, and (for now) I handle serial communication with a Python script. (later I would like to use the serial data from RFID inside codesys application, but like I said first time with codesys so 1 step at a time)

    However, when the codesyscontrol.service is active ( $ systemctl | grep codesys) I cannot read serial data with the Python script anymore. I figured this was because the Pi only has 1 UART, so I got an FTDI USB serial port adapter to handle the serial communication with the RFID module. I edit the Python script to use /tty/USB0 instead of /tty/AMA0, but I have the same problem: when I stop the codesys service everything works fine, as soon as I start codesys service it stops working.

    I've gone through the forum and I've tried adding

    [SysCom]
    Linux.Devicefile=/dev/ttyUSB

    to CODESYScontrol.cfg but this doesnt fix my problem.

    So some questions...
    1: Can someone explain why codesyscontrol.service would cause this problem?
    2: What exactly does
    [SysCom]
    Linux.Devicefile=/dev/ttyUSB
    do? I suspect it points codesys to the correct hardware address for serial communication?
    3: In some forum posts I see
    portnum := COM.SysCom.SYS_COMPORT1
    added to the .cfg, what does this do?
    4: Final question, how do I fix my problem?

    Thank you in advance.

     
  • NiekW - 2018-01-29

    This is the python code that stops working when codesys service is active. Any ideas how to get both of them working in parallel?

    \#! /usr/bin/env python3
    import RPi.GPIO as GPIO
    import serial
    ENABLE_PIN  = 18              # The BCM pin number corresponding to GPIO1
    SERIAL_PORT = '/dev/ttyUSB0'  # The location of our serial port.  This may
                                  # vary depending on OS and RPi version.  The
    \#SERIAL_PORT = '/dev/ttyS0'   # RPi 3 has apparently used 'ttyAMA0' for
                                  # Bluetooth and assigned 'ttyS0' to the GPIO
                                  # serial port, so uncomment the appropriate
                                  # SERIAL_PORT definition for your setup.
                                  # Failing that, check the output of:
                                  #   $ dmesg | grep serial
                                  # to get an idea as to where serial has been
                                  # assigned to.
                                  
    def validate_rfid(code):
        # A valid code will be 12 characters long with the first char being
        # a line feed and the last char being a carriage return.
        s = code.decode("ascii")
        if (len(s) == 12) and (s[0] == "\n") and (s[11] == "\r"):
            # We matched a valid code.  Strip off the "\n" and "\r" and just
            # return the RFID code.
            return s[1:-1]
        else:
            # We didn't match a valid code, so return False.
            return False
        
    def main():
        # Initialize the Raspberry Pi by quashing any warnings and telling it
        # we're going to use the BCM pin numbering scheme.
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        # This pin corresponds to GPIO1, which we'll use to turn the RFID
        # reader on and off with.
        GPIO.setup(ENABLE_PIN, GPIO.OUT)
        # Setting the pin to LOW will turn the reader on.  You should notice
        # the green LED light on the reader turn red if successfully enabled.
        print("Enabling RFID reader and reading from serial port: " + SERIAL_PORT)
        GPIO.output(ENABLE_PIN, GPIO.LOW)
        # Set up the serial port as per the Parallax reader's datasheet.
        ser = serial.Serial(baudrate = 2400,
                            bytesize = serial.EIGHTBITS,
                            parity   = serial.PARITY_NONE,
                            port     = SERIAL_PORT,
                            stopbits = serial.STOPBITS_ONE,
                            timeout  = 1)
        # Wrap everything in a try block to catch any exceptions.
        try:
            # Loop forever, or until CTRL-C is pressed.
            while 1:
                # Read in 12 bytes from the serial port.
                data = ser.read(12)
                # Attempt to validate the data we just read.
                code = validate_rfid(data)
                # If validate_rfid() returned a code, display it.
                if code:
                    print("Read RFID code: " + code);
        except:
            # If we caught an exception, then disable the reader by setting
            # the pin to HIGH, then exit.
            print("Disabling RFID reader...")
            GPIO.output(ENABLE_PIN, GPIO.HIGH)
            
    if __name__ == "__main__":
        main()
        
    
     

    Related

    Talk.ru: 11


Log in to post a comment.