More stuff here. This time the Diagnostic/In progress version on my Cubloc code.
I'll update this as I make significant alterations to it. But might come in handy to anyone starting out or struggling with the Cubloc PLC's.
Mostly in Cubloc basic. Some ladder will be added later.
Also a pic of the cubloc study board just about fully loaded with every switch and Led used up to test the logic on the desk. Everything has been connected to match the IO assignments on the CUSB36R PLC.
The code below has been updated here a fair bit. BUT BE AWARE is has a lot of diagnostic code included. e.g the Pendant stuff which enabled me to test the toolchange routine when not connected to Mach. I ran out of inputs and had to use the Estop input for the 4th button! This obviously is not staying like that as I have no use for the pendant on the real machine.
Wayne....

'Hardinge HXL-S PLC code for CUSB36R
Const Device = CB280
Set Display 2,0,1,50
Opencom 1,115200,3,80,80
Set Modbus 1,1,20
Cls
Wait 1500
Locate 3,0
Print "HARDINGE HXL-S"
Locate 1,1
Print "FDOS Design 2008"
Locate 0,3
Print "PLC Firmware V0.08"
Csroff
Wait 1500
Cls
Csroff
Dim cl As String 'Clear Line
Dim spin As String 'Spindle Status (CW, CCW, OFF)
Dim sro As Integer 'Spindle Override Pot (% FSD)
Dim fro As Integer 'Fedrate Override Pot (% FSD)
Dim station As Integer 'Decoded Turret Station #
Dim estop As Integer 'EStop Chain Status
Dim status_page As Integer 'Current Page (LCD Display)
Dim req_tool As Integer 'Requested Tool
Dim turret_encoder As Integer 'Encoder value (all 4 bits)
Dim pendant As Integer
Dim settle_time As Long
'*******************************************
'* RELAY OUPUTS *
'*******************************************
Usepin 4,Out,coolant 'Coolant
Usepin 5,Out,spindle_cw 'Spindle CW
Usepin 6,Out,spindle_ccw 'Spindle CCW
Usepin 7,Out,spindle_lock 'Spindle Lock Disengage
Usepin 16,Out,collet_open 'collet open
Usepin 17,Out,collet_close 'collet close
Usepin 22,Out,tstock_fwd 'tailstock forward
Usepin 23,Out,tstock_ret 'tailstock retract
Usepin 40,Out,cush_ret 'air cushion retract
Usepin 41,Out,cush_eng 'air cushion engage
Usepin 42,Out,index 'Turret Up and Index
Usepin 43,Out,lube_pump 'Lube Pump
Usepin 44,Out,sys_enable 'System Enable
'*******************************************
'* ANALOGUE INPUTS *
'*******************************************
Input 24 'Feedrate Override
Input 25 'Spindle Override
'*******************************************
'* 'OPTO Inputs *
'*******************************************
Usepin 10,In,turret_down 'turret down
Usepin 11,In,index_complete 'index complete
Usepin 12,In,spindle_locked 'spindle locked
Usepin 13,In,tstock_inuse 'tailstock in Use
Usepin 28,In,tur_enc1 'turret station 1
Usepin 29,In,tur_enc2 'turret station 2
Usepin 30,In,tur_enc3 'turret station 3
Usepin 31,In,tur_enc4 'turret station 4
Usepin 32,In,low_coolant 'low coolant switch
Usepin 33,In,low_collet 'low collet pressure
Usepin 34,In,low_air 'low air pressure
Usepin 35,In,emg_stop 'EMERGENCY STOP!
Usepin 36,In 'Page Select
Usepin 37,In,pendant1 'spare
Usepin 38,In,pendant2 'spare
Usepin 39,In,pendant3 'spare
'*******************************************
'* startup defaults *
'*******************************************
cl = " " '20 Spaces (clear line)
status_page = 0
settle_time = 250 'Air valve settle time in milliseconds
Set Ladder On
Wait 10 'Allow time for ladder to start
'*******************************************
'* Bootup Operations *
'*******************************************
Do While low_air = 0 'check low air switch is closed before proceeding
sys_enable = 0 'Open PLC estop relay
Gosub error_title
Gosub low_air_error
Wait 1000
Locate 0,0
Print cl 'Flash ERROR! msg 1 sec interval
Wait 1000
Loop
Cls
Csroff
Locate 2,0
Print "Reseting Startup"
Locate 6,2
Print "Defaults"
coolant = 0 'coolant off
spindle_cw = 0 'spindle cw off
spindle_ccw = 0 'spindle ccw off
Do While spindle_locked = 0 'read spindle locked switch
spindle_lock = 1 'disengage spindle lock
Loop
Wait settle_time
spindle_lock = 0
cush_ret = 1 'retract air cushion
Wait settle_time
cush_ret = 0
cush_eng = 1 'engage air cushion
Wait settle_time
cush_eng = 0
Wait 1000
start:
'*******************************************
'* Main Loop *
'*******************************************
Do
Gosub scan_page
_d(13) = station 'load current station # into d13
req_tool = _D(12) 'read data register 12 for requested tool
Gosub scan_pendant
If req_tool > 0 Then
Gosub index_turret
End If
_D(10) = Adin(0) 'Read ADC0 Feedrate overide load into D10
fro = _D(10)/10.21
_D(11) = Adin(1) 'Read ADC0 Spindle overide load into D11
sro = _D(11)/10.21
estop = In(35)
Csroff 'Cursor Off
Loop
'*******************************************
'* Sub Routines *
'*******************************************
page0:
Gosub decode_turret
Locate 0,0
Print "P", Dec status_page," (Turret Status) "
Locate 0,1
Print "Enc 1-2-3-4 Req = ",Dec req_tool," "
Locate 0,2
Print "Act ",Dec tur_enc1,"-",Dec tur_enc2,"-",Dec tur_enc3,"-",Dec tur_enc4
Locate 11,2
If station = 0 Then
Print " : Error!"
Else
Print " Stn = ",Dec station," "
End If
Locate 0,3
Print "Idx Cmp ",Dec In(11)," Tur Dwn ",Dec In(10)
Return
page1:
Locate 0,0
Print "P",Dec status_page," (Warning Inputs)"
Locate 0,1
Print "ESTOP ",Dec In(35)," Low Air ", Dec In(34)
Locate 0,2
Print "Low Collet Press ",Dec In(33)," "
Locate 0,3
Print "Low Coolant ",Dec In(32)," "
Return
page2:
Locate 0,0
Print "P",Dec status_page," (Analogue Inputs)"
Locate 0,1
Print "FRO ",Dp(fro,3),"%"," SRO ",Dp(sro,3),"%"
Locate 0,2
Print cl
Locate 0,3
Print cl
Return
page3:
Gosub scan_pendant
Locate 0,0
Print "P",Dec status_page," (Blank) "
Locate 0,1
Print "spindle locked ",Dec spindle_locked," "
Locate 0,2
Print "turret down ",Dec turret_down
Locate 0,3
Print "Index Complete ",Dec index_complete
Return
error_title:
Locate 7,0
Print "ERROR!"
Return
low_air_error:
Locate 2,2
Print "LOW AIR PRESSURE"
Locate 2,3
Print "SYSTEM INHIBITED"
Return
scan_page:
If Keyinh(36,250) Then
status_page = status_page + 1
Endif
If status_page >3 Then
status_page = 0
Endif
Select Case status_page
Case 0
Gosub page0
Case 1
Gosub page1
Case 2
Gosub page2
Case 3
Gosub page3
End Select
Return
scan_pendant: 'Temporary routine for testing toolchange via pendant
pendant = (pendant1 *

+ (pendant2 * 4) + (pendant3 * 2) + emg_stop 'Convert to Pseudo Binary
Select Case pendant
Case &b1000
req_tool = 1
Case &b0100
req_tool = 2
Case &b0010
req_tool = 3
Case &b0001
req_tool = 4
Case Else
req_tool = 0
End Select
Return
index_turret:
If req_tool >4 Then
Return
Endif
Cls
Wait 50
Locate 0,0
Print "Toolchange Requested"
Do
If station = req_tool Then
Exit Do
Endif
Locate 0,1
Print "Requested Station= ",Dec req_tool
Locate 0,2
Print " Current Station= ",Dec station
Locate 0,3
Print "Idx Comp ",Dec index_complete," Tur Dwn ",Dec turret_down
Csroff
Do Until index_complete = 1
index = 1
Locate 0,3
Print "Idx Comp ",Dec index_complete," Tur Dwn ",Dec turret_down
Csroff
Loop
Do Until turret_down = 1
index = 0
Locate 0,3
Print "Idx Comp ",Dec index_complete," Tur Dwn ",Dec turret_down
Csroff
Loop
Gosub decode_turret
If station = 0 Then
Goto turret_failure
Endif
Loop
Return
decode_turret:
turret_encoder = (tur_enc1 *

+ (tur_enc2 * 4) + (tur_enc3 * 2) + tur_enc4 'Convert to Pseudo Binary
Select Case turret_encoder
Case &b0111
station = 1
Case &b1011
station = 2
Case &b1101
station = 3
Case &b1110
station = 4
Case Else
station = 0
End Select
Return
turret_failure:
Cls
Gosub error_title
Locate 1,2
Print "Toolchange Failure"
Csroff
sys_enable = 0 'Generate Estop
Wait 5000
Goto start 'Temporary restart
Function status(raw As Byte)As String
If raw = 1 Then
status = "ON "
Else
status = "OFF"
Endif
End Function