ScriptAuthor "Deutschmann Automation GmbH" ScriptName "Modbus/TCP-Test" ScriptVersion "V 1.0" SetErrorHandler ( :Address ) ; SetSystemErrorHandler ( :Address ) ; // This script realized the Modbus-RTU-function // described in UNIGATE Manual // Bufferlength must be max. Fieldbus-Length // For this example = 32 Byte Var BusInput : BUFFER[32] ; // 32=max.Fieldbus-Length Var BusOutput : BUFFER[32] ; // 32=max.Fieldbus-Length Var wBusInputLen: Word ; Var wBusOutputLen: Word ; Var bBusInputLen: byte ; Var bBusOutputLen: byte ; Var Timeout_ms: Word ; Var ModbusRxLen: Word ; Var bModbusRxLen: byte ; Var ReturnCode: Byte ; Var NoError: Byte ; Var Const0: Word; MoveConst( Const0, 0); var channel: Byte; var pk1: Word; var bDestination:byte; MoveConst (Timeout_ms , 0) ; // No timeout-check MoveConst (NoError, 0) ; // Returncode 0 = No Error // Init serial port Set ( Baudrate , 19200 ) ; Set ( Databits , 8 ) ; Set ( Startbits , 1 ) ; Set ( Stopbits , 1 ) ; Set ( Parity , even ) ; Set ( ModbusRTUTimeout , 10000 ) ; // 250ms Set ( RS_State_LED , RedGreenFlashing ) ; // Start Bus and wait until running // Source-Ports und Destination-Ports fuer die 4 Kanaele festlegen // --------------------------------------------------------------- MoveConst(pk1,0x01F6); MoveConst (channel, 0 ); // InitFastEthernet ( channel,TCPIP_Server,pk1,pk1,ipa) ; InitFastEthernet ( channel,ModbusTCP); // ,pk1,pk1,ipa) ; BusStart ; Wait ( Bus_Active ) ; Set ( RS_State_LED , GreenFlashing ) ; :WaitNewBusData ; Set ( CommunicationChannel , 0 ) ; // Wait without timeout for changed Busdata // Get ( BusDataChanged , v1 ) ; :START; Get ( AvailableBusData , wBusInputLen ) ; if wBusInputLen greater Const0 then :GETD else :START ; // WaitBusDataChanged ( Timeout_ms , BusInputLen ) ; :GETD; ReadBus ( BusInput[0] , wBusInputLen ) ; // Received Busdata will be send by Modbus // Then wait for Modbus answer ExchangeModbusRTUMaster ( BusInput[0], BusOutput[0], bModbusRxLen ) ; // Check Returncode for Modbus-Error Get ( ErrorCode , ReturnCode ) ; if ReturnCode equal NoError then :CopyModbusDataToBus else :ModbusError ; :CopyModbusDataToBus ; Set ( Errorcode , 0 ) ; // Clear Error-LEDs // Send received Modbus-Data to fieldbus Convert ( bModbusRxLen , Byte , ModbusRxLen , Word ) ; WriteBus ( BusOutput[0] , ModbusRxLen ) ; Set ( RS_State_LED , StaticGreen ) ; jump :WaitNewBusData ; :ModbusError ; // RS-LED static red, Yellow-LED blinking with "10" Set ( Errorcode , 10 ) ; jump :WaitNewBusData ; :Address; Get ( errorCode , bDestination ) ; // TBD stop; return;