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 to communicate to COM1/COM2 port?and read/write data to it? 1

Status
Not open for further replies.

sasj0e

IS-IT--Management
Nov 6, 2002
82
Hi,
I need to communicate to RS232 ports and read/write data through it.Is there any way in which, I can get this done through coding in C using turbo C? I need to open the COM port, then read through it,and write also.write could be interpreted but read is not interpreted.
Please let me know as early as possible.
Regards,
Sas
 
> using turbo C?
Are you running on real DOS, or a win9x/NT/2K/XP console.

I would only use Turbo C for backward compatibility with some really old code really running on DOS.

For new code, I would suggest a newer compiler, which has access to the win32 API for accessing COM ports.

for many newer and free compilers.
 
Have a look at the functions 'outp' and 'inp' if your are using the DOS or consoles from windows.

 
/********************************************/
/* Text.h */
/********************************************/
#include <conio.h>
#include <stdio.h>
class text
{

int x1,y1,x2,y2;

public:

text()
{
window(1,1,80,25);
textbackground(0);
textcolor(15);
clrscr();
gotoxy(1,1);
printf(&quot;%s&quot;,&quot;COM-COM COMMUNiCATOR&quot;);
gotoxy(1,25);
printf(&quot;%s&quot;,&quot;(`) is for exit&quot;);
window(1,2,80,13);
x1=y1=1;
textbackground(1);
clrscr();
window(1,14,80,24);
x2=y2=1;
textbackground(2);
clrscr();
}

void me(int xx)
{
window(1,14,80,24);
textbackground(2);
textcolor(4);
gotoxy(x2,y2);
if (xx==13) cprintf(&quot;\n\r&quot;); else cprintf(&quot;%c&quot;,xx);
x2=wherex();
y2=wherey();
}

void he(int xx)
{
window(1,2,80,13);
textbackground(1);
textcolor(3);
gotoxy(x1,y1);
if (xx==13) cprintf(&quot;\n\r&quot;); else cprintf(&quot;%c&quot;,xx);
x1=wherex();
y1=wherey();
}

};

//--------------------------------------------------------

/********************************************/
/* Terminal.cpp */
/********************************************/
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include &quot;text.h&quot;

int com[4]={0x3f8,0x2f8,0x3e8,0x2e8};
union REGS regs;
text *t;
int c;

void init(int xx);

void interface()
{
textbackground(0);
clrscr();
window(30,15,50,20);
textbackground(5);
clrscr();
cprintf(&quot;CHOOSE COM:\n\n&quot;);
cprintf(&quot;\roCOM1\n&quot;);
cprintf(&quot;\roCOM2\n&quot;);
cprintf(&quot;\roCOM3\n&quot;);
cprintf(&quot;\roCOM4&quot;);
gotoxy(1,3);cprintf(&quot;v\r&quot;);
char m=1;
int y;
do
{
m=getch();
m-=49;
y=3+m;
if (m<4&&m>=0) {cprintf(&quot;o&quot;);gotoxy(1,y);cprintf(&quot;v\r&quot;);}
}
while (m!=13-49);
init(wherey()-3);
c=wherey()-3;
t=new text;
}

void init(int xx)
{
outport(com[xx]+3,0x80);
outport(com[xx],12);
outport(com[xx]+1,0);
outport(com[xx]+3,0);
outport(com[xx]+3,3);
}

void out(int xx)
{
int x;
if (x=inport(com[xx]+5)&1,x!=0)
t->he(inport(com[xx]));
}

void in(int xy)
{
int x;
regs.h.ah=6;
regs.h.dl=0xff;
int86(0x21,&regs,&regs);
x=inport(com[xy]+5)&0x20;
if (regs.h.al!=0&&x==0x20)
{
t->me(regs.h.al);
outport(com[xy],regs.h.al);
}
}

void main()
{
interface();
do
{
in(c);
out(c);
}
while (regs.h.al!=96);
}

 
Please post C++ solutions to the C++ windows or C++ unix groups.
This is a C forum.
 
This is highly platform/compiler specific stuff. If you're on a Windows platform, probably best to use the Windows API functions.

(Note: Error checking omitted for the sake of brevity)


#include <stdio.h>
#include <windows.h>

#define PORT_NAME &quot;COM1&quot;

int main(void)
{
HANDLE port;
DCB dcb;
COMMTIMEOUTS time_outs;

/* Open port for read/write access. */
port = CreateFile(PORT_NAME, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);


/* Set port to desired baud/data bits/parity/stop bits */
GetCommState(port,&dcb);

/* Set to 9600/8/N/1 */
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

SetCommState(port, &dcb);


GetCommTimeouts(port,&time_outs);

/* ReadFile returns after 3 seconds if no data is received
* on the interface. */
time_outs.ReadIntervalTimeout = MAXDWORD;
time_outs.ReadTotalTimeoutMultiplier = MAXDWORD;
time_outs.ReadTotalTimeoutConstant = 3000;

SetCommTimeouts(port,&time_outs);


{
#define READ_BUF_SIZE 200

char read_buf[READ_BUF_SIZE];
DWORD count;

/* Read some data. */
ReadFile(port, read_buf, sizeof read_buf, &count, NULL);
printf(&quot;%d bytes read\n&quot;, (int) count);

/* Write some data. */
WriteFile(port, &quot;hello&quot;, sizeof &quot;hello&quot; - 1, &count, NULL);
printf(&quot;%d bytes written\n&quot;, (int) count);
}
return 0;
}








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top