Using the various 'ports' of a PC from Liberty BASIC

The three main routes for signalling into or out of a PC are its parallel port (LPT1 etc); serial port (COM1 etc); and USB. Most modern machines have only USB. However USB serial port & parallel port adaptors plug into USB ports, and behave almost exactly like their original hardware counterparts. Follow the links to my pages where I have examples. NB *** electrical connections are able to cause motherboard damage to your computer **. I've never blown a port, and they have a degree of protection built in- but won't stand you accidentally connecting mains voltage to themselves!!

Parallel port

If you have a parallel port, you use commands like

OUT port, byte        and
returnedByte = INP(port)
The port address will usually be ( in hex) 3bf, 37f or 27f. If you are happy with hex (base 16) numbers, you'll find HEXDEC( "value" ) and DECHEX$( number ) useful and can bit-twiddle with OR, AND and XOR. The result is to set 8 output pins ( one byte) to your chosen pattern, or read all 8 as inputs. You decide how fast to read or write- there is no handshake to worry about. Sent data is 'latched' ie stays available at the output until superceded by new data.
Some typical applications will be found on my LB site index. There you will find too my 'Mains Light Sequencer' which ran off a laptop parallel port.

Serial port

If you have a serial port, the data is transmitted as 8 Os or 1s one after the other at fixed intervals, but preceded by a start bit, and followed by an optional parity bit and at least one stop bit, and at a rate called the 'baud rate', which may range from 1200 to 115000 or so. Transmission goes out on one line (TX Data) ; received data ( RX data) comes in on another. There are two extra lines to declare readiness to send ( RTS) or receive ( CTS) to the other end device. And the signals are sent as 'On' ='High' = (nominal) +15V, or 'Off' ='Low', =(nominal) -15V. Most modern machines accept High as 5V and Low as 0V. The data is transient- it is not retained unless the receiving end is programmed to store it. Both transmission and reception involve buffers- memory areas which accumulate received data until it is processed, or data for transmission until it can be sent or accepted at the 'other' end.

This means it is really only useful to talk to or listen to equipment with built in ability to 'understand' the protocol and set to the matching values, eg 2400 baud, 8 bits, no parity, one stop bit. It has a number of 'handshaking' lines which assert readiness to transmit or receive, and declare acceptance of a transmission. Often these were not used, so their presence is 'fiddled' by bridging certain wires together, which fools each end into believing the other end is always ready- which it normally is. It has been convenient through the years for commercial equipment to 'talk' serially, and in the recent past it has been a standard for many microprocessers. You have to watch out 'tho, because quite often pins like DTR and RTS are used to 'steal' power from a PC to run a peripheral, or even toggled to drive a 'pumped' voltage converter.
Liberty Basic talks to serial devices, whether native hardware or USB adaptors, as serial devices. You open a port, 'talk' to it or 'listen' to it, and then close when finished, with commands like the following.

open "COMn:baud, parity, data stop{,options}" for random as #commhandle
#commhandle, chr$( 42);
dataRead$ = input$( #commHandle, lof( #commHandle))
close #commHandle
You can also drive the DTR & RTS lines Hi or Low, which gives the simplest way to control two output lines. I use as indicators two LEDs in antiparallel, and a 1k5 resistor in series between the line and earth. You can equally easily drive transistors, logic, optoisolators.....

And a final caution- the computer serial port has male pins, and you connect a 9 pin female socket to it on the end of your interface cable. Pin 1 is in mirror positions on the two genders- check carefully. The pins are USUALLY numbered on the plastic.

Some typical applications will be found on my LB site main index., where you will find serial driving of a servo controller and of other peripherals. Also see Driving DTR & CTS

USB port

Plug into here a serial adaptor and get the equivalent of a serial port as described above. But, better, plug in a device which includes the adaptor, like Brian Schmalz's UBWs, which LB addresses as if it were a serial device, and gives you digital and analogue (and PWM) signals.
See more examples at LB index , and digame LB site. Especially see my AirWriter driven by UBW.
Any queries? e-Mail me changing DOT to '.'