read and write thru serial ports

Posted on 2003-03-31
Medium Priority
Last Modified: 2013-11-15
I need to send the data of the file through a serial COM1 port to a FPGA that has a mpeg-2 decoder running on it. The mpeg decoder will then send the decoded stuff back thru the COM2 port. So what i have is a program running on a PC that is converting the file (.m2v) into a bitstream that is to be sent thru the COM1 port.

The following are to be taken care of :

(1) I am to use GCC only.

(2) The data to be read from file should be sent thru COM1.

-- To check whether the program (that is yet to be made) works or not, i will connect COM1 to COM2 and then send thru COM1 and recieve thru COM2.

AIMS & Objectives :

-- read data from a file (say input.txt) and send this data thru COM1.
-- recieve data from a serial port (say COM2) and write this into a file.

I need :

(1) Program that reads file in 'n' bytes at a time. This is pretty simple and i have a version made by myself.

(2) Program (This is Important) that sends this read data thru the serial port. (and recieves too)

Remember : All in GCC.



Email : anshuman4u@rediffmail.com (feel free to email me, this is urgent :D )
Question by:anshuman4u
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

honey_hamster earned 80 total points
ID: 8240913
I've got code for COM1 (PORT1), but the exact same code holds for COM2.  Note that this will work for Windows 98 and 95, but not 2000, NT, or XP because those ones don't like you to monkey directly with the ports.

#define PORT1 0x3f8
#define PORT2 0x2f8

static void rs232_open_com1_115k( void )
  outportb(PORT1 + 3 , 0x80);  /* SET DLAB ON */
  outportb(PORT1 + 0 , 0x01);  /* 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, and OUT2 */

/* Baud Divisor
   110  0417h=1047
   300  0180h=384
  1200  0060h=96
  2400  0030h=48
  9600  000Ch=12
 19200  0006h=6
 38400  0003h=3
 57600  0002h=2
115200  0001h=1


static void rs232_write_com1( unsigned char data )
  outportb(PORT1, data);

  while( ( inportb( PORT1 + 5 ) & 0x20 ) == 0 )

static unsigned char rs232_read_com1( void )
  while( ( inportb( PORT1 + 5 ) & 0x01 ) == 0 )

  return( inportb( PORT1 ) );


Expert Comment

ID: 8240944
And to add to my previous post, it's probably easiest to write 2 programs:
1 for writing COM1
1 for reading COM2

That way, the simple blocking functions coded above can be used.

If you need to have all the code running in a single program, then you'll need to add polling to ensure that COM1 is ready to transmit a new byte before calling rs232_write_com1(), and that COM2 has data ready to be retrieved by rs232_read_com2().
LVL 20

Expert Comment

ID: 10199334
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by honey_hamster.

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
This video shows how use content aware, what it’s used for, and when to use it over other tools.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question