0

I have a problem with sending binary data via ATxMega128A1. I want to use the stdout with fwrite function, if possible. But I'm not able to set it in binary mode and every value that is 0x0a, is automatically converted to 0x0d 0x0a...

I'm totally frustrated, because all the wonderful solutions I found (fopen(..., "wb") or fdopen(dup(fileno(stdout)), "wb"); from enter link description here or freopen(NULL, "wb", stdout) and so on) won't work on avr-gnu-toolchain.

Can anyone help me please to transfer my array of simple binary data?

uint8_t data[10];
data[0] = 1;
data[1] = 2;
data[2] = 3;
data[3] = 10;
....
data[8] = 3;
data[9] = 5;
fwrite(data, 10, 1, stdout);
// receiving of 10 bytes at receiver:
// 0x01 0x02 0x03 0x0d 0x0a ... 0x03

As you can see the written data to serial port is appended by a leading 0x0d before the 0x0a and my last value is missing on receiver's side, when reading 10 bytes.

It is bad, that the interface automatically alters the data. Is there a suitable solution to tell stdout to go in "stupid" binary mode and just transfer the data as they are?!

Best regards

and I hope for a good solution

WPFGermany
  • 1,639
  • 2
  • 12
  • 30
  • do not use standard library file functions on tiny uCs as implementations are often very very "interesting". – 0___________ Aug 26 '21 at 21:35
  • 2
    what's in data[2]? – Bill Morgan Aug 27 '21 at 02:29
  • If you are using avr-libc: Did you look into the [function `putc()`](http://www.nongnu.org/avr-libc//user-manual/group__avr__stdio.html#stdio_note2)? – the busybee Aug 27 '21 at 06:17
  • @BillMorgan: Thanks, I've added the missing datafield. – WPFGermany Aug 27 '21 at 06:37
  • @thebusybee: I tried. got the same issue, that `0x0a` gets an additional `0x0d` by the `stdout`. The stdout is in ASCII mode and I'm unable to set it to binary. The ASCII mode in `stdout` automatically converts a `0x0a` to `0x0d 0x0a`. – WPFGermany Aug 27 '21 at 06:40
  • Are you sure that the "\r" in the sent data? Did you use an oscilloscope to watch the line? Sometimes your receiving program converts, what do you use to check the transmission? – the busybee Aug 27 '21 at 07:42
  • The documentation of avr-libc says that your implementation of `putc()` is responsible to add "\r". Is your version clean? – the busybee Aug 27 '21 at 07:44
  • Oh, and do you really receive just 10 bytes, with the last byte of 0x05 missing? That is really strange. -- What does `fwrite()` return (should be 1)? – the busybee Aug 27 '21 at 07:53
  • I observed the behavior with debuggers at different receivers (in C, Python, Matlab and so on). Also received with termite and other tools... Everywhere I see an `0x0d` before the `0x0a`, which is not in my databuffer given to fwrite, fputc or fprintf and all the other functions... The stream `stdout` in ASCII mode is doing this (independent from the method used to send the buffer...). – WPFGermany Aug 27 '21 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236475/discussion-between-the-busybee-and-wpfgermany). – the busybee Aug 27 '21 at 08:24
  • @WPFGermany it is tiny 8 bits uC. It has 2k RAM. No one sane uses anything from the `stdio`. Write your own small function to output via UART or other interfaces. Look at examples. Do not use this monsters from stdlib on AVR uCs. – 0___________ Aug 27 '21 at 08:41
  • @0___________ As long as you fulfill all requirements (performance, RAM) there is no problem to use anything that eases the development. You cannot compare a standard stdlib with avr-libc. – the busybee Aug 27 '21 at 09:35
  • you may need to setup the output stream. Have a look at the avr-libc documentation https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html – Bill Morgan Aug 27 '21 at 12:18
  • @billmorgan: I have read the avr stdio before. The problem is, that fdevopen doesn't provide a mode to set, like classical fopen with mode 'wb'. Ofcourse I could do all the uart stuff manually, but I'm wondering, that the stream stdout alters the userdata. In my mind it is an interface and should not care about my data in the buffer. – WPFGermany Aug 27 '21 at 14:47
  • For now I implemented a workaround at receiver side to remove the added data. But that is a stupid solution in my mind. The goal is to transfer the data fast without slow string encoding and decoding. – WPFGermany Aug 27 '21 at 14:51
  • I think you need to point the stdout stream to use your uart code. did you do this? FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); stdout = stdin = &uart_str; – Bill Morgan Aug 27 '21 at 15:44

0 Answers0