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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial Port Programming with C/C++

Status
Not open for further replies.

deanlwvu

Technical User
Jul 23, 2003
25
0
0
US
I am wondering how to get I/O with a serial port in C or C++. All I need is to read incoming data, which is already working. I am currently using the BIOSCOM functions, but that does not work because I am using RS485 instead of RS232. The program works with RS232. What's the easiest/simplest way to do this?

Thanks,
DFL
 
hi,
yesterday too, there was someone working with BIOSCOM !? .

Which OS are you using ?

Probably bioscom() is a too-low-level routine for your current OS, rather than 485 vs 232

bye
 
Well, you're right in the respect that BIOSCOM is too low level. All we need is a way to interface to the serial port without using the bios commands. Can you treat the serial port as a file and send the data to the file?
 
hi,
probably it is obvious for you, but which OS do you use ?
Probably it is Win32 ?
bye
 
I am personally using Win2K, but this program is going to run on a DOS based machine.
 
hi,
if I have understood, the target OS is DOS.

In Win32, is possible, by the CreateFile system call, manage a serial port as a file: it manages also interrupts, to manage handshacking chars or signals

In DOS no; you can use outp() or inp() os open(), but you will lost chars. The best is use a 3rd part package (library) that implements such funcionalities.

bye
 
I was successful in getting this to work using the inportb and outportb functions.

Code:
/* This program is used to pull data from the serial port */

#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x3F8  /* Defines Serial Port Base Address (COM1 */

void main(void){
    unsigned char c = 0;
    unsigned char chrctr = 0;
    /*int exit = 1; */

    outportb(PORT1 + 1, 0); /* Turn off interrupts */

    /* PORT1 Communication Settings */

    outportb(PORT1 + 3, 0x80); /* Set DLAB ON */
    outportb(PORT1 + 0, 0x0C); /* Set the baud rate to 9600 */

    outportb(PORT1 + 1, 0x00); /* Set Baud - Divisor latch HIGH */
    outportb(PORT1 + 3, 0x03); /* 8 bits, no parity, 1 stop */
    outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */
    outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2) */

    printf(&quot;Waiting on transmission from source.\nPress ESC to quit.\n&quot;);

    while(chrctr != 27){      /* Execute the loop if ESC has been hit */
	c = inportb(PORT1 + 5);
	if (c & 0x01){
		chrctr = inportb(PORT1);
		printf(&quot;%d&quot;,chrctr);
		}
	if (kbhit()){
		chrctr = getch();
		outportb(PORT1, chrctr);
		}
    }
    }
 
I would like to ask deanlwvu (TechnicalUser) about this email:

&quot;I was successful in getting this to work using the inportb and outportb functions.


/* This program is used to pull data from the serial port */

#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x3F8 /* Defines Serial Port Base Address (COM1 */

void main(void){
unsigned char c = 0;
unsigned char chrctr = 0;
/*int exit = 1; */

outportb(PORT1 + 1, 0); /* Turn off interrupts */

/* PORT1 Communication Settings */

outportb(PORT1 + 3, 0x80); /* Set DLAB ON */
outportb(PORT1 + 0, 0x0C); /* Set the baud rate to 9600 */

outportb(PORT1 + 1, 0x00); /* Set Baud - Divisor latch HIGH */
outportb(PORT1 + 3, 0x03); /* 8 bits, no parity, 1 stop */
outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */
outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2) */

printf(&quot;Waiting on transmission from source.\nPress ESC to quit.\n&quot;);

while(chrctr != 27){ /* Execute the loop if ESC has been hit */
c = inportb(PORT1 + 5);
if (c & 0x01){
chrctr = inportb(PORT1);
printf(&quot;%d&quot;,chrctr);
}
if (kbhit()){
chrctr = getch();
outportb(PORT1, chrctr);
}
}
}&quot;

I'm trying to communicate (write) serially to a device and believe the stated code might work; but, the operating system I'm using is Windows XP. My question is, do you think the code above will work?

Thanks
 
I think that it should work, running a dos window. I'm using Win2k, and I compiled using Borland Turbo C. It works pretty well, actually, and I would give it a try. Let me know what you find out.

I've also built on this code, sending text files as strings and whatnot. The key here is the setup of the serial port and the sending/receiving of the data.
 
deanlwvu,

thank you for responding. I tried compiling the code and of course I experienced errors. The errors had to do with the inportb and outportb functions. Here's one of the errors:
[Linker error] undefined reference to `outportb'. Do you know what header file contains these functions? I got a feeling our compiler does not include the header files where these functions are defined. Any suggestions?
 
> [Linker error] undefined reference to `outportb'. Do you know what header file contains these functions?
That code is a relic from an old DOS compiler.
If you're using vc++ or something more modern, you need a different approach
Start by saying which compiler you do have


--
 
Like I said, I am using Turbo C (I know, an old compiler) but it's only for a DOS terminal. A small terminal that doesn't run windows or anything like that. Turbo C is all that you need. Now, if you're using dev c++ or vc++, you'll need to use different header files more than likely.

Good luck.
 
I'm using dev c++. Do you happen to know what functions in more contemporary compilers like dev c++ are used for writing to the serial port? I simply want to spit some characters out COM1. Thanks so much.
 
I'm not sure. Inportb and outportb both use header files that don't include inportb and outportb in their definitions.
 
Hmm. Well in the program that you'r using which header file do you think includes inportb and outportb?
 
I should have asked how you got inportb and outportb to work if those functions are not defined in the header file?
Thanks.
 
I'm not sure why mast last reply didn't post, but I was restating my question: How did you get the inportb and outportb function to work if not defined in a header file? Thanks.
 
Deanlwvu,
Thanks for the example code dean, I'm doing a com port proj for school and it helped.

Tekniod,
I think c++ uses this command instead of outportb()
outb(unsigned int [address value],
unsigend int [output value]);

or it might be outp() but definatly one of them.
 
Joe Campbell's book &quot;C Programmer's Guide to Serial Communications&quot; is quite good if you are programming the serial port in dos.

It's not terribly easy to extract what you want from it though.

rgds
Zeit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top