|
FORUMS PROFESSIONNELS WINDEV, WEBDEV et WINDEV Mobile |
| | | | | |
Débuté par Saby , 22 fév. 2024 16:11 - 2 réponses |
| |
| | | |
|
| |
Membre enregistré 2 messages |
|
Posté le 22 février 2024 - 16:11 |
Good morning. I'm trying to connect to a ModBUs TcIp to read data. I followed the instructions in the manual.... "...... // Create a session and connect to the Modbus server at the address 192.168.1.18 on port 3275 Session is modbusSession Session = ModbusConnectTCP("192.168.1.18",3275)
// If the connection is there, affiche l'erreur IF ErrorOccurred = True THEN Trace(ErrorInfo()) ELSE // Write a value in the server register 0x42. ModbusWriteRegister(Session, 0x42,0xCAFE) END
// Disconnection of the server ModbusDisconnect(Session) ................"
Replacing with my IP and my port... but when I try to read the data...
".......... // Create a session and connect to a Modbus server Session is modbusSession Session = ModbusConnectTCP("192.168.1.7")
arrResponse is array of 5 int
// If the connection fails, display the error IF ErrorOccurred = True THEN Trace(ErrorInfo()) ELSE // Read a sequence of 5 bits in read-write mode starting at address 0x42 arrResponse = ModbusReadBit(Session, 0x42, 5, False) // Display the error, if any IF ErrorOccurred = True THEN Trace(ErrorInfo()) END END
// Disconnection ModbusDisconnect(Session) ..............."
But I always get value "0". Does anyone have any suggestions - examples? Thank you. |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 55 messages Popularité : +2 (2 votes) |
|
Posté le 23 février 2024 - 14:41 |
Hello Saby, I code this class to read/write data from modbus Schneider Proface HMI Then I build other class that inherits my ModBusComm base class Try to play with it
Hope this helps Andrea
CONSTANT ModBusRetry=3 END
ModBusComm is a Class mbIpAddress is string mbPort is int=502 mbSlaveID is int=255 mbTimeOut is int=500 mbSession is modbusSession mbConnected is boolean
mbError is boolean mbMessErr is string mbAutoResetErr is boolean=True
mbarrBit is array of boolean mbarrInt16 is array of 2-byte int mbarrInt32 is array of 4-byte int
mbString is string END
PROCEDURE Constructor()
PROCEDURE Destructor()
PROCEDURE mbConnect()
IF mbConnected THEN RESULT True END
IF mbAutoResetErr THEN mbResetErr() END
IF NoSpace(mbIpAddress)="" OR mbPort=0 THEN mbError=True mbMessErr="ModBusConn - controllare i parametri di connessione" RESULT False END
bTuttoOk is boolean FOR i=1 TO ModBusRetry mbSession=ModbusConnectTCP(mbIpAddress, mbPort, mbSlaveID, mbTimeOut)
IF NOT ErrorOccurred THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk THEN mbError=True mbMessErr="ModBusConn - error ["+ErrorInfo()+"]" RESULT False END
mbConnected=True
RESULT True
PROCEDURE mbAvailable()
IF mbAutoResetErr THEN mbResetErr() END
IF NoSpace(mbIpAddress)="" THEN mbError=True mbMessErr="ModBusConn - IpAddress non definito" RESULT False END
RESULT Ping(mbIpAddress, 100)
PROCEDURE mbResetErr()
mbError=False mbMessErr=""
PROCEDURE mbDisconnect()
IF mbAutoResetErr THEN mbResetErr() END
IF NOT mbConnected THEN RESULT True END
mbSession.Disconnect() mbConnected=False
mbSession.Reset()
IF ErrorOccurred THEN mbError=True mbMessErr="ModBusConn - disconnection error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbReadRegister(nIndirizzo is int, nQuanti is int=1)
IF NOT mbConnect() THEN RESULT False END
IF nIndirizzo<=0 OR nQuanti<=0 THEN mbError=True mbMessErr="mbReadRegister - controllare parametri" RESULT False END
ArrayDeleteAll(mbarrInt16)
bTuttoOk is boolean FOR i=1 TO ModBusRetry mbarrInt16=mbSession.ReadRegister(nIndirizzo-1, nQuanti, False)
IF NOT ErrorOccurred THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk THEN mbError=True mbMessErr="mbReadRegister error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbReadReg32(nIndirizzo is int, nQuanti is int=1)
ArrayDeleteAll(mbarrInt32)
IF NOT mbReadRegister(nIndirizzo, nQuanti*2) THEN RESULT False END
nIndex is int FOR i=1 _TO_ nQuanti nIndex=((i-1)*2)+1 ArrayAdd(mbarrInt32, MakeInteger(mbarrInt16[nIndex+1],mbarrInt16[nIndex])) END
RESULT True
PROCEDURE mbReadReg16(nIndirizzo is int, nQuanti is int=1)
IF NOT mbReadRegister(nIndirizzo, nQuanti) THEN RESULT False END
RESULT True
PROCEDURE mbWriteRegister(nIndirizzo is int, arrVal16 is array of 2-byte int)
IF NOT mbConnect() THEN RESULT False END
IF nIndirizzo<=0 THEN mbError=True mbMessErr="mbWriteRegister - controllare parametri" RESULT False END
IF ArrayCount(arrVal16)=0 THEN mbError=True mbMessErr="mbWriteRegister - nessun valore da scrivere" RESULT False END
bTuttoOk is boolean FOR i=1 TO ModBusRetry IF mbSession.WriteRegister(nIndirizzo-1, arrVal16) THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk _OR_ ErrorOccurred THEN mbError=True mbMessErr="mbWriteRegister - write register error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbWriteReg32(nIndirizzo is int, arrVal32 is array of 4-byte int)
arrVal16 is array of 2-byte int
FOR i=1 _TO_ ArrayCount(arrVal32) ArrayAdd(arrVal16, LoWord(arrVal32[i])) ArrayAdd(arrVal16, HiWord(arrVal32[i])) END
IF NOT mbWriteRegister(nIndirizzo, arrVal16) THEN RESULT False END
RESULT True
PROCEDURE mbWriteReg16(nIndirizzo is int, arrVal16 is array of 2-byte int)
IF NOT mbWriteRegister(nIndirizzo, arrVal16) THEN RESULT False END
RESULT True
PROCEDURE mbReadBit(nIndirizzo is int, nQuanti is int=1)
IF NOT mbConnect() THEN RESULT False END
IF nIndirizzo<=0 OR nQuanti<=0 THEN mbError=True mbMessErr="mbReadBit - controllare parametri" RESULT False END
ArrayDeleteAll(mbarrBit)
bTuttoOk is boolean FOR i=1 TO ModBusRetry mbarrBit=mbSession.ReadBit(nIndirizzo-1, nQuanti, False)
IF NOT ErrorOccurred THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk THEN mbError=True mbMessErr="mbReadBit error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbWriteBit(nIndirizzo is int, arrValori is array of boolean)
IF NOT mbConnect() THEN RESULT False END
IF nIndirizzo<=0 THEN mbError=True mbMessErr="mbWriteBit - controllare parametri" RESULT False END
IF ArrayCount(arrValori)=0 THEN mbError=True mbMessErr="mbWriteBit - nessun valore da scrivere" RESULT False END
bTuttoOk is boolean FOR i=1 TO ModBusRetry IF mbSession.WriteBit(nIndirizzo-1, arrValori) THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk _OR_ ErrorOccurred THEN mbError=True mbMessErr="mbWriteBit error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbWriteStr(nIndirizzo is int, sValore is string="", LOCAL nMaxChar is int=1)
IF NOT mbConnect() THEN RESULT False END
IF nIndirizzo<=0 OR nMaxChar<=0 THEN mbError=True mbMessErr="mbWriteStr - controllare parametri" RESULT False END
IF IsOdd(nMaxChar) THEN nMaxChar++ END
bufVal is Buffer=sValore bufVal=Complete(bufVal, nMaxChar)
ArrayDeleteAll(mbarrInt16)
FOR i=1 _TO_ nMaxChar/2 ArrayAdd(mbarrInt16, BufferToInteger(bufVal,(i-1)*2,2)) END
bTuttoOk is boolean FOR i=1 TO ModBusRetry IF mbSession.WriteRegister(nIndirizzo-1, mbarrInt16) THEN bTuttoOk=True BREAK END END
IF NOT bTuttoOk _OR_ ErrorOccurred THEN mbError=True mbMessErr="mbWriteStr error ["+ErrorInfo()+"]" RESULT False END
RESULT True
PROCEDURE mbReadStr(nIndirizzo is int, LOCAL nQuanti is int=1)
IF IsOdd(nQuanti) THEN nQuanti++ END
nQuanti=nQuanti/2
IF NOT mbReadReg16(nIndirizzo,nQuanti) THEN RESULT False END
mbString=""
FOR i=1 TO nQuanti mbString+=Charact(LoWord(mbarrInt16[i],1)) mbString+=Charact(HiWord(mbarrInt16[i],1)) END
RESULT True
PROCEDURE Bcd2Bin(LOCAL nValore is 8-byte int)
nValByte is 1-byte int sValStr is string
FOR i=1 TO 8 nValByte[1] = nValore[(i-1)*4+1] nValByte[2] = nValore[(i-1)*4+2] nValByte[3] = nValore[(i-1)*4+3] nValByte[4] = nValore[(i-1)*4+4]
sValStr=NumToString(nValByte, "01d")+sValStr END
RESULT Val(sValStr) |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 2 messages |
|
Posté le 27 février 2024 - 16:43 |
Thanks for the reply. Could you send me which example code? thanks. tech@caseariacioffispa.it |
| |
| |
| | | |
|
| | | | |
| | |
| | |
| |
|
|
|