Diversions 5/4/08
Not a lot of progress on the actual modbus RTU in the last few weeks, but I've managed to get a whole bunch of other things done, or at least partially done.
Via a rather confused path I'm back on track though, and looking to get some more progress on the RTU in the next week or two.
In the mean time I've been playing with the software abstraction end of the virtual PLC. To get a 'real' example going I decided I'd make a small RS232 PLC based around a Parallax Basic Stamp I. The lowly Basic Stamp I is a great tool for rapid prototyping of ideas, although they are a wee bit pricey for what they are, they are still a very good tool.
Fig 1.Fig. 1 shows the wee sorta-rtu. There are some issues with using a BS1 in this type of application in the real world, they have a very limited code space (About 80 instructions) and their serial comands are blocking. That is, if the Basic Stamp is waiting for serial input, it does just that, waits. Your main program will hang. For this reason I'd not recommend you rely on a Basic Stamp 1 for anything 'real'.
Anyway, my Basic Stamp RTU has two relay outputs, two opto-isolated digital inputs, two 10 bit A/D's provided by a PIC12F675, an RS232 'host' port and a ttl serial output to drive a serial LCD.
You can probably guess I wasted an inordinate amount of time making something that really isn't that much practical use due to the limitations of the Basic Stamp I. On the up side I came up with a dandy way to use a PIC12F675 and a single I/O pin on the stamp to get a configurable A/D Converter, which can do 8 bit, 10 bit, and selectable reference by serial commands. I'll create a separate page for that little widget at some stage, as I think it's probably of use to someone else by itself.
Having created the bs1rtu (tm) I stuck it ona bit of MDF, attached some relays and miscellaneous wire and low, I have a gadget bigger than the sum of the parts, or something like that.
Fig 2.The basic premise of this bit of random wiring is that I have a solar panel on the roof of the shed, which charges a 20Ah gel cell battery via a shunt charge controller I built ages ago. I'm running a laptop from the battery, which talks to the bs1rtu. When the battery voltage is high enough it runs the laptop directly off the gel cell. When the battery voltage gets low it turns on the mains supply to a 13.8V power supply, waits for the power to come good on that, and then runs off the 13.8V supply.
A wider shot of this whole thing is in order I feel.
Fig 3.The laptop is missing it's screen, as it had failed, and lets face it, there is no need for a screen when I never go into the shed. I'm running Debian Linux on the laptop, and a bit of ladder-logic type software which is where the whole statement about abstraction of the virtual PLC comes in. The laptop is acting as a PLC, the bs1rtu the RTU in this rather interesting relationship.
The 'front end' of the software displays the ladder logic, and changes colours to show the status of inputs and outputs. It also keeps a log of all the history, so I can look back and find out how well the experiment is working.
Fig 3.So far this has been running flawlessly for 3 weeks, but I'd still not recommend you build something like this for a production environment. At the very least you should have a microcontroller without a blocking serial routine, and use a protocol with robust error checking.
Once I've got a couple more of issues sorted out with this fabulous bit of engineering I'll put a live copy of the front end on the web, so if you're really, really bored you can tell if an old laptop in my garage is running on solar power, or on mains. Fascinating.
An interesting discovery out of this process is that the solar shunt regulator, which uses a 12F675 to PWM a mosfet regulator is not actually that good if the battery is being cycled. It's fine for a float charger, but because it regulates full time, the battery never reaches float voltage if a current is being drawn from the circuit as well.
I also re discovered the simple fact that I love Basic Stamp's for prototyping. Being a bit of a tinkerer I like the basic stamp, but I'm averse to paying for them. This lead to a great deal of wasted time, researching options on the web, and designing various 'stampalikes' which I could use for my own rapid prototyping. The funny thing about this is that I've now wasted far more time designing some small 'stampalikes' that I may never make than I will ever recover in terms of time spent verses the cost of buying some more parallax stamps! I'll post some info about my stampalikes another day....
So, what's next you ask? I picked up some RS485 drivers for my modbus RTU last week, and intend to change to RS485, and commit the circuit design to drawings for a simple isolated I/O RTU. I'm on the hunt for some reasonably priced DIN rail enclosures to mount the thing in as well, so that might be a bit of a challenge, we'll see. I also may move the project into a larger PIC. My logic in using the 'cheap' 16F870 is a little flawed, as I discovered I can get the far more capable 18F2520 for less. Such is the weirdness that is component pricing.