ScriptAuthor "Deutschmann Automation GmbH" ScriptName "FB-CO" ScriptVersion "V 1.1" // V 1.0 start script // V 1.1 change RS Baudrate to 62500 // read busbaudrate and fieldbus ID from hardware switch // V 1.2 an PD V2 angepasst // This script realized a script for our filedbus-fieldbus gateway // where one side is CANopen // 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_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 vw_fieldbusID: Word ; Var vl_fieldbusID: Long ; Var vb_tempnibble: Byte ; Var vb_temp : Byte ; // Long var of types var vltemp : long ; // Errorhandler init => After Error, script will automatic jump to "Errorhandler" SetErrorHandler ( :ErrorHandler ) ; // Init serial port Set ( Baudrate , 62500); Set ( Databits , 8 ) ; Set ( Startbits , 1 ) ; Set ( Stopbits , 1 ) ; Set ( Parity , none ) ; Set ( ShiftRegisterOutputType , RiseClock_RiseLoad ) ; Set ( ShiftRegisterOutputBitLength , 8 ) ; // Fieldbus ID init Set ( ShiftRegisterInputType , RiseClock_LowLoad ) ; Set ( ShiftRegisterInputBitLength , 8 ) ; ReadShiftRegister ( vb_fieldbusID ) ; Convert(vb_FieldbusID, byte, vw_FieldbusID, word); CalculateByte ( vw_fieldbusID , vw_const_1 , not , vw_fieldbusID ) ; call :ConvertID ; 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 ) ; jump :EndID_Init ; :EndID_Init ; Set ( BusInputSize , 8 ) ; Set ( BusOutputSize , 8 ) ; call :SetBaudrate ; 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 , Vw_BusInputLen ) ; Get ( BusOutputSize , Vb_BusOutputLen ) ; if vb_fieldbusID equal vb_const_0 then :IC_TestModeLoop else :CheckNewBusData ; :IC_TestModeLoop ; ReadShiftRegister ( vb_fieldbusID ) ; CalculateByte ( vb_fieldbusID , vb_const_1 , not , vb_fieldbusID ) ; //call :ConvertID ; WriteBus ( vb_fieldbusID , vb_const_1 ) ; delay (200); jump :IC_TestModeLoop ; // Check if Busdata available :CheckNewBusData ; Get ( BusDataChanged , vb_NewBusData ) ; if vb_NewBusData equal vb_Const_0 then :NoBusData else :CopyBusDataToRS ; //Get ( AvailableBusData , VW_BusInputLen ) ; //if VW_BusInputLen equal VW_Const_0 then :NoBusData else :CopyBusDataToRS ; :CopyBusDataToRS ; //ReadBus ( VA_BusInput[0] , VW_BusInputLen ) ; //SendRS ( VA_BusInput[0] , VW_BusInputLen ) ; ReadBus ( VA_BusInput[0] , Vw_BusInputLen ) ; SendRS ( VA_BusInput[0] , Vw_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 ; :SwapNibble; 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 return ; :ConvertID ; Copy ( vb_fieldbusID , vb_tempnibble , vb_const_1 ) ; call :SwapNibble ; Copy ( vb_tempnibble , vb_fieldbusID , vb_const_1 ) ; return ; :SetBaudrate ; if vb_temp equal vb_const_0 then :SetBaud_1_Mbaud else :CheckBaud_500_kbaud ; :SetBaud_1_Mbaud ; moveconst ( vltemp , 1000000 ) ; //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 ; moveconst ( vltemp , 500000 ) ; //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 ; moveconst ( vltemp , 250000 ) ; //Set ( BusBaudrate, 250000 ); // CANopen, DeviceNet jump :SetBaudrateEnd ; :SetBaud_125_kbaud ; moveconst ( vltemp , 125000 ) ; //Set ( BusBaudrate, 125000 ); // CANopen, DeviceNet :SetBaudrateEnd ; //SetByVar ( BusBaudrate , vltemp ) ; return ;