RodBird
Sep 2, 2010
=Accessing the Serial Port= //Rod Bird// [[toc]] ---- PC's use a serial communications standard usually referred to as RS232. This standard defines the signals and lines of the port and was first established in the early 1960s. Once regarded as complex, its present format is simplified and easy to implement in Liberty BASIC. An RS232 serial port is always hardware based. It uses voltages higher than normally used in a PC, typically -15 to +15 volts and so needs dedicated hardware. The port will be implemented on the motherboard of older PC's, as a separate PCI card in later PC's and in more modern machines it will take the guise of a dongle on the end of a USB lead. Let me differentiate the Universal Serial Bus (USB) and the serial port. USB is the new wired way to connect PC peripherals. Almost any kind of peripheral comes in a USB flavour. Joysticks, keyboards, printers etc.including serial port and parallel port interfaces. Now it matters not a jot that the serial port we are going to discuss is wired in via USB. The device itself will behave exactly like a serial port soldered on the motherboard. When I say exactly, I should differentiate between the very original serial port on the motherboard and all other types, defined as "virtual" serial ports. The difference is that the originals had fixed memory addresses and could be programmed by "bit banging". That is directly accessing the memory addresses of the controlling hardware. Thus pins could be set and reset simply by writing to a single, fixed memory address. Liberty BASIC allows this with INP and OUT but as so few of us will possess such a serial port I'm not going to cover it. There are examples of the technique on the Forum. Virtual ports don't allow "bit banging" but we can access the pins in other ways. ==Pin out== Check out the diagram below. I'm showing the DB9 format, this is the simplest format, as we might have been discussing DB25 and its 25 pin names. Most new devices will use the DB9 format. If yours is DB25 it is pretty easy to research the pin outs. We will actually be using very few of the pins even in DB9 format. [[image:db9.gif align="center"]] RTS & DTR are outgoing handshaking lines that we can set and hold. DCD, DSR, CTS, and RI are incoming handshaking lines that we can read. RX & TX are controlled by the serial port hardware and pass the serial data. A positive voltage at the TX pin indicates ON or SPACE, a negative voltage indicates OFF or MARK. The port itself is pretty robust and will withstand the odd short or grounding but do take care if connecting your own kit, make sure it will withstand the voltages. Some modern implementations sometimes work with 0 volts as OFF and +5 volts as ON but will only operate over a limited cable length. ==Making the connection== Now how do we connect all of these pins between two devices to create a serial link? Well we only need three of the pins, TX, RX, and GND. Connect the TX of one to the RX of the other and vice versa, connect the GNDs and you have a working serial communications link with just three wires! Device 1 Device 2 Pin Pin --- --- 2 RX (Receive Data) Input ----------- Output 3 TX (Transmit Data) 3 TX (Transmit Data) Output ----------- Input 2 RX (Receive Data) 5 GND (Signal Ground) Ground ----------- Ground 5 GND (Signal Ground) Because we cross connect the TX and RX pins, independent channels are established for two-way (full-duplex) communications. Each device has an input buffer and an output buffer. Messages, in the form of ASCII characters are accepted from the PC and queued in the output buffer to be passed over the communications link as time permits. At the other end the received characters are stored in an input buffer awaiting the receiving PC reading and emptying that buffer. Thus there are four buffers involved in any serial link. Data is passed across the wire as a sequence of ON OFF bits, the rate bits are passed at is determined by the Baud rate, 9600 bits per second is common. Typically 8 data bits define one character and to that parity checking and stop bits are added. This additional bit information helps the serial hardware pass the messages efficiently. With 8 data bits ASCII characters between 0 and 255 can be sent. If we drop down to seven bits we are limited to sending ASC values between 0 and 127. Always, the smallest component that can be sent is one character, a byte, a number between 0 and 127 or 255. ==Loop back== If you want to have some fun you can link Device 1's TX and RX pins. This is called "Loop back" and lets you send and receive messages from yourself. This is very useful for experimenting. Device 1 Pin --- 2 RX (Receive Data) Input \ 3 TX (Transmit Data) Output 5 GND (Signal Ground) Ground ==Opening the port== This line of code might start to make a little more sense. [[code format="vbnet"]] open "Com2:9600,n,8,1" for random as #commHandle [[code]] ==Second Part Title== Text here. [[code format="vbnet"]] 'code here [[code]] ==Third Part Title== Text here. [[code format="vbnet"]] 'code here [[code]]