4

I have a Xilinx Virtex-II Pro FPGA board that is attached via RS232 to an iRobot Create.

The iRobot takes a stream of byte integers as commands.

I've found that printf will actually send over the serial port (Hypterminal is able to pick up whatever I print), and I figure that I can use printf to send my data to the iRobot.

The problem is that printf seems to format the data for ascii output, but I'd REALLY like it to simply send out the data raw.

I'd like something like:

printf(%x %x %x, 0x80, 0x88, 0x08);

But instead of the hexadecimal getting formatted, I'd like it to be the actual 0x80 value sent.

Any ideas?

ZacAttack
  • 2,005
  • 5
  • 21
  • 34

3 Answers3

10

Use fwrite:

char buf[] = { 0x80, 0x80, 0x80 };

fwrite(buf, 1, sizeof(buf), stdout);

You can write to any file handle; stdout is just an example to mirror your printf.

On a Posix system, you can also use the platform-specific write function that writes to a file descriptor.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • This is good! But I don't know the address of my stdout,... I suppose I could try digging around. My idea is to just use the memory address of the RS232 RX output... Does that sound like the right idea? – ZacAttack Dec 03 '11 at 02:58
  • Why do you need any address? You were happy to use `printf` earlier, and that's the same as `fprintf(stdout, ...)`... what's the question now? You most definitely can *not* write to any random number. It has to be a `FILE*` that you obtained legally somehow. – Kerrek SB Dec 03 '11 at 02:59
  • Huh... I've been using printf this whole time, but I don't seem to be able to include the stdio.h library, my compiler doesn't seem to like it... I.. am at a loss. Using stdout gives me an error, and including the library gives an error as well. – ZacAttack Dec 03 '11 at 03:13
  • 1
    This seemed to be the answer in the end. I found I could use an equivalent in the platform I was using called STDOUT_BASE_ADDR. I didn't mark it right away because I finally figured out the cord I was using to connect the robot was bad T_T Took hours to figure it out. Thanks again Kerrek! – ZacAttack Dec 03 '11 at 08:39
  • Nice. For long strings, this is much better than the "%c%c%c" format I've been using. – Alex Jul 27 '17 at 14:09
9

Use the format "%c%c%c" instead.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
3

You would use fwrite instead. Printf is by definition an ascii printer.

char buf[] = {0x80, 0x80, 0x80};
fwrite(buf, 1, 3, stdout);

seems to be what you want.

Dave
  • 10,964
  • 3
  • 32
  • 54