// Ths script demonstrates the usage of script commands // relating to Modbus TCP // it may not reflect your application and should only be seen // as an example or as an principle ScriptName "example Modbus TCP" ScriptAuthor "FP/" ScriptVersion "V 1.0" // some variable and constants var vb_Channel: byte; // which Ethernet channel to use var vw_NumBusInput: word; var vwc_Const0: word; MoveConst ( vwc_const0, 0); var vwc_Const1: word; MoveConst ( vwc_const1, 1); var vw_RSRcvSize: word; // serial receive size var vb_RSRcvSize: byte; var vb_ErrorCode: byte; var vw_tmpRSRcvSize: word; var va_RSRcvBuffer: Buffer[1024]; var va_ENRcvBuffer: Buffer[1024]; var vw_BusBytesUsed: word; // install error handler SetErrorHandler ( :MainError ) ; // Initializing Modbus TCP for Channel 0 MoveConst( vb_Channel, 0); InitFastEthernet( vb_channel, ModbusTCP); // Now the Ethernet uses Ports 502 as defined for Modbus TCP. // If you read the busdata the data format is similar // as described for Modbus RTU // initialize ethernet hardware BusStart; // wait for Ethernet controller ok Wait ( Bus_Active ) ; :MainLoop; // Check Ethernet Get ( AvailableBusData, vw_NumBusInput ); // some data availble, read data and write to serial port if vw_NumBusInput greater vwc_const0 then :CheckMoreEthernetData else :CheckRs; :CheckMoreEthernetData; // wait short and receive complete message Set( SelectID, 4); delay(100 ); Get (AvailableBusData, vw_NumBusInput); // read this data from bus and send this data to serial side :SendEN; Set( SelectID, 2); ReadBus( va_ENRcvBuffer[0], vw_NumBusInput); SendRs( va_ENRcvBuffer[0], vw_NumBusInput); :CheckRS; // any character available at RS port Get ( NumberRSInputCharacter , vb_RSRcvSize ) ; Convert( vb_RSRcvSize, byte, vw_RSRcvSize, word ); if vw_RSRcvSize greater vwc_const0 then :CheckMoreSerialData else :MainLoop; :CheckMoreSerialData; // Check serial port until 1 ms no character received Get ( RSInputCharacter16 , vw_RSRcvSize ) ; delay( 2 ); Get ( RSInputCharacter16 , vw_TmpRSRcvSize ) ; if vw_tmpRSRcvSize equal vw_RSRcvSize then :RSRcv else :SendEN; :RSRcv; // receive data from serial port ReceiveSomeCharRS( vwc_Const1, va_RSRcvBuffer[0], vw_RsRcvSize ); :WaitSendEN; // wait until EN Buffer is free Get ( UsedBusOutputCharacters , vw_BusBytesUsed ) ; if vw_BusBytesUsed greater vwc_const0 then :WaitSendEN else :RealSendEN; :RealSendEN; // send data by EN Set( SelectID, 1); Set( RS_State_LED, staticGreen ); WriteBus( va_RSRcvBuffer[0], vw_RsRcvSize ); // next cycle jump :MainLoop; // ErrorHandling :MainError; // should never be reached // display lower 4 error bits Get( Errorcode, vb_ErrorCode ); SetByVar( SelectID, vb_ErrorCode ); // next cycle return;