C/Serial port programming
Expert: Prince M. Premnath - 6/11/2007
Questionhow to program a serial port in visual c++ and c++.
Answer
Hi Rizwan!
Turbo C/C provides set of functions to perform these kinds of operations over ports !
The following set of functions will do the task pretty well,
inport Read a word from hardware port
inportb Read a byte from hardware port
outport Read a word from hardware port
outportb Read a byte from hardware port here I am summarizing you the general form of the functions hope you can understand well about the arguments and return values !
int inport( int portid);
unsigned char inportb(int portid);
void outport(int portid , int data);
void outportb( int portid , char data);
outportb() function is used to send a byte of data to the port ‘portid’ , where as the function outport() sends a word ( say 2 bytes ) These functions can be used with any ports say parallel , and similarly to receive the data you have to use inport(0 and inportb() functions
in order to use these functions you have to include the header file “dos.h”
biosciom() an alternate way ?
In addition with the function what I have listed above bioscom() is also a function that’s used to establish serial communications over the RS232connector !
Here im presenting the code for bioscom() hope it will help you !
/*
RS232 Bios Calls , the AH value on entry determines the
function to be performed
AH = 0 Reset Port , DX = 0 = com1
Return value in AL
7 , 6 , 5 Baud Rate
000 = 110
001 = 150
010 = 300
011 = 600
100 = 1200
101 = 2400
110 = 4800
111 = 9600
3 , 4 Parity
00 , 10 = OFF
01 = ODD
11 = EVEN
2 Stops
0 = One stop
1 = Two stops
1 , 0 Word size
10 = 7
11 = 8
AH = 1 Emit a character , character in AL
AH = 2 Receive a Character , Return character in AL
AH = 3 Return Status
*/
int bioscom(char Command , char Byte , int Port)
{
int status;
asm mov dx , Port;
asm mov ah , Command;
asm mov al , Byte;
asm int 0x14;
asm mov status , ax;
return status;
}
If you still have any doubts over these please do respond me!
Thanks and Regards
Prince M. Premnath