1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Industrial Robot Support and Discussion Center
  4. KUKA Robot Forum
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

KRC2Ed05 to Omron Safety PLC Serial Connection, Submit Interpreter stopping when transferring files

  • cnccreations
  • June 30, 2024 at 8:39 AM
  • Thread is Unresolved
  • cnccreations
    Reactions Received
    4
    Trophies
    3
    Posts
    9
    • June 30, 2024 at 8:39 AM
    • #1

    I setup a serial link a few years ago (with much research on this forum) between my KUKA krc2 ed05 (KSS 5.6.need to check the rest) and an Omron G9SP safety plc which handles the robot cell safety, some critical functions (not ejecting the tool while the spindle is running, not releasing the ATC when the tool is not in the nest etc) and gives some additional IO. The serial communications is handled through the submit interpreter in the sps.sub file.

    It has been running well EXCEPT sometimes when I'm copying or moving large files between the usb thumb drive and the robot controller it hangs. After this I have to change to T2 (or T1), cancel the submit interpreter then start it again.

    I've included a copy of the relavent sections of the sps.sub file below (declarations and the actual code which runs within the main loop of the sps file). Some of the variables not defined here are defined globally in the config.dat file.

    Code
    ;****************Serial Initializations*********************
    CheckSumFail=0
    Handle=3
    Offset=199
    Timeout=5.0
    TX_State={RET1 #CMD_OK,MSG_NO 0,HITS 1,LENGTH 0}
    RX_State={RET1 #DATA_END,MSG_NO 0,HITS 1,LENGTH 199}
    CH_State={RET1 #CMD_OK,MSG_NO 0,HITS 0,LENGTH 0}
    Modey=#ABS
    FOR I = 1 to 32
    	PLCOut[I] = FALSE
    ENDFOR
    MistOn=0
    ;**************End Serial Initializations*******************
    Display More
    Code
    ;-----------------------------------------------------------
    ; Serial module for communicating with Omron G9SP Safety PLC
    ;-----------------------------------------------------------
    ;********************Start Send Data************************
    SerialDebug=0
    SendString[]="'H40''H00''H00''H0F''H4B''H03''H4D''H00''H01'" ;Add PLC Header to SendString (9 characters)
    ;Compile the 32 Robot to PLC signals into 4 bytes of data to send to the PLC and add them to SendString
    SerialDebug=1
    K=1
    FOR I = 1 to 4
    SerialDebug=2
    	CharValue=0
    	FOR J = 1 to 8
    		IF PLCOut[J+((I-1)*8)] THEN
    SerialDebug=3
    			CharValue=CharValue+K
    		ENDIF
    		K=K*2		
    	ENDFOR
    	SendString[I+9]=CharValue
    SerialDebug=4
    	K=1
    ENDFOR
    SerialDebug=5
    ;Add the two additional dummy bytes to the string, should now be 15 characters long
    FOR I = 1 to 2
    	SendString[I+13] = "'H00'"  
    ENDFOR
    SerialDebug=6
    ;Calculate Check Sum on first 15 characters of SendString
    CheckSumCalc=0
    FOR I = 1 TO 15
    	CharValue = (SendString[I])
    	CheckSumCalc = CheckSumCalc + CharValue
    ENDFOR
    SerialDebug=7
    CheckSum[1]=(CheckSumCalc B_AND 'B1111111100000000')/256 ;Upper byte of CheckSum
    SerialDebug=8
    CheckSum[2]=CheckSumCalc B_AND 'B0000000011111111';Lower byte of CheckSum
    SerialDebug=9
    ;Finish building string to send
    FOR I = 1 to 2
    	SendString[I+15] = CheckSum[I]  ;Add the CheckSum to the SendString, should now be 17 characters long
    ENDFOR
    SerialDebug=10
    SendString[18]="'H2A'" ;Add 2 footer characters to string
    SendString[19]="'H0D'" ;should now be the full 19 characters long
    SerialDebug=11
    COPEN (:SER_3, Handle) ; open serial port COM3
    SerialDebug=12
    CWRITE (Handle, TX_State, Modey, "%r", SendString[]) ; send TX string to G9SP
    SerialDebug=13
    ;*********************End Send Data*************************
    ;*******************Start Receive Data**********************
    Modey = #ABS ; CREAD waits for data or timeout
    SerialDebug=14
    WAIT FOR ($DATA_SER3 <> 0) ; wait for data in COM3 buffer
    SerialDebug=15
    offset=0 ; start from string first character
    CREAD (Handle, RX_State, Modey, Timeout, Offset, "%r", Response[]) ; read serial buffer into RX string
    SerialDebug=16
    CCLOSE (Handle, CH_State) ; close serial channel
    SerialDebug=17
    ;Calculate CheckSum on first 195 bytes
    CheckSumCalc=0
    FOR I = 1 to 195
     SerialDebug=18
    	CharValue = (Response[I])
    	CheckSumCalc = CheckSumCalc + CharValue
    ENDFOR
    SerialDebug=19
    ;Read CheckSum bytes and compare to calculated value.
    CharValue = Response[196]
    CheckSumRet = CharValue*256
    SerialDebug=20
    CharValue = Response[197]
    CheckSumRet = CheckSumRet + CharValue
    SerialDebug=21
    IF CheckSumRet <> CheckSumCalc THEN
    	CheckSumFail = CheckSumFail+1
    	IF CheckSumFail == 5 THEN
      SerialDebug=22
    		;Halt ;Communications with PLC has failed 5 times in a row, so stop program
    	ENDIF
    ELSE
    SerialDebug=23
    	;Extract data in bytes 8 to 11 and map them to the PLCIn PLC to Robot Inputs
    	CheckSumFail = 0
    	K=1
    	FOR I = 1 to 4
    		FOR J = 1 to 8
    			IF Response[7+I] B_AND K >0 THEN
    				PLCIn[J+(I-1)*8]=TRUE
    			ELSE
    				PLCIn[J+(I-1)*8]=FALSE
    			ENDIF
    			K=K*2		
    		ENDFOR
    		K=1
    	ENDFOR
    ENDIF 
    SerialDebug=24
    ;**********************End Receive Data*********************
    ;*********************End Serial Module*********************
    Display More

    I added the integer SerialDebug to find where it was hanging, and it's value is 14 so it's on the following line:

    Code
    WAIT FOR ($DATA_SER3 <> 0) ; wait for data in COM3 buffer

    I think basically the copying of large files messes up the timing of the serial port sending data to the Omron PLC. As such, the PLC doesn't respond. As such I'd like to add some sort of a timeout, so if nothing has been received in the COM3 buffer after say 500ms, it skips the receive data section and tries sending again. My first thought is to use a WHILE loop that also includes a small delay and counter to keep checking the buffer, and abort if it takes to long, but would like to know what best practice is given it's in the submit interpreter in terms of keeping loop time down etc. Would welcome any thoughts on optimising other sections of the code too (my background definitely isn't in programming...) or if there is a completely different way I should be doing this. Hopefully the code may be of help to others looking to implement serial comms too.

    Edited once, last by cnccreations (June 30, 2024 at 1:52 PM).

  • kwakisaki June 30, 2024 at 9:04 AM

    Approved the thread.
  • panic mode
    Reactions Received
    1,295
    Trophies
    11
    Posts
    13,130
    • June 30, 2024 at 5:14 PM
    • #2

    Using WAIT command in Submit is a bad idea. And leaving it unchecked is as bad as it gets.

    At least add timer limit so it does not get stuck indefinitely

    WAIT FOR ($DATA_SER3 <> 0) or TIMER_LIMIT(1.75) ; wait for data in COM3 buffer but not more than 1.75 seconds

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • cnccreations
    Reactions Received
    4
    Trophies
    3
    Posts
    9
    • July 1, 2024 at 5:53 AM
    • #3

    Thankyou panic mode that's a big help.

    I hadn't come across the TIMER_LIMIT function before (but have now found reference to it here on the forum in the hidden functions thread).

    I've added that in and made a few other adjustments, and appears to be working well now. Have gone through several large file copies/moves without any issue. I'll include the updated code below in case it is of use to anyone looking to implement a serial connection.

    Declarations:

    Code
    ;**************Serial Module Declarations*******************
    DECL INT CharValue, Handle, Offset, CheckSumCalc, CheckSumRet, SerialFail, I, J, K
    DECL CHAR Comma, CheckSum[2], SendString[19], Response[200]
    DECL REAL Timeout
    DECL STATE_T TX_State, RX_State, CH_State
    DECL MODUS_T Modey
    ;************End Serial Module Declarations*****************

    Initializations:

    Code
    ;****************Serial Initializations*********************
    SerialFail=0
    Handle=3
    Offset=199
    Timeout=5.0
    TX_State={RET1 #CMD_OK,MSG_NO 0,HITS 1,LENGTH 0}
    RX_State={RET1 #DATA_END,MSG_NO 0,HITS 1,LENGTH 199}
    CH_State={RET1 #CMD_OK,MSG_NO 0,HITS 0,LENGTH 0}
    Modey=#ABS
    FOR I = 1 to 32
    	PLCOut[I] = FALSE
    ENDFOR
    MistOn=0
    ;**************End Serial Initializations*******************
    Display More

    Main Code:

    Code
    ;-----------------------------------------------------------
    ; Serial module for communicating with Omron G9SP Safety PLC
    ;-----------------------------------------------------------
    ;********************Start Send Data************************
    SerialDebug=0
    SendString[]="'H40''H00''H00''H0F''H4B''H03''H4D''H00''H01'" ;Add PLC Header to SendString (9 characters)
    ;Compile the 32 Robot to PLC signals into 4 bytes of data to send to the PLC and add them to SendString
    SerialDebug=1
    K=1
    FOR I = 1 to 4
    SerialDebug=2
    	CharValue=0
    	FOR J = 1 to 8
    		IF PLCOut[J+((I-1)*8)] THEN
    SerialDebug=3
    			CharValue=CharValue+K
    		ENDIF
    		K=K*2		
    	ENDFOR
    	SendString[I+9]=CharValue
    SerialDebug=4
    	K=1
    ENDFOR
    SerialDebug=5
    ;Add the two additional dummy bytes to the string, should now be 15 characters long
    FOR I = 1 to 2
    	SendString[I+13] = "'H00'"  
    ENDFOR
    SerialDebug=6
    ;Calculate Check Sum on first 15 characters of SendString
    CheckSumCalc=0
    FOR I = 1 TO 15
    	CharValue = (SendString[I])
    	CheckSumCalc = CheckSumCalc + CharValue
    ENDFOR
    SerialDebug=7
    CheckSum[1]=(CheckSumCalc B_AND 'B1111111100000000')/256 ;Upper byte of CheckSum
    SerialDebug=8
    CheckSum[2]=CheckSumCalc B_AND 'B0000000011111111';Lower byte of CheckSum
    SerialDebug=9
    ;Finish building string to send
    FOR I = 1 to 2
    	SendString[I+15] = CheckSum[I]  ;Add the CheckSum to the SendString, should now be 17 characters long
    ENDFOR
    SerialDebug=10
    SendString[18]="'H2A'" ;Add 2 footer characters to string
    SendString[19]="'H0D'" ;should now be the full 19 characters long
    SerialDebug=11
    COPEN (:SER_3, Handle) ; open serial port COM3
    SerialDebug=12
    CWRITE (Handle, TX_State, Modey, "%r", SendString[]) ; send TX string to G9SP
    SerialDebug=13
    ;*********************End Send Data*************************
    ;*******************Start Receive Data**********************
    Modey = #ABS ; CREAD waits for data or timeout
    SerialDebug=14
    WAIT FOR ($DATA_SER3 <> 0) OR TIMER_LIMIT(1.0) ; wait for data in COM3 buffer
    IF $DATA_SER3 == 0 THEN
    	SerialFail = SerialFail+1
    	CCLOSE (Handle, CH_State) ; close serial channel
    ELSE
    	SerialDebug=15
    	offset=0 ; start from string first character
    	CREAD (Handle, RX_State, Modey, Timeout, Offset, "%r", Response[]) ; read serial buffer into RX string
    	SerialDebug=16
    	CCLOSE (Handle, CH_State) ; close serial channel
    	SerialDebug=17
    	;Calculate CheckSum on first 195 bytes
    	CheckSumCalc=0
    	FOR I = 1 to 195
    	SerialDebug=18
    		CharValue = (Response[I])
    		CheckSumCalc = CheckSumCalc + CharValue
    	ENDFOR
    	SerialDebug=19
    	;Read CheckSum bytes and compare to calculated value.
    	CharValue = Response[196]
    	CheckSumRet = CharValue*256
    	SerialDebug=20
    	CharValue = Response[197]
    	CheckSumRet = CheckSumRet + CharValue
    	SerialDebug=21
    	IF CheckSumRet <> CheckSumCalc THEN
    		SerialFail = SerialFail+1
    		IF SerialFail == 5 THEN
    			SerialDebug=22
    			MsgNotify("Serial Comms to PLC failed 5 consecutive times")
    		ENDIF
    	ELSE
    	SerialDebug=23
    		;Extract data in bytes 8 to 11 and map them to the PLCIn PLC to Robot Inputs
    		SerialFail = 0 ; Serial comms was successful in sending and receiving
    		K=1
    		FOR I = 1 to 4
    			FOR J = 1 to 8
    				IF Response[7+I] B_AND K >0 THEN
    					PLCIn[J+(I-1)*8]=TRUE
    				ELSE
    					PLCIn[J+(I-1)*8]=FALSE
    				ENDIF
    				K=K*2		
    			ENDFOR
    			K=1
    		ENDFOR
    	ENDIF 
    ENDIF
    SerialDebug=24
    ;**********************End Receive Data*********************
    ;*********************End Serial Module*********************
    Display More
  • hermann
    Reactions Received
    411
    Trophies
    9
    Posts
    2,621
    • July 1, 2024 at 6:05 AM
    • #4

    You wait for $data_ser3 <>0, then read until buffer is empty, then use exact 195 bytes that you hope where new read. But you don't check whether really 195 bytes where received. OK, the checksum can reveal faulty reading, so it's kind of bullet proof.

  • cnccreations
    Reactions Received
    4
    Trophies
    3
    Posts
    9
    • July 1, 2024 at 6:32 AM
    • #5

    Thanks hermann that's a good point, I could probably wait for $DATA_SER3==197 instead as the response from the PLC should always be the same length and this would avoid any cases where characters are missing, although the checksum should pick this up. My only thought is this may slow the submit loop slightly, as it is waiting until all the data has been received before it starts processing it, although I suspect the difference is negligible.

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Tags

  • omron
  • PLC
  • Submit Interpreter
  • serial
  • krc2 ed05
  • plc to controller

Users Viewing This Thread

  • 1 Guest
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download