1. Trang chủ
  2. » Công Nghệ Thông Tin

CHƯƠNG 9 LẬP TRÌNH GIAO TIẾP MÁY TÍNH VÀ THIẾT BỊ ppsx

20 404 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 145,29 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Giao tiếp qua cổng máy in 3 GIAO TIẾP QUA CỔNG RS232 CHƯƠNG 9 4 Giao tiếp qua cổng RS232 và thiết bị • Mặc dù cổng RS232 phức tạp hơn cổng song song nhưng nó là cổng hữu hiệu nhất trong

Trang 1

1 KỸ THUẬT LẬP TRÌNH

CHƯƠNG 9

LẬP TRÌNH GIAO TIẾP MÁY TÍNH VÀ THIẾT BỊ

2 Mục lục chương 9

• 9.1 Giao tiếp qua cổng RS232

• 9.2 Giao tiếp qua cổng song song

• 9.3 Giao tiếp qua cổng nối tiếp

• 9.4 Giao tiếp qua cổng máy in

3 GIAO TIẾP QUA CỔNG RS232

CHƯƠNG 9

4 Giao tiếp qua cổng RS232

và thiết bị

• Mặc dù cổng RS232 phức tạp hơn cổng song song nhưng nó là cổng hữu

hiệu nhất trong truyền dữ liệu, có thể là wireless trong một số ngành và ít

tốn chi phí

• Liên k ết cổng RS232 bằng dây và dây này có 3 chức năng chính: “truyền”,

“nhận”, “nối đất”,

5 Giao tiếp qua cổng RS232

6 Giao tiếp qua cổng RS232

7 Giao tiếp qua cổng RS232

• Dây “nhận” và “truyền” này được kết nối giữa 2 máy tính với nhau

• Dữ liệu được truyền theo chuỗi Có 2 chân chính là TXD và RXD Các

chân khác của cổng là RTS, CTS, DSR, DTR, and RTS, RI

• Dữ liệu ‘1’ và ‘0’ được xác định theo mức điện áp 3V to 25V và 3V to

-25V

8 Giao tiếp qua cổng RS232

Đặc tính điện áp của cổng nối tiếp theo EIA (Electronics Industry

thấp so với tiêu chuẩn tốc độ hiện nay

• Vì lý do này, chúng ta nên chọn cổng RS-232D với tiêu chuẩn mới và

được áp dụng trên các máy tính thế hệ mới hiện nay

9 Giao tiếp qua cổng RS232

• Baud: t ốc độ truyền dữ liệu.

• Các tốc độ thiết lập: 300, 600, 1200, 2400, 4800, 9600, 19200,

@y

ahoo

Trang 2

38400,57600, 115200

• Kí hi ệu baud tương ứng với số bit truyền được trong 1 giây

• Có 1 bit bắt đầu và 1 bit dừng được gửi kèm theo với 1 byte dữ liệu

• à nếu định 9600 baud thì có tốc độ truyền 960 byte/giây

10 Giao tiếp qua cổng RS232

và cả 2 dạng này ở dạng đầu “cái” nằm ở phía sau máy tính.

• Chúng ta cần có một đầu “đực” để kết nối từ máy tính này sang máy tính

khác

• Các chân ra của D-9 và D-25 được trình bày dưới đây:

11 Giao tiếp qua cổng RS232

12 Giao tiếp qua cổng RS232

13 Giao tiếp qua cổng RS232

14 Giao tiếp qua cổng RS232

15 Giao tiếp qua cổng RS232

• Các thiết bị dùng các cổng nối tiếp cho giao tiếp được phân làm hai loại Chúng gồm DCE (Data

Communications Equipment) và DTE (Data Terminal Equipment)

• DCE là các thiết bị tương tự modem thiết bị, TA adapter, máy in chẳng hạn trong khi DTE là máy

tính bạn hoặc các thiết bị ngoại vi

• Một DTE là một máy tính và một modem là DCE Thông thường, mọi người hay nói về DTE tới

DCE hoặc DCE tới các bộ phát DCE.

• Bộ truyền DTE tới DCE là giữa modem và máy tính của chúng ta (đôi khi được xem như là một bộ

thiết bị ngoại vi ngoài) Điều này cho thấy truyền dự liệu rất nhanh hơn so với từ DCE đến DCE

DCE to DCE là kết nối giữa các modem, đôi khi còn được gọi là bộ kết nối các đường truyền

16 Giao tiếp qua cổng RS232

• Hầu hết hiện nay người dùng phổ biến các modem 28.8K hoặc 33.6K Do vậy, chúng

ta nên sử dụng các modem có tốc độ DCE đến DCE là 28.8K hoặc 33.6K

• Nếu cần loại modem có tốc độ cao, chúng ta nên dùng DTE đến DCE có tốc độ

khoảng 115,200 BPS (Tốc độ tối đa là 16550a UART)

• Các chương trình giao tiếp chúng ta sử dụng được thiết lập cho DCE đến DTE Tuy

nhiên, tốc độ thiết lập là 9.6 KBPS, 144 KBPS, và theo tốc độ modem có thể

truyền

17 Giao tiếp qua cổng RS232

• Nếu chúng ta chuyển dữ liệu dạng file text tại 28.8K (DCE- DCE), thì modem sẽ nén

file này lại và tốc độ truyền thực tế là 115.2 KBPS giữa các máy tình và do đó, tốc độ

truyền giữa DCE- DTE là 115.2 KBPS

• Vì vậy, điều này lý giải tại sao DCE- DTE nên có tốc độ kết nối cao hơn tốc độ kết

nối của modem Do đó, nếu tốc độ DTE đến DCE của chúng ta nhiều lần nhanh hơn

tốc độ DCE đến DCE, PC có thể gửi dữ liệu đến modem của chúng ta ở tốc độ

115,200 BPS

@y

ahoo

Trang 3

18 Giao tiếp qua cổng RS232

• NULL MODEM (modem r ỗng):

• Null modem được dùng để kết nối giữa hai DTE với nhau

• Thường được sử dụng để truyền các file dữ liệu giữa các máy tính, sử

dụng các giao thức (protocol) như Zmodem, xmodem,

19 Giao tiếp qua cổng RS232

20 Giao tiếp qua cổng RS232

• Hình vẽ được đề cập ở trên cho thấy kết nối các dây của null modem Đặc tính chính được

chỉ ra ở đây là sử dụng một máy tính để trao đổi mọi thứ (chat) với modem gần giống như

với một máy tính khác

SG

• Bất kỳ dữ liệu được truyền thông qua dây TD từ máy chủ đến máy khách, được nhận theo

dây RD Máy tính chủ phải được thiết lập thông số tương tự máy tính khách

• Tín hiệu dây nối đất_signal ground (SG) của cả hai máy tính phải được ngắn lại.

21 Giao tiếp qua cổng RS232

• Data Terminal Ready (DTR) được nối kín từ bộ Data Set Ready và Carrier Detect

trên cả hai máy tính

• Khi bộ Data Terminal Ready được xác nhận hoạt động, thì Data Set Ready và

Carrier Detect ngay lập tức được hoạt động

• Ở điểm này, máy tính được xem như là modem ảo Virtual Modem được kết nối sẵn

sàng và được phát hiện bởi các modem truyền dữ liệu khác

22 Giao tiếp qua cổng RS232

• All left to worry about now is the Request to Send and Clear To Send

• As both computers communicate together at the same speed, flow control is not

needed thus these two lines are also linked together on each computer

• When the computer wishes to send data, it asserts the Request to Send high and as

it is hooked together with the Clear to Send, It immediately gets a reply that it is ok to

send and does so

23 Giao tiếp qua cổng RS232

• The Ring indicator line is only used to tell the computer that there is a

ringing signal on the phone line As we do not have, a modem connected

to the phone line this is left disconnected

24 Giao tiếp qua cổng RS232

• To know about the RS232 ports available in your computer, Right click on "My

Computer", Goto 'Properties', Select tab 'Device Manager', go to Ports( COM & LPT

), In that you will find 'Communication Port(Com1)' etc

• If you right click on that and go to properties, you will get device status Make sure

that you have enabled the port( Use this port is selected)

25 Giao tiếp qua cổng RS232

@y

ahoo

Trang 4

• There are two popular methods of sending data to or from the serial port in

Turbo C

• One is using outportb(PORT_ID, DATA) or outport(PORT_ID,DATA)

defined in “dos.h”

• Another method is using bioscom() function defined in “bios.h”

26 Giao tiếp qua cổng RS232

• Using outportb() :

• The function outportb () sends a data byte to the port ‘PORT_ID’ The function

outport() sends a data word These functions can be used for any port including

serial port, parallel ports Similarly to receive data these are used

• inport reads a word from a hardware port

• inportb reads a byte from a hardware port

• outport outputs a word to a hardware port

• outportb outputs a byte to a hardware port

27 Giao tiếp qua cổng RS232

• Declaration: (Cú pháp)

• int inport(int portid);

• unsigned char inportb(int portid);

• void outport(int portid, int value);

• void outportb(int portid, unsigned char value);

28 Giao tiếp qua cổng RS232

• Remarks:

• inport works just like the 80x86 instruction IN It reads the low byte of a word from

portid, the high byte from portid + 2.

• inportb is a macro that reads a byte

• outport works just like the 80x86 instruction OUT It writes the low byte of value to

portid, the high byte to portid + 1.

• outportb is a macro that writes value Argument

29 Giao tiếp qua cổng RS232

• portid:

• Inport- port that inport and inportb read from;

• Outport- port that outport and outportb write to

• value:

• Word that outport writes to portid;

• Byte- that outportb writes to portid.

30 Giao tiếp qua cổng RS232

• If you call inportb or outportb when dos.h has been included, they are

treated as macros that expand to inline code

• If you don't include dos.h, or if you do include dos.h and #undef the

macro(s), you get the function(s) of the same name

@y

ahoo

Trang 5

31 Giao tiếp qua cổng RS232

• Return Value:

• # inport and inportb return the value read

• # outport and outportb do not return

32 Giao tiếp qua cổng RS232

• Using bioscom:

• The macro bioscom () and function _bios_serialcom() are used in this method in the serial

communication using RS-232 connecter.

• First we have to set the port with the settings depending on our need and availability In this

method, same function is used to make the settings using control word, to send data to the port

and check the status of the port

• These actions are distinguished using the first parameter of the function Along with that we are

sending data and the port to be used to communicate

• Here are the deatails of the Turbo C Functions for communication ports.

33 Giao tiếp qua cổng RS232

• Declaration: (Cú pháp)

• bioscom(int cmd, char abyte, int port)

• _bios_serialcom(int cmd ,int port, char abyte)

• bioscom() and _bios_serialcom() uses the bios interrupt 0x14 to perform

various communicate the serial communication over the I/O ports given

in port

• cmd: The I/O operation to be performed

34 Giao tiếp qua cổng RS232

35 Giao tiếp qua cổng RS232

• portid: port to which data is to be sent or from which data is to be read.

• 0: COM1

1: COM2

2: COM3

36 Giao tiếp qua cổng RS232

• abyte:

• When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyte

is ignored

• When cmd = 0 (_COM_INIT), abyte is an OR combination of the following

bits (One from each group):

37 Giao tiếp qua cổng RS232

38 Giao tiếp qua cổng RS232

• For example, if

@y

ahoo

Trang 6

• abyte = 0x8B = (0x80 | 0x08 | 0x00 | 0x03) = (_COM_1200 | _COM_ODDPARITY |

_COM_STOP1 | _COM_CHR8)

Odd parity (0x08 = _COM_ODDPARITY)

1 stop bit (0x00 = _COM_STOP1)

8 data bits (0x03 = _COM_CHR8)

• To initialise the port with above settings we have to write,

bioscom(0, 0x8B, 0);

39 Giao tiếp qua cổng RS232

• To send a data to COM1, the format of the function will be bioscom(1,

data, 0)

• Similarly bioscom(1, 0, 0 ) will read a data byte from the port

40 Giao tiếp qua cổng RS232

41 Giao tiếp qua cổng RS232

• When you compile and run the above program in both the computers, The characters typed in one

computer should appear on the other computer screen and vice versa

• Initially, we set the port to desired settings as defined in macro settings Then we waited in an idle

loop until a key is pressed or a data is available on the port If any key is pressed, then kbhit()

function returns non zero value

• So will go to getch function where we are finding out which key is pressed Then we are sending it

to the com port Similarly, if any data is available on the port, we are receiving it from the port and

displaying it on the screen

42 Giao tiếp qua cổng RS232

• To check the port, If you have a single computer, you can use loop-back

connection as follows

• This is most commonly used method for developing communication

programs Here, data is transmitted to that port itself Loop-back plug

connection is as follows

43 Giao tiếp qua cổng RS232

44 Giao tiếp qua cổng RS232

• If you run the above program with the connection as in this diagram, the character entered

in the keyboard should be displayed on the screen

• This method is helpful in writing serial port program with single computer Also you can

make changes in the port id if your computer has 2 rs232ports

• You can connect the com1 port to com2 of the same computer and change the port id in the

program

• The data sent to the port com1 should come to port com2 then also whatever you type in

the keyboard should appear on the screen

45 GIAO TIẾP QUA CỔNG MÁY IN

CHƯƠNG 9

46 Giao tiếp qua cổng máy in

@y

ahoo

Trang 7

• Port Assignments

• Each printer port consists of three port addresses; data, status and control

port

• These addresses are in sequential order That is, if the data port is at

address 0x0378, the corresponding status port is at 0x0379 and the control

port is at 0x037a

47 Giao tiếp qua cổng máy in

• Printer: LPT1, LPT2, LPT3

• Data port: 0x03bc, 0x0378, 0x0278

• Status: 0x03bd, 0x0379, 0x0279

• Control: 0x03be, 0x037a, 0x027a

• To definitively identify the assignments for a particular machine, use the DOS debug

program to display memory locations 0040:0008 For example:

• >debug

• -d 0040:0008 L8 0040:0008 78 03 78 02 00 00 00 00

48 Giao tiếp qua cổng máy in

• Note in the example that LPT1 is at 0x0378, LPT2 at 0x0278 and LPT3

and LPT4 are not assigned

• Thus, for this hypothetical machine;

• Printer: LPT1, LPT2, LPT3, LPT4

• Data port: 0x0378, 0x0278, none, none

• Status: 0x0379, 0x0279

• Control: 0x037a, 0x027a

49 Giao tiếp qua cổng máy in

• An alternate technique is to run Microsoft Diagnostics (MSD.EXE) and

review the LPT assignments

50 Giao tiếp qua cổng máy in

• Outputs

51 Giao tiếp qua cổng máy in

52 Giao tiếp qua cổng máy in

• Note that there are eight outputs on the Data Port (Data 7(msb) - Data 0) and four

additional outputs on the low nibble of the Control Port /SELECT_IN, INIT, /AUTO

FEED and /STROBE

• [Note that with /SELECT_IN, the "in" refers to the printer For normal printer

operation, the PC exerts a logic zero to indicate to the printer it is selected The

original function of INIT was to initialize the printer, AUTO FEED to advance the

paper In normal printing, STROBE is high The character to be printed is output on

the Data Port and STROBE is momentarily brought low.]

@y

ahoo

Trang 8

53 Giao tiếp qua cổng máy in

• All outputs on the Data Port are true logic That is, writing a logic one to a bit causes

the corresponding output to go high

• However, the /SELECT_IN, /AUTOFEED and /STROBE outputs on the Control Port

have inverted logic

• That is, outputting a logic one to a bit causes a logic zero on the corresponding

output This adds some complexity in using the printer port, but the fix is to simply

invert those bits using the exclusive OR function prior to outputting

54 Giao tiếp qua cổng máy in

• [One might ask why the designers of the printer port designed the port in this manner Assume

you have a printer with no cable attached An open usually is read as a logic one Thus, if a

logic one on the SELECT_IN, AUTOFEED and STROBE leads meant to take the appropriate

action, an unconnected printer would assume it was selected, go into the autofeed mode and

assume there was data on the outputs associated with the Data Port

• The printer would be going crazy when in fact it wasn't even connected Thus, the designers

used inverted logic A zero forces the appropriate action.]

55 Giao tiếp qua cổng máy in

• #define DATA 0x03bc

• #define STATUS DATA+1

• #define CONTROL DATA+2

• int val1, val2;

• val1 = 0x81; /* 1000 0001 */ /* Data bits 7 and 0 at one */

• outportb(DATA, val1);

= 1, INIT = 0, /AUTO_FEED = 0, /STROBE = 0 */

56 Giao tiếp qua cổng máy in

• Note that only the lower nibble of val2 is significant Note that in the last

line of code, /SELECT_IN, /AUTO_FEED and /STROBE are output in

inverted form by using the exclusive-or function so as to compensate for

the hardware inversion

57 Giao tiếp qua cổng máy in

• For example; if I intended to output 1 0 0 0 on the lower nibble and did not do the

inversion, the hardware would invert bit 3, leave bit 2 as true and invert bits 1 and 0

The result, appearing on the output would then be 0 0 1 1 which is about as far from

what was desired as one could get By using the exclusive-or function, 1 0 0 0 is

actually sent to the port as 0 0 1 1 The hardware then inverts bits 3, 1 and 0 and the

output is then the desired 1 0 0 0

58 Giao tiếp qua cổng máy in

• Note that in the diagram showing the Status Port there are five status leads from the printer

(BSY, /ACK, PE (paper empty), SELECT, /ERROR)

• [The original intent in the naming of most of these is intuitive A high on SELECT indicates

the printer is on line A high on BSY or PE indicates to the PC that the printer is busy or out

of paper A low wink on /ACK indicates the printer received something A low on ERROR

@y

ahoo

Trang 9

indicates the printer is in an error condition.]

• These inputs are fetched by reading the five most significant bits of the status port

59 Giao tiếp qua cổng máy in

• However, the original designers of the printer interface circuitry, inverted the bit

associated with the BSY using hardware That is, when a zero is present on input

BSY, the bit will actually be read as a logic one Normally, you will want to use "true"

logic, and thus you will want to invert this bit

• The following fragment illustrates the reading the five most significant bits in "true"

logic

60 Giao tiếp qua cổng máy in

• #define DATA 0x03bc

• #define STATUS DATA+1

• unsigned int in_val;

• in_val = ((inportb(STATUS)^0x80) >> 3);

61 Giao tiếp qua cổng máy in

• Note that the Status Port is read and the most significant bit, corresponding to the

BSY lead is inverted using the exclusive-or function The result is then shifted such

that the upper five bits are in the lower five bit positions

0 0 0 BUSY /ACK PE SELECT /ERROR Another input, IRQ on the Status

Port is not brought to a terminal on the DB-25 printer port connector I have yet to

figure out how to use this bit

62 Giao tiếp qua cổng máy in

• At this point, you should see that, at a minimum, there are 12 outputs;

eight on the Data Port and four on the lower nibble of the Control Port

There are five inputs, on the highest five bits of the Status Port Three

output bits on the Control Port and one input on the Status Port are

inverted by the hardware, but this is easily handled by using the

exclusive-or function to selectively invert bits

63 Giao tiếp qua cổng máy in

• Refer to the figure titled Figure #3 - Typical Application showing a normally

open push button switch being read on the BUSY input (Status Port, Bit 7)

and an LED which is controlled by Bit 0 on the Data Port A C language

program which causes the LED to flash when the push-button is

depressed appears below Note that an output logic zero causes the LED

to light

64

#include <stdio.h>

#include <dos.h> /* required for delay function */

#define DATA 0x03bc

#define STATUS DATA+1

@y

ahoo

Trang 10

void main(void)

{

int in;

while(1)

{

in = inportb(STATUS);

if (((in^0x80)&0x80)==0) /* if BUSY bit is at 0 (sw closed) */

{

outportb(DATA,0x00); /* turn LED on */

delay(100);

outportb(DATA, 0x01); /* turn it off */

delay(100);

}

else

{

outportb(DATA,0x01); /* if PB not depressed, turn LED off */

}

}

}

65 Giao tiếp qua cổng máy in

66 Giao tiếp qua cổng máy in

• Circuit Description: Logic 1 on output DATA 0 (Data Port - Bit 0) causes LED to be

off Logic 0 causes LED to turn on

• Normally open push-button causes +5V (logic 1) to appear on input BUSY (STATUS

PORT - Bit 7) When depressed, push-button closes and ground (logic 0) is applied

to input Busy

• Note external source of 5V

• Program Description: When idle, push-button is open and LED is off On

depressing push-button, LED blinks on and off at nominally 5 pulses per second

67 GIAO TIẾP CỔNG SONG SONG

CHƯƠNG 9

• Parallel ports are easy to program and faster compared to the serial ports

• But main disadvantage is it needs more number of transmission lines

Because of this reason parallel ports are not used in long distance

communications

• Let us know the basic difference between working of parallel port and serial port

• In serial ports, there will be two data lines: One transmission and one receive line

To send a data in serial port, it has to be sent one bit after another with some extra

bits like start bit, stop bit and parity bit to detect errors.

• But in parallel port, all the 8 bits of a byte will be sent to the port at a time and a

indication will be sent in another line.

• In the PC there will be D-25 type of female connector having 25 pins and

in the printer, there will be a 36-pin Centronics connector

• Connecting cable will combine these connecter using following convention

@y

ahoo

Ngày đăng: 26/07/2014, 16:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm