I can't help you with any code, but if you need to drive a relay or solenoid first you should check whether you fulfill all hardware requirements. Most solenoids need more current than usual digital outputs can supply, and also usually the guaranteed 'logic high' voltage is just 2.4V or so - sufficient to drive a TTL input but not sufficient to drive a solenoid.
I would recommend to add a PhotoMOS relay like the AQV212 by Matsushita. All you need to drive the input side of this ic is a series resistor connected to the input, 390 ohms will be a good choice to connect it to a TTL level output. The output of the AQV212 can handle something like 30V and 300mA. When connecting solenoids or relays to electronic circuitry make sure to add a snubber diode across relay terminals to cut off voltage spikes.