This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

fprintf that outputs all characters to the serial port at one time

Guru 20045 points

Hello,

I am trying to create an fprintf that outputs all characters at once to the serial port.

I thought that the easiest way to do this is to create a function name fprintfm that takes the same parameters as fprintf.  The fprintfm function would call fprintf with the parameters passed to fprintfm.   Then, I would code the fputc function so that all characters passed to it are put it are put in a buffer.  The buffer could then be passed to the serial ports driver send function.

I tried to test my idea with the code shown below and it doesn't seem to be working as can be seen by content of the fputcBuffer.

What am I doing wrong and is there a better way to do it.  Also, how do I format code in my post?

Thanks,

Stephen

 
fputcBuffer = 0x000085C0@Program
 [0 ... 99]
  [0] = 0x9A08
  [1] = 3
  [2] = )
  [3] = 3
  [4] = 0x0000
  [5] = 0x0000
  [6] = .
  [7] = .
  [8] = .
  [9] = .
  [10] = .
  [11] = .
  [12] = .
  [13] = .
  [14] = .
 [100 ... 127]

[code]

#include <stdio.h>

#include "format.h"

#include <stdarg.h>

signed char fputcBuffer[128];

int fputcBufferIndex = 0;

struct __FILE {    

      int handle; 

};

FILE __stdout;

int fprintfm(FILE *_fp, const char *_format, ...) {

    va_list  _ap;

    int rval;

    va_start(_ap, _format);

    rval = fprintf(_fp, _ap); 

    va_end(_ap); 

    return rval;

 }

int fputc(int ch, FILE *f) { 

   fputcBuffer[fputcBufferIndex++] = (signed char)ch;

   return ch;

}

int main(void) { 

   FILE pFile = __stdout; 

   int a = 20;   

   fprintfm(&pFile, "\n\rError - ???!!!%d",a);

}

[\code]