ScriptAuthor "Deutschmann Automation GmbH" ScriptName "FB-FB auto detect" ScriptVersion "V 0.1t" // V 1.0 start script // This script realized a script for every filedbus-fieldbus gateway // where one side is fieldbus // Errorhandler init => After Error, script will automatic jump to "Errorhandler" SetErrorHandler ( :ErrorHandler ) ; // Declaration of Variable // VA_=Variable Array, VW_=Variable Word, VB_=Variable Byte Var VA_BusInput : BUFFER[32] ; // max.Fieldbus-Length Var VA_BusOutput : BUFFER[32] ; // max.Fieldbus-Length Var VW_BusInputLen: Word ; Var VW_BusOutputLen: Word ; Var Vb_BusInputLen: Byte ; Var Vb_BusOutputLen: Byte ; Var VW_Timeout_ms: Word ; MoveConst (VW_Timeout_ms , 2) ; // 2ms Timeout => Wartezeit zwischen Datenblöcken Var VW_Const_0: Word ; MoveConst (VW_Const_0, 0); Var VW_Const_1: Word ; MoveConst (VW_Const_1, 1); Var Vb_Const_4: Byte ; MoveConst (Vb_Const_4, 4); var vb_HEX0xC0: Byte ; MoveConst (vb_HEX0xC0, 0xC0); var vb_HEX0x3F: Byte ; MoveConst (vb_HEX0x3F, 0x3F); Var Vb_Const_63: Byte ; MoveConst (Vb_Const_63, 63); Var Vb_Const_64: Byte ; MoveConst (Vb_Const_64, 64); Var Vb_Const100: Byte ; MoveConst (Vb_Const100, 100); Var Vb_Const_126: Byte ; MoveConst (Vb_Const_126, 126); Var Vb_Const_128: Byte ; MoveConst (Vb_Const_128, 128); Var Vb_Const_255: Byte ; MoveConst (Vb_Const_255, 255); Var vw_DestOffset: Word ; Var vb_ReturnCode: Byte ; Var VB_ErrorNr: Byte ; Var VB_Const_0: Byte ; MoveConst (VB_Const_0, 0) ; // Returncode 0 = No Error Var VB_Const_1: Byte ; MoveConst (VB_Const_1, 1) ; Var VB_BusLen: Byte ; Var VB_Const_0FH: Byte ; MoveConst (VB_Const_0FH, 0x0F); Var VB_Const_20H: Byte ; MoveConst (VB_Const_20H, 0x20); Var vb_TempB: Byte ; Var vb_rx_char: Byte ; Var vb_NewBusData: Byte ; Var vb_fieldbusID: Byte ; Var vl_fieldbusID: Long ; Var vb_tempnibble: Byte ; Var vb_temp : Byte ; var TRUE : byte ; moveconst ( TRUE , 1 ) ; var vb_bus_type : byte ; Get ( Bus_Type , vb_bus_type ) ; var vb_script_rev : byte ; Get ( ScriptRevision , vb_script_rev ) ; var IC : byte ; var SC_TRUE : byte ; // detect of SC or IC call : Init_Hardware_Type ; // Init serial port Set ( Baudrate , 62500); Set ( Databits , 8 ) ; Set ( Startbits , 1 ) ; Set ( Stopbits , 1 ) ; Set ( Parity , none ) ; MoveConst (VB_ErrorNr, 0x10 ) ; // 1xH = grün, x0H = All yellow LED's off Call :ErrorDisplay ; // Start Bus and wait until running BusStart ; Wait ( Bus_Active ) ; Get ( BusInputSize , Vb_BusInputLen ) ; Get ( BusOutputSize , Vb_BusOutputLen ) ; call : TestMode ; // Check if Busdata available :CheckNewBusData ; Get ( BusDataChanged , vb_NewBusData ) ; if vb_NewBusData equal vb_Const_0 then :NoBusData else :CopyBusDataToRS ; :CopyBusDataToRS ; ReadBus ( VA_BusInput[0] , Vb_BusInputLen ) ; SendRS ( VA_BusInput[0] , Vb_BusInputLen ) ; // State-LED = green; Yellow-LED's = Bus-Receive-Data-Len MoveConst (VB_ErrorNr, 0x10 ) ; // 1xH = grün, x0H = All yellow LED's off Convert ( vw_BusInputLen , Word , vb_TempB , Byte ) ; CalculateByte ( vb_TempB , VB_Const_0FH , and , VB_BusLen ) ; //CalculateByte ( vb_const_1 , VB_BusLen , add , VB_BusLen ) ;// increment the 4 yellow Led´s every bus receive CalculateByte ( VB_ErrorNr , VB_BusLen , add , VB_ErrorNr ) ; Call :ErrorDisplay ; // Check if RS-Data available :NoBusData ; MoveConst(vw_DestOffset, 0); :ReadRS ; // Read always one char until timeout ReceiveSomeCharRS ( vw_Timeout_ms , vb_rx_char , vw_const_1) ; Get ( ErrorCode , vb_ReturnCode ) ; if vb_ReturnCode equal vb_Const_0 then :SaveRxChar else :CopyRSDataToBus ; :SaveRxChar ; CopyIndexed ( vw_const_1 , vb_rx_char , vw_const_0 , va_BusOutput[0] , vw_DestOffset ) ; CalculateWord ( vw_DestOffset , vw_const_1 , add , vw_DestOffset ) ; jump :ReadRS ; //Get ( NumberRSInputCharacter , vb_TempB ) ; //Convert ( vb_TempB , Byte , VW_BusOutputLen , Word ) ; //if VW_BusOutputLen equal VW_Const_0 then :NoRSData else :CopyRSDataToBus ; :CopyRSDataToBus ; //ReceiveSomeCharRS ( VW_Timeout_ms , VA_BusOutput[0] , vw_DestOffset ) ; WriteBus ( VA_BusOutput[0] , vw_DestOffset ) ; :NoRSData ; jump :CheckNewBusData ; // The following code will automaticly executed after a scriptcommand returns with error :ErrorHandler ; //MoveConst (VB_ErrorNr, 0x21 ) ; // xyH: x = 2 => rot, y = ErrorCode Get ( ErrorCode , vb_ReturnCode ) ; CalculateByte ( vb_ReturnCode , VB_Const_0FH , and , vb_ReturnCode ) ; CalculateByte ( vb_ReturnCode , VB_Const_20H , add , VB_ErrorNr ) ; Call :ErrorDisplay ; Return ; // Display "ErrorNr" on bicolor and yellow LEDs :ErrorDisplay ; WriteShiftRegister ( VB_ErrorNr ) ; Return ; //***************************** : Init_Hardware_Type ; var GATEWAY_ICPB_SC_NR : byte ; moveconst (GATEWAY_ICPB_SC_NR, 273 ) ; var GATEWAY_ICDN_SC_NR : byte ; moveconst (GATEWAY_ICDN_SC_NR, 274 ) ; var GATEWAY_ICCO_SC_NR : byte ; moveconst (GATEWAY_ICCO_SC_NR, 275 ) ; var GATEWAY_ICIB_SC_NR : byte ; moveconst (GATEWAY_ICIB_SC_NR, 276 ) ; var GATEWAY_ICEN_SC_NR : byte ; moveconst (GATEWAY_ICEN_SC_NR, 277 ) ; var GATEWAY_ICFE_SC_NR : byte ; moveconst (GATEWAY_ICFE_SC_NR, 278 ) ; var GATEWAY_ICLN_SC_NR : byte ; moveconst (GATEWAY_ICLN_SC_NR, 279 ) ; if vb_bus_type greater vb_const100 then : Set_IC_TRUE else :Set_SC_TRUE; : Set_IC_TRUE ; moveconst (IC, 1); moveconst (SC_TRUE, 0); jump : End_Check_Type ; : Set_SC_TRUE ; moveconst (SC_TRUE, 1); moveconst (IC, 0); jump : End_Check_Type ; : End_Check_Type ; if IC equal TRUE then : Set_IC_Init else : Check_SC ; : Set_IC_Init ; call : InitIC ; jump : End_Init_Hardware ; : Check_SC ; : End_Init_Hardware ; return ; //***************************** //***************************** : InitIC ; // mit Return zurück Set ( ShiftRegisterOutputType , RiseClock_RiseLoad ) ; Set ( ShiftRegisterOutputBitLength , 8 ) ; // IC Fieldbus ID init Set ( ShiftRegisterInputType , RiseClock_LowLoad ) ; Set ( ShiftRegisterInputBitLength , 8 ) ; ReadShiftRegister ( vb_fieldbusID ) ; CalculateByte ( vb_fieldbusID , vb_const_1 , not , vb_fieldbusID ) ; if vb_bus_type equal GATEWAY_ICCO_SC_NR then :Set_CO_Init else :Check_DP_IC ; : Check_DP_IC ; if vb_bus_type equal GATEWAY_ICPB_SC_NR then :Set_PB_Init else :Check_DN_IC ; : Check_DN_IC ; if vb_bus_type equal GATEWAY_ICDN_SC_NR then :Set_DN_Init else :Check_DN_IC ; : Check_IB_IC ; if vb_bus_type equal GATEWAY_ICIB_SC_NR then :Set_IB_Init else :Check_EN_IC ; : Check_EN_IC ; if vb_bus_type equal GATEWAY_ICEN_SC_NR then :Set_EN_Init else :No_more_Fieldbus_Check ; : No_more_Fieldbus_Check ; return ; //******************************* : Set_CO_Init ; Set ( BusInputSize , 8 ) ; Set ( BusOutputSize , 8 ) ; :CO_SwapNibble; Copy ( vb_fieldbusID , vb_tempnibble , vb_const_1 ) ; Copy ( vb_tempnibble , vb_temp , vb_const_1 ) ; // make a copy CalculateByte ( vb_tempnibble , vb_HEX0x3F , and , vb_tempnibble ) ; // ID Mask CalculateByte ( vb_temp , vb_HEX0xC0 , and , vb_temp ) ; // shift Low Nibble to High Nibble Copy ( vb_tempnibble , vb_fieldbusID , vb_const_1 ) ; call : CO_SetFieldbusID ; call : CO_SetBaudrate ; return ; // return von InitIC //**************************** //******************************* : Set_IB_Init ; Set ( BusInputSize , 8 ) ; Set ( BusOutputSize , 8 ) ; call : IB_SetFieldbusID ; call : IB_SetBaudrate ; return ; // return von InitIC //**************************** //******************************* : Set_EN_Init ; call : EN_SetFieldbusID ; call : EN_SetBaudrate ; return ; // return von InitIC //**************************** //******************************* : Set_DN_Init ; call : Set_CO_Init ; return ; // return von InitIC //**************************** //******************************* : Set_PB_Init ; call : PB_SwapNibble ; call : PB_SetFieldbusID ; call : PB_SetBaudrate ; return ; // return von InitIC :PB_SwapNibble; Copy ( vb_fieldbusID , vb_tempnibble , vb_const_1 ) ; Copy ( vb_tempnibble , vb_temp , vb_const_1 ) ; // make a copy CalculateByte ( vb_tempnibble , vb_const_4 , shr , vb_tempnibble ) ; // shift High Nibble to Low Nibble CalculateByte ( vb_temp , vb_const_4 , shl , vb_temp ) ; // shift Low Nibble to High Nibble CalculateByte ( vb_tempnibble , vb_temp , add , vb_tempnibble ) ; Copy ( vb_tempnibble , vb_fieldbusID , vb_const_1 ) ; return ; //**************************** //**************************** : CO_SetFieldbusID ; if vb_fieldbusID greater vb_const_0 then :CheckLess64 else :TestMode ; : CheckLess64 ; if vb_fieldbusID less vb_const_64 then :SetIDfromSwitch else :TestMode ; : SetIDfromSwitch ; Convert ( vb_fieldbusID , Byte , vl_fieldbusID , Long ) ; SetByVar ( FieldbusID , vl_fieldbusID ) ; // must be a long type jump : EndID_Init ; : TestMode ; if vb_fieldbusID equal vb_const_0 then :SetIDto1 else :EndID_Init ; : SetIDto1 ; Set ( FieldbusID , 1 ) ; : EndID_Init ; return ; //************************* //**************************** : PB_SetFieldbusID ; if vb_fieldbusID greater vb_const_0 then :CheckLess126 else :PB_TestMode ; : CheckLess126 ; if vb_fieldbusID less vb_const_126 then :PB_SetIDfromSwitch else :PB_TestMode ; : PB_SetIDfromSwitch ; Convert ( vb_fieldbusID , Byte , vl_fieldbusID , Long ) ; SetByVar ( FieldbusID , vl_fieldbusID ) ; // must be a long type jump : PB_EndID_Init ; : PB_TestMode ; if vb_fieldbusID equal vb_const_255 then :PB_SetIDto1 else :EndID_Init ; : PB_SetIDto1 ; Set ( FieldbusID , 1 ) ; : PB_EndID_Init ; return ; //************************* //**************************** : IB_SetFieldbusID ; return ; //************************* //**************************** : EN_SetFieldbusID ; return ; //************************* //************************* : CO_SetBaudrate ; if vb_temp equal vb_const_0 then :SetBaud_1_Mbaud else :CheckBaud_500_kbaud ; : SetBaud_1_Mbaud ; Set ( BusBaudrate, 1000000 ); // CANopen, DeviceNet jump : SetBaudrateEnd ; : CheckBaud_500_kbaud ; if vb_temp equal vb_const_64 then :SetBaud_500_kbaud else :CheckBaud_250_kbaud ; : SetBaud_500_kbaud ; Set ( BusBaudrate, 500000 ); // CANopen, DeviceNet jump : SetBaudrateEnd ; : CheckBaud_250_kbaud ; if vb_temp equal vb_const_128 then :SetBaud_250_kbaud else :SetBaud_125_kbaud ; : SetBaud_250_kbaud ; Set ( BusBaudrate, 250000 ); // CANopen, DeviceNet jump : SetBaudrateEnd ; : SetBaud_125_kbaud ; Set ( BusBaudrate, 125000 ); // CANopen, DeviceNet : SetBaudrateEnd ; return ; //************************* //************************* : PB_SetBaudrate ; return ; //************************* //************************* : IB_SetBaudrate ; return ; //************************* //************************* : EN_SetBaudrate ; return ; //************************* //*********************************************************** : TestMode ; if vb_bus_type equal GATEWAY_ICCO_SC_NR then :Set_CO_TestMode else :Check_DP_IC_TestMode ; : Check_DP_IC_TestMode ; if vb_bus_type equal GATEWAY_ICPB_SC_NR then :Set_PB_TestMode else :Check_DN_IC_TestMode ; : Check_DN_IC_TestMode ; if vb_bus_type equal GATEWAY_ICDN_SC_NR then :Set_DN_TestMode else :Check_IB_IC_TestMode ; : Check_IB_IC_TestMode ; if vb_bus_type equal GATEWAY_ICIB_SC_NR then :Set_IB_TestMode else :Check_EN_IC_TestMode ; : Check_EN_IC_TestMode ; if vb_bus_type equal GATEWAY_ICEN_SC_NR then :Set_EN_TestMode else :No_more_TestMode_Check ; : No_more_TestMode_Check ; return ; //*************************** //*************************** : Set_CO_TestMode ; if vb_fieldbusID equal vb_const_0 then :IC_TestModeLoop else :Ende_Set_CO_TestMode ; : IC_CO_TestModeLoop ; ReadShiftRegister ( vb_fieldbusID ) ; CalculateByte ( vb_fieldbusID , vb_const_1 , not , vb_fieldbusID ) ; WriteBus ( vb_fieldbusID , vb_const_1 ) ; delay (200); jump : IC_CO_TestModeLoop ; : Ende_Set_CO_TestMode ; return ; //*************************** //*************************** : Set_DN_TestMode ; call : Set_CO_TestMode ; return ; //*************************** //*************************** : Set_IB_TestMode ; return ; //*************************** //*************************** : Set_EN_TestMode ; return ; //*************************** //*************************** : Set_PB_TestMode ; if vb_fieldbusID equal vb_const_255 then :IC_TestModeLoop else :Ende_Set_PB_TestMode ; : IC_TestModeLoop ; ReadShiftRegister ( vb_fieldbusID ) ; CalculateByte ( vb_fieldbusID , vb_const_1 , not , vb_fieldbusID ) ; call : PB_SwapNibble ; WriteBus ( vb_fieldbusID , vb_const_1 ) ; jump : IC_TestModeLoop ; : Ende_Set_PB_TestMode ; return ; //***************************