Вы находитесь на странице: 1из 0

5-1

Chapter 5 Introduction to the serial port



Overview
The serial ports of general personal computer were usually called the RS-232, and it is one
of the most popular input/output interfaces of PC. This chapter was interduced briefly the serial
ports of PC, and use the read/write I/O port of LabVIEW and the related type of vi function of the
serial port to do a simple test.

Objective
To understand the principle of action for the serial ports.
To understand how to use the LabVIEW to test the serial ports.

Keyword
RS232
The Serial Ports

Explanation of function
Path
All FunctionsInstrument I/OI/O
SerialVISA Configure Serial Port
Explanation
Initializes the serial port specified by
VISA resource name to the specified
settings.
Input
VISA resource name specifies the
resource to be opened.
The parameters need initialization such
as: the baud rateflow control etc.
Output If there is a bug, output the error code
Path
All FunctionsAdvancedPort I/OIn
Port.vi
Explanation
Reads a signed integer from a specific
address.
Input
address specifies the address from
which you want to read a 8-, 16-, or
32-bit signed integer.

Output
data read is the byte (8 bits)of data read
from theaddress specified.
Path
All FunctionsAdvancedPort I/OOut
Port.vi
Explanation
Writes a signed integer to the specified
address.
Input
address specifies the address in the I/O
memory range to begin the read from.
write value is the byte (8-bit value) to
write to the systems's memory.

5-2
Brief
There are two serial ports on the back board of PC to be called COM1COM2 respectively,
they use RS-232 as the protocol of telecommunication. The earlier version of serial ports on the
back board of PC is shown as Figure 1, there is one of 9 pins DB-9 male socket so called COM1,
the other one of 25 pins DB-25 male socket so called COM2, the third one of 25 pins female
socket so called parallel printer port. But now most new version PCs are all changed peripheral
ports as Figure 2, this two serial ports are all changed as DB-9 male type socket. You have to
first recognize your computer to belong to which kind of alignment type.













Figure 1














Figure 2

The serial port is one of channels for a general PC communicated with the outside world, for
example the modem connected to the Internet has the usage of serial ports to send and receive
message. In the industrial control world we still has the usage of serial ports to send data or
control signals, the reason is that it can save money. Of course the serial port have to transmit

5-3
data fast enough other than we cant use it. Suppose today we have a control device which want
to send a command to remote controlled object, then the serial port has several kinds of mode
according to the transmission direction of

Simplex Mode
This mode can only deliver the signal with single direction, the control device is only
responsible for sending out the signal and the controlled device was asked to receive signal only.
Data stream can flow just one direction, this mode of data flow only suits for one-way
communication and it does not need any response applies.

Semi-duplex Mode
Each side of data stream can flow to mutual direction, but at the same time it only one data
stream can flow to the opposite by one direction. This mode of data flow suits for two-way
communication but no instant necessity response applies. This is because when the sender
sends the message the receiver can not send message at the same time. So if the controlled
devices need to send back message instantly in case of emergency, this mode of data flow is
not suitable.

Full-duplex Mode
Each side of data stream can flow to mutual direction simultaneously. This kind of data flow
is used to improve the semi-duplex mode, but it need to add more cost to accomplish. Why? Let
us think to consider about it.

There are two serial ports on general PC at present, and generally it is called the COM1 or
COM2. At the earlier version of PC inside there is an exclusive IC called 8250 to deal with serial
port, but now the new version of PC has integrated the 8250 IC into south-bridge chip and those

5-4
two types of PC are all functional comparable to the serial ports. This 8250 IC has called UART
(Universal Asynchronous Receiver Transmitter). The Asynchronous means that the transmitter
and receiver at each end of the serial port must have the same transmission baud rate, they
dont need to synchronize the timing sequence at both systems in order to exchange data or
signal. But in order to assure the accurate data, the UART must have the function of parity check
and handshake signal etc. to reinforce the using of serial ports.

If you have learned the I/O structure of PC, you have known that the heart of PC is CPU,
but CPU communicate with peripheral to exchange data through the I/O port. The serial port on
PC is one of peripheral, so it exchange data through the I/O port. We have mentioned that the
serial port is controlled by UART chip, but only connecting the UART chip pins with the I/O port
pins does not work. Because in this chip there are several control register to program some
parameters for the serial port, therefore we must understand these control register in order to
control the serial port to communicate for us.

The following listed the UART chip inner register with relation to the I/O port.
I/O address Name of Register
COM1 COM2
Abbreviation
of English
Full Name of English
Comment
3F8H 2F8H THR Transmitter Holding Register
When DLAB* is 0, the address
point to this register. This
register writes only.
3F8H 2F8H RBR Receiver Buffer Register
When DLAB* is 0, the address
point to this register. This
register read only.
3F8H 2F8H LSB
Baud Rate Divisor Register
(LSB fraction)
When DLAB* is 1, the address
point to this register.
3F9H 2F9H MSB
Baud Rate Divisor Register
(MSB fraction)
When DLAB* is 1, the address
point to this register.
3F9H 2F9H IER Interrupt Enable Register
When DLAB* is 0, the address
point to this register.
3FAH 2FAH IIR Interrupt Identification Register
3FBH 2FBH LCR Line Control Register
3FCH 2FCH MCR MODEM Control Register
3FDH 2FDH LSR Line Status Register
3FEH 2FEH MSR MODEM Status Register
* The DLAB is the 7th bit of the LCR register.

5-5
This table describes the correspondence between the internal register of UART and I/O.
From this table we see that there are 10 different kinds of register but just 7 address of I/O to
cross-refer, so why it would be like this? Because some registers use the same address of I/O,
for example THR and RBR they use the same address of I/O, when the data to be written into
this address it is to write into the THR, when the data to be read from this address it is to read
from the RBR. The other example is that LSBMSB and IER use the same address too, and
how can they know when data write-in or readout which register will be used? This is decided by
the status of DLAB ( the 7
th
bit of LCR ), for COM1 as the example when the status is 0 the
address 3F8
H
refers to THRRBR and the address 3F9
H
refers to IER. When the status is 1
the address 3F8
H
refers to LSB and the address 3F9
H
refers to MSB. Therefore if you want to
readout or write-in data on those address you have to set the status of the 7
th
bit of LCR first.
Now we will describe the function of each register in the table briefly:

Transmitter Holding Register
The transmitter holding register is used to buffer outgoing data. When the data has been
transmitted, the bit 5 in the line status register would be stetted as 1. The value 1 indicates the
transmitter holding register is empty, new data could be written into it.

Receiver Buffer Register
The receiver buffer register contains the data just received, and if this data is not taken
away properly it will be overlaid by the next received data, then it could result in data loss.

Baud rate Divisor Register
The serial baud rate of 8250 chip is to make use of the external crystal of quartz to oscillate
1.8432 MHz clocks which could be divided to many kinds of baud rate by baud rate divisor.
Therefore the value in this register decide the baud rate of the serial port, the following table lists
the 14 different kinds of baud rate related to the value in baud rate divisor register.

5-6
All the internal register of UART are 8 bits width, therefore all the value in the table to fill in
register must be divided into two fractions, that is higher 8 bits and lower 8 bits, then we can put
the higher 8 bits into MSB(3F9
H
) and the lower 8 bits into LSB(3F8
H
) .
Baud rate( bits per second)
The value in baud rate divisor register
( decimal system)
50 2304
75 1536
150 768
300 384
600 192
1200 96
1800 64
2400 48
3600 32
4800 24
7200 16
9600 12
19200 6
38400 3

Interrupt Enable Register
This register is used to set the UART to generate a processor interrupt when the condition
happens, so that it can inform PC to perform interruption service routines (ISR) to response this
calling. Due to the whole practices in this book does not use the interrupt function, so we skip to
discuss it more detail. If the reader hope to learn more about it, please refer to the other related
books.

Interrupt Identification Register
This register is used to provide information for interruption service routines (ISR) of PC to
judge which devices in the UART to generate a processor interrupt in order to proceed the next
steps. Due to the whole practices in this book does not use the interrupt function, so we skip to
discuss it more detail. If the reader hope to learn more about it, please refer to the other related
books.


5-7
Line Control Register
This register is used to set several important parameters to the serial communication, such
as data word lengthnumber of stop bits or parity check etc., and the details of bit setting in this
register is as follows
7 6 5 4 3 2 1 0
Setting of parity check

000 = No parity check
001 = Odd parity check
011 = Even parity check
101 = Mark
111 = Space
The other status is not allowed
Setting of data word
length
00 = 5 bits
01 = 6 bits
10 = 7 bits
11 = 8 bits

Number of stop bits
0 = 1 stop bit
1 = 1.5 stop bit (when 5 bits word)
2 stop bit (when 6.7 or 8 bits
word)
0 = Break signal disabled
1 = Break signal enabled
0 = Transfer the addressing of 3F8H/3F9H to the register THR/RBR
1 = Transfer the addressing of 3F8H/3F9H to the register LSB/MSB

MODEM Control Register
This register is used to set the related pins status of MODEM. The detailed function is
shown as the next table.
7 6 5 4 3 2 1 0
correspon
ding pin of
DTR
1= Data
terminal is
ready

corresponding pin of
RTS
1=Request to send


OUT1

OUT2
Forever set to the null
1 = Loopback mode




5-8
Line Status Register
The line status register shows the current state of communication. Errors are reflected in
this register. The state of the receiver and transmit buffers is also available.
7 6 5 4 3 2 1 0
1=Data
available
1=Overrun error

1=Parity check error

1=Framing error

1=Break signal received

1=THR is empty
Forever
set to the
null
1= THR is empty, and line is idle

MODEM Status Register
This register contains information about the four incoming modem control lines on the
device. The four most significant bits contain information about the current state of the inputs
where the least significant bits are used to indicate state changes. The four LSBs are reset,
each time the register is read. The following table explains the function of each bit
7 6 5 4 3 2 1 0
1=MODE
M have
already
prepared
to receive
the
character

1= MODEM power
supply have already
activated

1= telephone bell is ringing
Carrier
detect
Ring
indicator
Data set
ready
Clear to
send
1= MODEM is already connected with
another MODEM

Definition of Connector for RS-232
The pin assignment of connector for RS-232 DB-25 on PC end is explained as follows.



5-9





Pin definition The meaning Pin definition The meaning Pin definition The meaning
1 PG
Protective
ground
10 - - 19 - -
2 TxD Transmit data 11 - - 20 DTR
Data terminal
ready
3 RxD Receive data 12 - - 21 - -
4 RTS
Request to
send
13 - - 22 RI Ring indicator
5 CTS Clear to send 14 - - 23 - -
6 DSR
Data set
ready
15 - - 24 - -
7 SG Signal ground 16 - - 25 - -
8 DCD
Data carrier
detect
17 - -
9 - - 18 - -

The pin assignment of connector for RS-232 DB-9 on PC end is explained as follows.




Pin definition The meaning
1 DCD Data carrier detect
2 RxD Receive data
3 TxD Transmit data
4 DTR Data terminal ready
5 SG Signal ground
6 DSR Data set ready
7 RTS Request to send
8 CTS Clear to send
9 RI Ring indicator


5-10
Practice 5-1 To examine on the serial port

Do you know how to inspect the serial port?
First you must make sure there is at least one serial port on your PC. You can follow the
following steps to find out the device manager. Begin from start, next setting, choose
control, select system and find out device manager. Spread out the device manager to find
out COM&LPT, if you cannot find out COM&LPT on your PC, that means your PC out-of-date
please buys a new one. When you spread out COM&LPT, you can find one printer port (LPT1)
and two communication ports (COM1COM2), but on laptop or notebook PC it may be just one
communication port (COM1), it does not matter because we just use one communication port in
all practices of the bookThe following Figure shows communication port (COM1) properties.










After Our PC owned at least one serial port, we can use the LabVIEW to do a simple
self-examination test to inspect the status of the serial port.




5-11
1Open All functionsInstrument I/OSerialVISA Configure Serial Port.VI to the
Block Diagram.
To create a new I/O for one of input parameter COM port number.
To create an indicator for output error code.
Next to decide what number should be filled into this parameter of port number.
We should go back to find how many communication ports we have. Right now
we use COM1 or COM2 as activated port. Assume that it is COM1 then
according to the above table we can find out the corresponding number.


5-12
2It will be generated a number to show the quality of COM1. If we press the
execution button, what is the number displayed on the screen? If we got 0,
which means everything O.K., the quality of COM1 is good. If we got the other
number, which means that the communication port (COM1) does not work, it
may make a mistake while installation. Please come back to check your system
and setup again.

The end of practice 5-1



















5-13
Practice 5-2 UART self-checking

Another more formal testing is to use UART internal self-checking method. The following
process is taken COM1 as the example.
To make use of the 4
th
bit of loop test function we could test UART whether works well or
not.
To open the loop function of the 4
th
bit of MCR( setting to 1), it may cause the following
action

The serial output of the UART will be setted to status of mark(1).
The serial input of the UART will be disconnected with the outside world.
The serial output of the UART will be connected internally with input of the UART
automatically.
The four bits of the MSR:DSR, CTS, RI, and DCD, would link to DTRRTSOUT1
and OUT2 of MCR one by one.

[MCR] [MSR]
DTR DSR
RTS CTS
OUT 1 RI
OUT 2 DCD

We have such a convenient function, so we can test our serial port very quickly. By using
the selfloop-diagnostic function of the UART, it could make the output and input of serial port
connected together internally. So if we output a byte of data onto the serial port by program, then
it will be received by receiving terminal internally. Therefore we make use of this function for
testing our serial port at this practice.


5-14
1At first put on the Stacked Sequence Structured on the Block Diagram and adjust
the size to fit it, next in Sequence put a new Out Port.vi on it. To generate a new
variable from each input parameter register address and value of Out Port.vi,
we must transfer those two variable formats into Hexadecimal system. We input
3FC
H
which is the address of MCR in COM1 into the parameter of register address,
and the parameter value does not input any number temporarily.

2To create a new page frame from Sequence and connect it on the back. In this new
page frame we put into a port.vi on it. To generate a variable from parameter
register address, we must transfer this variable format into hexadecimal system.
To generate a control item from parameter value, its input format will transfer into
hexadecimal system automatically. We input 3F8
H
into the variable of parameter
register address, which means we transfer the input value of control item into
transmitter holding register of COM1. Please take notice that if we dont transfer the
variable of input parameter register address into Hexadecimal format, the input
value 3F8
H
will not come out visibly.



5-15

3To create a new page frame from Sequence and connect it on the back again. In this
new page frame we put into a delay block on it. This is for generating a waiting
latency to wait data from transmitter holding register to come out. Because we dont
set the transmit rate of COM1, we just take the original setting value by default. The
COM1 usually own the properties of 9600bpsno parity checkdata width 8 bits and
one stop bit. So the totally holding transmitting time is calculated as follows

Total transmit bits transmit time per bit
= total transmit time
Total transmit time = 1one start bit
+ 88data bits +1one stop bit =10 bits
Transmit time per bit = The reciprocal of 9600 Hz =104.2 us
The total transmit time =104.2u 10bit =1.042ms

So the time delay of this page must be longer than 1.042 ms, for insurances sake we set
the 10 ms as the time delay. Normally the parameter of time delay for .vi is with ms as time unit,
therefore we can fill in the variable with 10, which means that we want to delay 10 ms.



5-16

4To create a new page frame from Sequence and connect it on the back again.
Because the test method of this time is self-testing which means the data in the inner
part of UART to transmit the data into him-self, therefore it has to read in the data
from Receiver Buffer Register". Now we use In Port.vi to read in the value at the
address of 3F8
H
, please remember to transfer the format of register address into
hexadecimal system. We also need to generate a indication to display the value from
the output of In Port.vi.

5-17
5On the Front Panel there would be displayed a new control item and an item of
indication that just generated.

6Please try to input a value under 256 to the above control item, and click the single
execution key. Now check the value of the indication item is it the same or not? The
answer should be negative! Do you remember the value of the first page frame is
zero? That is what the value we want to set inside of the MCR, now we can set the
value 10 (hexadecimal) into the value of the first page frame, and click the single
execution key again to see what happens on the indication item?

Run

5-18
After click the single execution key, if you find that the value of the indication item equal to
the value of control item, then it means the experiment success and the serial ports is O.K

The end of practice 5-2

5-19
Exercise and discussion
1Do you know why the MCR have to fill in 10( HEX)? What does it mean?

2We have previously mentioned that the four readable bits of MSR were connected
with DTR, RTS, OUT 1, and OUT 2 of MCR as following table:

[MCR] [MSR]
DTR DSR
RTS CTS
OUT 1 RI
OUT 2 DCD
Do you know how to use vi of the LabVIEW to control and read those signals?


















5-20

Вам также может понравиться