Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I open a serial port?

Status
Not open for further replies.

cruicu

Technical User
Nov 25, 2002
24
RO
Hello all!
I need some help for open a serial port and sent some characters(in ansii and hex) to it.May be will possible to receive some characters from this port.
Can somebody give me a sample of code for that?
Any advice is very good.
Thanks in advance.
Costica
 
Depends on the operating system. For UNIX, you generally use open, write, read, close (see the man pages). For Windows, CreateFile, WriteFile, ReadFile, CloseHandle (see the SDK documentation and/or msdn.microsoft.com).

 
u have some dos services to do a char send and receive
you can u int86x function in c to execute those services
I can remember only the interrupt number as of now ..its 21(hex) ..

i have written a few programs which uses serial port ..if u want i can get u one


 
Thank you Robinhood361!
I would appreciate if you can give me some example programs.
 
sorry i was not able to extract the code...I think i may have deleted it ! however i'll give u an overview how to go about..

first u need to decide two things
1) serial port number which u r going to use
2) baud rate (speed at which u'll transmit)

there are a lot of other factors which u can ignore as of now

address of serial ports:
--------------------------

Serial port 1: 0x02F8
Serial Port 2: 0x03f8

(default values ...can be changed at boot setup)


Lets choose port 1. and baud rate as 600

You need to follow the steps below

A) For the sending computer

1) initialize port
2) send a ready signal to the other computer
3) receive a ready signal from other computer
4) transmit a byte of data
5) follow steps 2 to 4 in a loop until whole data is transferred
6) send a "End" signal to receiving computer



B) For the receiving computer

1) initialize port
2) wait for a ready signal from other computer
3) receive a ready signal from other computer
4) send a ready signal to other computer
4) receive a byte of data
5) follow steps 2 to 4 in a loop until "End" signal is received

This logic is a very amaturish logic but will serve your purpose for small data transmission

Now lets see the coding part

A) Initializing port
------------------------
you need to run int86 function for this

#include<dos.h>

union REGS iregs,oregs;
void InitPort(void)
{
iregs.h.ah = 0;
iregs.h.al = 0x63; /* explained below */
iregs.x.dx = 0; /* 0 for port 1; 1 for port 2 */
int86(0x14,&iregs,&oregs)
}

iregs.h.al is assigned a hex value of 63. This is a setup value for the transmission which determines the baud rate, parity bit, stop bits, word length etc. Its is important to note that this value must be same on both the machines.


hex value of 63 sets the baud rate to 600 baud and word length to 8 bits

B) Sending a ready signal
------------------------
Ready signal can be defined by you..choose an ASCII value which is not among you transmission data

#include<dos.h>
#define READY Ó /* this is ascii character 224 */

main()
{
initport();
............
...........
SendCharacter(READY);
..........
...........

}

void SendCharacter(char Value)
{
outp(0x03f8,Value); /* defined in dos.h */
}


C)Receive a ready signal
------------------------
Ready signal must be same on both computers

#include<dos.h>
#define READY Ó /* this is ascii character 224 */

main()
{
char ch;

ch = '';
initport();
while(ch != READY) /*wait until READY is received */
ReceiveCharacter(ch);
..........
...........

}

char ReceiveCharacter(void)
{
return(inp(0x03f8)); /* defined in dos.h */
}

Similary you can define your own &quot;End&quot; signals on both machines and transmit them with the functions defined above.
The data bytes can also be transferred using same functions.


 
You will almost certainly not need DOS interrupts to do serial communications on your system. DOS interrupts will not work, as far as I know, for Win32 programs.
 
It's going to remain a rather academic exercise until cruicu says which OS/Compiler is being used for this task
 
yes ofcourse..this is not for win32 programs..
this will work only with dos version of C

I think VB is the best option for building win32 applications for serial communications.
You'll get a wealth of info about that on web with example codes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top