Convert file to bitstream and send thru serial port

Posted on 2003-03-30
Medium Priority
Last Modified: 2013-11-15
I am to read a m2v file and send it thru serial port. The mpeg2decoder that i am using reads a bunch of bytes at a time( say n) . I want to read the m2v file 'n' bytes at a time and then send it thru a serial port to an FPGA board where a decoder will be running. How should i go about it ?

Problems :
(1) I am facing problems in reading a file in 'n' bytes at a time.
(2) Even if i am successful in doing that, how do i send it thru a serial port (say a modem port coneected to an FPGA)

Thanx in advance. Help me out pls.

Email : anshuman4u@rediffmail.com
Question by:anshuman4u

Expert Comment

ID: 8233753
The first problem is easy.
#define NDATA 1024

  FILE *f;
  char data[NDATA];
  int nread;
  f=fopen("MYDATA.DAT", "r+b");
  do {
     nread = fread(f, NDATA, 1, data);
     /* you got the data as many as nread (max = NDATA)
        should be less at the end of file */

    /* do something with your data */

  } while (!feof(f));

The second is a little bit complex.
What computer/OS/compiler are you working on ?


Accepted Solution

Kocil earned 60 total points
ID: 8233760
And what is FPGA ?
Does it have a standard protocol to receive the file (say kermit or xmodem), or will you write a receiver program for it too ?

Expert Comment

ID: 8237715
Use one array of kind as follows

unsgined char DATA[MAX_SIZE];

Depends on the device connected at the serial port decide the transfer method of data.
If you want to send the data bit by bit do something as follows.

//Get the data
unsigned char data = DATA[i];

The output of below code will be a binary string equivallent to  the data we got from DATA array.

char *convertobinary()
     unsigned char ch = ( unsigned char ) num ;
     unsigned char mask = UCHAR_MAX - ( UCHAR_MAX >> 1 ) ;
     static char result [CHAR_BIT + 1] ;
     char *store = result ;
     while ( mask )
          *store++ = ( char ) ( '0' + ( ( ch & mask) != 0 ) ) ;
          mask >>= 1 ;
     *store = '\0' ;
     return result ;

  Use the binary string to encode bit stream to be sent to the output. Now use C apis of outp or outport to emit data to the port.
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.


Expert Comment

ID: 8238128
A FPGA is a Digital logic device it stands for Field Programmable Gate Array.

You can think of it as a big digital microcontroller.

FPGA's can be programmed with software to be any digital circuit element you want.  They can be programmed in VHDL (visual hardware description language) or sometimes in a schematic form.  

The software required to program the FPGA is usually very expensive.  At my university we use Xilinx FPGA's and their sofware.

FPGA's contain millions of logic gates and there are joined together by the software (during programming the chip) to give the functionality required.

In my experience with them I have made a RS232 monitor and various other 'State Machines'.  They are very flexable and you can create pretty much whatever you like.

As far as protocol, the FPGA does whatever it is programmed to.  (Depending on the board that its attached to and its inputs).

You will have to look at what the board does and see how the FPGA's been programmed.

Hope this helps

Expert Comment

ID: 8243418
If you're device willing to take bits coming at the port why cant you send the binary stream to the port? Do you've a standard adapter to connect to the RS232 port? If the voltage levels are matching you can pass bit by bit. If suitable encoding methods not present in the device you'll have to use IC555 for making the pulses in your requiremnts. Get the standard protocol first then make the frames to communicate. Once the frames are ready you can send them with standard APIs inp,outp etc. Or win32 apis WriteFile(). By protocol I meant there will be some start stop parity bit structures for your device.

LVL 20

Expert Comment

ID: 10199304
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 Kocil [grade B] (only partial answer).

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today as you open your Outlook, you witness an error message: “Outlook is using an old copy of your Outlook Data File…”. Probably, Outlook is accessing an old OST file.
A discussion about four distinct advantages of using a Custom Software Development solution.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
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…

599 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