Modbus TCP using Wiznet W6100
This is looking like it might be easy...
Have working over serial port already (Modbus RTU):
https://forums.parallax.com/discussion/176224/modbus-rtu-slave
Using a wiznet driver from @ke4pjw
https://forums.parallax.com/discussion/177801/wiznet-w6100-spin2-driver-usage#latest
Just changed port from 80 to 502.
Tried just now to connect with Open Modscan's scanner and seeing data that makes sense.
Found a document that describes what getting from scanner:
https://www.prosoft-technology.com/kb/assets/intro_modbustcp.pdf
Comments
In the above, first pinged it with function #3 (Read holding regs) and then function #17 (Report Slave ID).
Had the code debug out all the bytes.
Was unsure at first why the packet started with a few zeros, but seeing that now in the above document.
Was thinking that was going to be exactly the same format as RTU, just without CRC.
Guess it somewhat is, but not exactly...
Next up is to work on sending a response...
This code now appears to be working with Open ModScan's Modbus Scanner in TCP mode.
Not 100% sure if the data is correct or not yet. The regular Open Modscan interface won't let me open the socket for some reason.
Might have to look for instructions...
Open ModScan is happy now with the regular data interface as well as the scanner.
TCP is pretty picky about #bytes that gets transferred, has to be exact...
May need to work on getting reconnection working now.
If disconnect and then reconnect, doesn't work...
It's looking like doing a timeout on a connection and then restarting it is something that needs to be coordinated with the master.
Seems like Open ModScan does a poll every second or so. Seems the only way to tell if connection is broken is to look for a stop in polling.
But, maybe @ke4pjw knows better?
So, added this timeout code to the query & response loop and seems to work.
If there's no query in a while after the first query, it will restart the connection:
@rayman,
That 10000 is a timeout value. I have changed this in the latest code. I will be publishing it soon. The new value will be based on milliseconds vs a generic counter++. That allows you to poll for input and if none is found you can go do something else.
I wrote isdisconnected(socket) to determine if a socket connection is disconnected. It should return true if Sn_IR_DISCON is set in the register status of REG_Sn_IR of the given socket.
Thanks @ke4pjw !
That sounds way too easy, but guess will try it.
@Rayman I have not tested, but pushed a new W6100.spin2 to my repo.
https://griswoldfx.visualstudio.com/Spinner Edge/_git/Software
Timeout for ReceiveData() is now milliseconds. Added more comments for each method. I am going to fold all of these changes into my other projects, the Blitzen 24 and CoCo Blender.