3

I tried to open serial port via fopen("serial port path", "+w")

And get the file descriptor by fileno().

After that, i call tcsetattr() but it generate an error showing Inappropriate ioctl for device (perror() print the error message)

I Know i can just use open() but fopen() and it was working with same code.

But i am curious what is a problem.

FILE* file = fopen("serialDevice", "w+");

fd = fileno(file);
if(fd < 0){
    perror("fileno error");
}
struct termios conf;

//setting conf.....

if(tcsetattr(fd, TCSANOW, &conf) != 0){
    perror("tcsetattr() failed");
    exit(1);
}
SangminKim
  • 8,358
  • 14
  • 69
  • 125
  • 1
    Did you check the return value of `fopen()`? The `fileno(3)` man page indicates that `fileno()` never fails (and doesn't set `errno`). – Ulfalizer Mar 30 '15 at 13:45
  • you did not mention which OS, even so, I would expect the 'serialDevice' is not a valid path name. For Linux it would be something llike '/dev/USBtty0' and for windows it would be something like: 'com4' – user3629249 Mar 30 '15 at 14:16
  • in general, a call to tcgetattr() should be first, to retrieve/save the original port settings so they can be restored before exiting the program – user3629249 Mar 30 '15 at 14:19
  • 1
    My OS is `ubuntu 12.04` and `serialDevice` is just to describe simply, i used proper device name so that it was working if i use `open()` with the same code. – SangminKim Mar 30 '15 at 15:25
  • Run both versions under `strace` and see where the behavior diverges. This should reveal the cause. – R.. GitHub STOP HELPING ICE Mar 31 '15 at 05:49

1 Answers1

-3

The only difference between open and fopen relevant to your question is that fopen does buffering by default. This may be your issue.

You can try disabling the buffer and see if it helps, add: setbuf(f, NULL); after you fopen the file and see if it helps.

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
  • No, this is glossing over a lot of more important differences. – R.. GitHub STOP HELPING ICE Mar 30 '15 at 15:03
  • Hi! thanks for the comment, can you explain which other differences that are relevant to this question exist? – Ishay Peled Mar 30 '15 at 15:04
  • Okay, just did some more research... You seem to be wrong R, I refer you to the following resources: http://stackoverflow.com/questions/1658476/c-fopen-vs-open http://www.experts-exchange.com/Programming/Languages/C/Q_20905306.html http://www.unix.com/programming/16622-fopen-open.html – Ishay Peled Mar 30 '15 at 15:12
  • ...And it made me even more sure that the issue is indeed the buffering – Ishay Peled Mar 30 '15 at 15:13
  • "Buffering" is not a mode on the open file. It's a layer that only comes into play if you actually use the stdio functions on the `FILE*` to access the file rather than using the file descriptor. I see no way "buffering" could be relevant to OP's question. – R.. GitHub STOP HELPING ICE Mar 31 '15 at 05:47
  • Please provide references that support your claim. – Ishay Peled Mar 31 '15 at 07:13