Null modem is used to connect two DTE's together. This is used to transfer files between the computers using protocols like Zmodem protocol, xmodem protocol, etc
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).
How 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:
Serial Port Communications Software
int inport(int portid);
unsigned char inportb(int portid);
void outport(int portid, int value);
void outportb(int portid, unsigned char value);
Remarks:
inport works just like the 80x86 instruction IN. It reads the lowbyte 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 thelow byte of value to portid, the high byte to portid + 1.
outportb is a macro that writes value Argument
portid:
value:
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 themacro(s), you get the function(s) of the same name.
Return Value:
#inport and inportb return the value read
#outport and outportb do not return
For more details of these functions read article from beondlogic.com
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.
Declaration:
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.
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)
the communications port is set to
1200 baud (0x80 = _COM_1200)
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);
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.
The following example illustrate how to serial port programs. When a data is available in the port, it inputs the data and displays onto the screen and if a key is pressed the ASCII value will be sent to the port.
#include <bios.h> #include <conio.h> #define COM1 0 #define DATA_READY 0x100 #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00) int main(void) { int in, out, status; bioscom(0, SETTINGS, COM1); /*initialize the port*/ cprintf('Data sent to you: '); while (1) { status = bioscom(3, 0, COM1); /*wait until get a data*/ if (status & DATA_READY) if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/ putch(out); if (kbhit()) { if ((in = getch()) 27) /* ASCII of Esc*/ break; bioscom(1, in, COM1); /*output a data*/ } } return0; }
|
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.
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.
Fig 2. Loop-back plug connection
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.
The program given below is an example source code for serial communication programmers. It is a PC to PC communication using RS232. Download the code, unzip and run to chat in dos mode between two computers. Use the program to get more idea about serial port programming.
Click here to download example source code: pc2pc.zip
Please contact us if any problem exits in the above programs.
>Interfacing Links:Links related to serial port, bioscom function, rs232, rs485, usb, parallel port etc.
>Click here to list other C Programming sites
Note: Examples given above are tested with Turbo C++ Compiler Version 3.0. How ever, we do not give any guaranty for working of the programs in the examples.
The Communication Device Class (CDC) is used for implementing virtual communication ports. This example demonstrates a bridge between a Virtual COM Port on the USB Host Computer and an UART port on the evaluation board.
The following picture shows an exemplary connection of the development board and the USB Host Computer.
The Abstract.txt file contained in the Documentation group of the Project window gives you more information on the general setup and the available I/O on the development board.
Open the example project in MDK. The µVision Project window should display a similar project structure:
Source Files
- VirtualCOM.c contains the main C function that initializes the board hardware and the USB Device Component. Furthermore, it contains the code that exchanges the data internally between the USB and the UART port.
- The USBD_User_CDC_0.c is an adapted code template that implements all necessary file access functions. Refer to CDC: Communication Device Class (ACM) for details about these template functions.
If you are using RTOS other than CMSIS-RTOS2 RTX5 for your project please make sure to satisfy USB Device Resource Requirements.
You may now build and download the example project to the evaluation board using the µVision commands:
- Project –> Build target (F7)
- Flash –> Download (F8)
After these steps, the project should start executing on your evaluation kit. In case of errors, refer to the Evaluation Board User's Guide for configuration information.
Hardware Setup
The setup of the Evaluation Board hardware is described in the Abstract.txt file.
- Verify all jumper settings on the target hardware.
- Connect the UART on the development board to your PC (you might need an USB to serial RS232 adapter). Use an USB cable to connect your development board to the Host PC and power up your board.
- The Welcome to the Found New Hardware Wizard appears. Installation of the driver is described in detail in the Abstract.txt file.
PC Software
The USB Device Virtual COM example can be tested on a Windows PC using a terminal emulation program. Since Hyperterminal in not part of Windows any more, please download an appropriate program for this purpose (such as PuTTY for example). Open the two COM ports 'COMx' and 'COMy'. Any data from 'COMx' will be echoed on 'COMy' and visa versa:
Serial Communication In C
About Host PC driver for Microsoft Windows
The example folder contains two files relevant for driver installation on the Microsoft Windows:
- Driver setup information file (xxx-vcom.inf) which is used to create a digitally signed driver catalog file (xxx-vcom.cat)
- Digitally signed driver catalog file (xxx-vcom.cat)
C Source Code Serial Port Communication Tutorial Download
The driver files are provided as an example, the driver setup information file should be adapted and digitally signed driver catalog file should be created from adapted driver setup information file.
Serial Port Communication Program
Driver setup information file should be adapted in the following way:
C Serial Port Communication
- c251 in Vendor ID VID_c251 entries should be changed to the vendor ID number assigned to your company by the USB organization (c251 Vendor ID is reserved for Keil Software and should not be used)
- xxxx in Product ID PID_xxxx entries should be changed to the product ID as assigned by your company
- in [DeviceList.xxx] sections, entries not relevant for the device, should be removed or added as described below:
- if device is pure CDC class device (not composite) then all entries ending with &MI_xx should be removed
- if device is a composite device with one or more CDC class instances then entries not ending with &MI_xx should be removed and entries ending with &MI_xx should exist for each CDC class instance (example driver contains entries ending with &MI_00 and &MI_02 which are used for composite device containing two CDC class instances and each instance uses 2 interfaces where MI_00 describes first CDC instance and MI_02 entry describes second CDC instance)
- [Strings] section should be changed as desired
C Source Code Serial Port Communication Tutorial Free
- Note
- Vendor ID and Product ID are configured in the USBD_Config_n.c configuration file of the embedded application.
- For producing digitally signed driver catalog file please refer to Microsoft Windows documentation.