Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

read and write thru serial ports

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 )
  • 2
1 Solution
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 ) );

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().
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now