Serial port's buffer flushing?

I'm working on a program which transmits data through serial port. I ended up making this code. It works good when run for the 1st time, afterwords some strange things happen. Like, when trying to transmit a string, only single character is recieved, sometimes none.
I'm using turbo C.
Someone please help me.

C code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <process.h>
#include<string.h>

#define PORT1 0x3F8

void main(void)
{
	int c,ch,cnt,select;
loop:
	clrscr();
	outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
	outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
	outportb(PORT1 + 0 , 0x0c); /* Set Baud rate - Divisor Latch Low Byte */
	outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
	outportb(PORT1 + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
	outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register */
	outportb(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS */
	cnt=0;
	cout<<"1. Transmission Mode\n";
	cout<<"2. Exit\n\n";
	cout<<"Enter your Option: ";
	cin>>select;
	if(select==1)
	{
		clrscr();
		char str[200]={'\0'},st;
		cout<<"Enter string(less than 200) to trasmit\n";
		gets(str);
		cout<<"\nTotal number of "<<strlen(str)<<" characters entered";
		cout<<"\nPress enter to transmit...";
		getch();
		for(int x=0;x<strlen(str);x++)
		{
			outport(PORT1,str[x]);
		}
		clrscr();
		cout<<"\n\n\t\t\t\tPRESS ESCAPE TO EXIT\n";
		do
		{
			c = inportb(PORT1 + 5);                
			if (c & 1)
			{
				st = inportb(PORT1);
				cout<<st;
				cnt++;
			}
			if(kbhit())
			{
				ch=getch();
			}
		}while(ch !=27);
		cout<<"\nTotal number of "<<cnt<<" characters recieved";
		cout<<"Press enter to continue..."<<getch();
	}                            
	else
	{
	    exit(0);
	}
	goto loop;
}

Last edited on
Hi!

Under Windows I had to use some delay while sending data through serial port. I think you should also use delay.

Like, when trying to transmit a string, only single character is recieved, sometimes none.


So, the sending is OK, isn't it? How do you receive (and check) the sent data?
So, the sending is OK, isn't it?

No, it isn't. Except for the 1st time i transmit and receive, its good.

Looking at the output of this code and thinking over it again and again, i concluded that the buffer is not getting cleaned up rather i'm not using any of such commands in my program... Because i don't know the command.

Please tell me how to clear the serial port's buffer?
Please tell me how to clear the serial port's buffer?


I think that if you send a character and wait some millisecundums, than the buffer will be empty. Present CPUs are very quicker than the serial port.
try the delay instruction.

...

I found a serial port program in Turbo C

http://209.85.135.132/search?q=cache:365EwkcHZnoJ:www.programmersheaven.com/download/6662/35/ZipView.aspx+turbo+c+dos+clear+serial+port&cd=4&hl=hu&ct=clnk&gl=hu
Topic archived. No new replies allowed.