• No results found

Serial Communication via RS-232_ Bioscom Example -ElectroSofts

N/A
N/A
Protected

Academic year: 2021

Share "Serial Communication via RS-232_ Bioscom Example -ElectroSofts"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

HOME Electronics Directory Articles/ Tutorials eBooks

About Us FORUM Links Contact Us

Serial Comm nica ion ia RS232 Por :

By Harsha Perla and Veena Pai Download example source code

Introduction to RS-232:

RS232 is the most known serial port used in transmitting the data in communication and interface.

Even though serial port is harder to program than the parallel port, this is the most effective method in which the data transmission requires less wires that yields to the

less cost. The RS232 is the communication line which enables the data transmission by only using three wire links. The three links provides transmit , receive and

common ground...

The transmit and receive line on this connecter send and receive data between the computers. As the name indicates, the data is transmitted serially. The two pins are TXD & RXD. There are other lines on this port as RTS, CTS, DSR, DTR, and RTS, RI. The 1 and 0 are the data which defines a voltage level of 3V to 25V and -3V to -25V respectively.

The electrical characteristics of the serial port as per the EIA (Electronics Industry Association) RS232C Standard specifies a maximum baud rate of 20,000bps, which is slow compared to today s standard speed. For this reason, we have chosen the new RS-232D Standard, which was recently released.

The RS-232D has existed in two types. i.e., D-TYPE 25 pin connector and D-TYPE 9 pin connector, which are male connectors on the back of the PC. You need a female connector on your communication from Host to Guest computer. The pin outs of both D-9 & D-25 are show below.

D-Type-9 pin no.

D-Type-25 pin no.

Pin outs Function

3 2 RD Receive Data (Serial data input)

2 3 TD Transmit Data (Serial data output)

7 4 RTS Request to send (acknowledge to modem that

UART is ready to exchange data

8 5 CTS Clear to send (i.e.; modem is ready to exchange

data)

6 6 DSR Data ready state (UART establishes a link)

(2)

5 7 SG Sig a g d 1 8 DCD Da a Ca ie de ec (Thi i e i ac i e he de de ec a ca ie 4 20 DTR Da a Te i a Read . 9 22 RI Ri g I dica (Bec e ac i e he de de ec i gi g ig a f PSTN

Abo DTE & DCE:

De ice , hich e e ia cab e f hei c ica i , a e i i ca eg ie . The e a e DCE (Da a C ica i E i e ) a d DTE (Da a Te i a E i e .) Da a C ica i E i e a e de ice ch a de , TA ada e , e e c hi e Da a Te i a E i e i

C e Te i a . A ica Da a Te i a De ice i a c e a d a ica Da a C ica i De ice i a M de . Of e e e i a ab DTE DCE DCE DCE eed . DTE DCE i he eed be ee de a d c e , e i e efe ed a e i a

eed. Thi h d a fa e eed ha he DCE DCE eed. DCE DCE i he i be ee de , e i e ca ed he i e eed.

M e e da i ha e 28.8K 33.6K de . The ef e, e h d e ec he DCE DCE eed be ei he 28.8K 33.6K. C ide i g he high eed f he de e h d e ec he DTE DCE eed be ab 115,200 BPS. (Ma i S eed f he 16550a UART) . The c ica i

g a , hich e e, ha e i g f DCE DTE eed . H e e , he eed i 9.6 KBPS, 144 KBPS e c a d he de eed.

If e e e a fe i g ha e fi e a 28.8K (DCE- DCE), he he he de c e e i a e ac a a fe i g 115.2 KBPS be ee c e a d h ha e a DCE- DTE eed f 115.2 KBPS. Th , hi i h he DCE- DTE h d be ch highe ha he de ' c ec i eed. The ef e, if DTE DCE eed i e e a i e fa e ha DCE DCE eed he PC ca e d da a de a 115,200 BPS.

Wha i NULL MODEM?

N de i ed c ec DTE' ge he . Thi i ed a fe fi e be ee he

(3)

Figure: Above shows the connections of the Null modem using RS-232D connecter

Above-mentioned figure shows the wiring of the null modem. The main feature indicated here is that the to make the computer to chat with the modem rather than another computer. The guest & host computer connected through the TD, RD, and SG pins. Any data that is transmitted through TD line from the Host to Guest is received on RD line. The Guest computer must have the same setup as the Host. The signal ground (SG) line of the both must be shorted so that grounds are common to each computer.

The Data Terminal Ready (DTR) is looped back to Data Set Ready and Carrier Detect on both computers. When the Data Terminal Ready is asserted active, then the Data Set Ready and Carrier Detect immediately become active. At this point, the computer thinks the Virtual Modem to which it is connected is ready and has detected the carrier of the other modem.

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.

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

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).

Ho to program the Serial Port using C/C++?

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”.

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 Declaration:

int inport(int portid);

unsigned char inportb(int portid); void outport(int portid, int value);

(4)

oid o po b(in po id, n igned cha al e);

Rema k :

inpo o k j like he 80 86 in c ion IN. I ead he lo b e of a o d f om po id, he

high b e f om po id + 2.

inpo b i a mac o ha ead a b e

o po o k j like he 80 86 in c ion OUT. I i e he lo b e of al e o po id, he

high b e o po id + 1.

o po b i a mac o ha i e al e A g men

po id:

Inpo - po ha inpo and inpo b ead f om; O po - po ha o po and o po b i e o al e:

Wo d ha o po i e o po id; B e- ha o po b i e o po id.

If o call inpo b o o po b hen do .h ha been incl ded, he a e ea ed a mac o ha e pand o inline code.

If o don' incl de do .h, o if o do incl de do .h and # ndef he mac o( ), o ge he f nc ion( ) of he ame name.

Re n Val e:

# inpo and inpo b e n he al e ead

# o po and o po b do no e n

Fo mo e de ail of he e f nc ion ead a icle f om beondlogic.com U ing bio com:

The mac o bio com () and f nc ion _bio _ e ialcom() a e ed in hi me hod in he e ial comm nica ion ing RS-232 connec e . Fi e ha e o e he po i h he e ing depending on o need and a ailabili . In hi me hod, ame f nc ion i ed o make he e ing ing con ol o d, o end da a o he po and check he a of he po . The e ac ion a e di ing i hed ing he fi pa ame e of

he f nc ion. Along i h ha e a e ending da a and he po o be ed o comm nica e. He e a e he dea ail of he T bo C F nc ion fo comm nica ion po . Decla a ion:

bio com(in cmd, cha ab e, in po )

_bio _ e ialcom(in cmd ,in po , cha ab e)

bio com() and _bio _ e ialcom() e he bio in e p 0 14 o pe fo m a io comm nica e he e ial comm nica ion o e he I/O po gi en in po .

(5)

cmd: The I/O operation to be performed.

cmd (boiscom) cmd(_bios_serialcom)Action

0 _COM_INIT Initialise the parameters to the port

1 _COM_SEND Send the character to the port

2 _COM_RECEIVE Receive character from the port

3 _COM_STATUS Returns rhe current status of the

communication port portid: port to which data is to be sent or from which data is to be read. 0: COM1

1: COM2 2: COM3 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):

value of abyte Meaning

Bioscom _bios_serialcom 0x02 0x03 _COM_CHR7 _COM_CHR8 7 data bits 8 data bits 0x00 0x04 _COM_STOP1 _COM_STOP2 1 stop bit 2 stop bits 0x00 0x08 0X10 _COM_NOPARITY _COM_ODDPARITY _COM_EVENPARITY No parity Odd parity Even parity 0x00 0x20 0x40 0x60 0x80 0xA0 0xC0 0xE0 _COM_110 _COM_150 _COM_300 _COM_600 _COM_1200 _COM_2400 _COM_4800 _COM_9600 110 baud 150 baud 300 baud 600 baud 1200 baud 2400 baud 4800 baud 9600 baud For example, if

abyte = 0x8B = (0x80 0x08 0x00 0x03) = (_COM_1200 _COM_ODDPARITY _COM_STOP1 _COM_CHR8)

(6)

he comm nica ion po i e o 1200 ba d (0 80 = _COM_1200) Odd pa i (0 08 = _COM_ODDPARITY) 1 op bi (0 00 = _COM_STOP1) 8 da a bi (0 03 = _COM_CHR8)

To ini iali e he po i h abo e e ing e ha e o i e, bio com(0, 0 8B, 0);

To end a da a o COM1, he fo ma of he f nc ion ill be bio com(1, da a, 0). Simila l bio com(1, 0, 0 ) ill ead a da a b e f om he po .

The follo ing e ample ill a e ho o e ial po p og am . When a da a i a ailable in he po , i inp he da a and di pla on o he c een and if a ke i p e ed he ASCII al e ill be en o he po .

#include <bios.h> #include <conio.h> #define COM1 0 #define DATA_READY 0 100 #define SETTINGS ( 0 80 0 02 0 00 0 00) in main(oid)

in in, out, status;

bioscom(0, SETTINGS, COM1); /*initiali e the port*/ cprintf("Data sent to ou: ");

hile (1)

status = bioscom(3, 0, COM1); /* ait until get a data*/ if (status & DATA_READY)

if ((out = bioscom(2, 0, COM1) & 0 7F) != 0) /*input a data*/ putch(out);

if (kbhit())

if ((in = getch()) == 27) /* ASCII of Esc*/ break;

bioscom(1, in, COM1); /*output a data*/

re rn0;

When o compile and n he abo e p og am in bo h he comp e , The cha ac e ped in one comp e ho ld appea on he o he comp e c een and ice e a. Ini iall , e e he po o de i ed e ing a defined in mac o e ing . Then e ai ed in an idle loop n il a ke i p e ed o a da a i a ailable on he po . If an ke i p e ed, hen kbhi () f nc ion e n non e o al e. So ill go o ge ch f nc ion he e e a e finding o hich ke i p e ed. Then e a e ending i o he com po . Simila l , if an da a i a ailable on he po , e a e ecei ing i f om he po and di pla ing i on he c een.

To check he po , If o ha e a ingle comp e , o can e loop-back connec ion a follo . Thi i mo commonl ed me hod fo de eloping comm nica ion p og am . He e, da a i an mi ed o ha po i elf. Loop-back pl g connec ion i a follo .

(7)

Fig 2. Loop-back pl g connec ion

If o n he abo e p og am i h he connec ion a in hi diag am, he cha ac e en e ed in he ke boa d ho ld be di pla ed on he c een. Thi me hod i helpf l in i ing e ial po p og am i h ingle comp e . Al o o can make change in he po id if o comp e ha 2 232po . Yo can connec he com1 po o com2 of he ame comp e and change he po id in he p og am. The da a en o he po com1 ho ld come o po com2. hen al o ha e e o pe in he ke boa d ho ld appea on he c een. The p og am gi en belo i an e ample o ce code fo e ial comm nica ion p og amme . I i a PC o PC comm nica ion ing RS232. Do nload he code, n ip and n o cha in do mode be een o comp e . U e he p og am o ge mo e idea abo e ial po p og amming.

Click here to download example source code: pc2pc. ip Plea e con ac if an p oblem e i in he abo e p og am .

>Interfacing Links:Links related to serial port, bioscom function, rs232, rs485, usb, parallel port etc.

>Click here to list other C Programming sites

Click he e o end feedback o e email: ha ha@elec o of .com o eena@elec o of .com No e: E ample gi en abo e a e e ed i h T bo C++ Compile Ve ion 3.0. Ho e e , e do no gi e an g a an fo o king of he p og am in he e ample .

Hom e | About Us | Articles/ Tutorials | Downloads | Feedback | Links | eBooks | Privacy Policy

Copyright 2005-2007 electroSofts.com .

webm [email protected]

References

Related documents

(2) anterior with their left hand and arm holding the patient's right shoulder and controlling the patient ' s ventral flexion, left side bending and right rotation and

• People who worked in Cuyahoga County lost a combined total of $5.2 billion from their paychecks between 2000 and 2010, according to an analysis of payroll data of residents'

Testing Data Radio Modem with Serial Port Tool V1.20 This document demonstrates how to test the communication of data radio modem with tool Advanced Serial

Uma das atividades da reunião de Planejamento da Release é definir o Plano de Release, nesse plano estabelece-se o prazo de entrega (estimado) do produto e nível de prioridade

[r]

The Kaduna division of the Court of Appeal (by a majority decision) held that the State High Court had jurisdiction to entertain the suit, notwithstanding that the Defendants

In views of the Medication Events/Inpatient Nursing Services view type (event views) in the clinical work station and in the administration dialog, administration functions for

Chapter 3.1: Management via RS-232 serial connection Connect the RS-232 cable following the, from an available communication port in the computer to the Com socket in the back of