1

I'm trying to communicate some numbers between a Linux program and my Ardunino Uno. To this end I wrote a C++ program which somehow contains:

#include <stdio.h>

int a,b;
a = 1;
b = 2000;

File* serial = fopen("/dev/ttyACM0","w");
fprintf(serial,"%d,%d\n",a,b);
fclose(serial);

Now out of standards I guess, the baud rate is 9600 bit/s. If I use stty -F /dev/ttyACM0 115200 I can change the baude rate of that port to 115200 bit/s. However the communication does not work anymore. My arduino program waiting for number,number does not show any action to the same code as before. Oh, and in the Arduino program of course I changed the code to serial.begin(115200);.

How can I change the baude rate in such a setup? If it is a concern, I'm using Ubuntu and an Arduino Uno.

  • 1
    Also take into account that default settings may mean that opening the port resets the Arduino. There will then typically be a brief delay before the bootloader hands over control to your sketch, so anything your program writes immediately after a port-open-reset will be lost and never seen by your sketch. You can add a delay before writing your data or change the modem control signal settings to not trigger a reset (or at least not trigger one on subsequent port openings). Even more reliable might be to open the port bidirectional and wait to receive a ready prompt from the sketch. – Chris Stratton Mar 11 '16 at 14:50
  • Thanks for the highly relevant hint! Could you maybe elaborate more on how the reset trigger is called? Or where I can disable it? At the time beeing a bidirectional opening is more hasstle, than service, but I'll have it in mind for the future. – mike Mar 13 '16 at 8:25
3

Setting baud rates in a C program isn't a trivial task. You need to use the low-level open() instead of fopen() and act on the returned file descriptor with ioctl() or tcsetattr() / tcgetattr(). The former method allows the setting of non-standard baud rates while the latter only allows specific baud rates to be used.

I wrote a C++ class to deal with it all for me a while back. You are welcome to examine the methodology and/or steal whatever bits of it you like to use in your own program.

0

Actually, I tried it again with exactly the same settings and it worked. It seems, as if Linux uses the settings of ssty, when referring to the "file" of a serial connection.

Thanks anyway!

  • Settings do tend to persist. You may want to reboot your system and see that it still works, and isn't only working because of settings set up by some program you ran during your experiments. Programs that use the serial port are split in behavior - some leave their settings behind, others (at least on a normal exit) will set things back however they found them. Also be aware that the necessary difference from bootup defaults may be more than baud rate - there is a lot about word format and character mapping and special casing to contemplate, too. – Chris Stratton Mar 11 '16 at 14:47
  • Thanks, using stty -F /dev/.. I tried to verify the baud rate after a reboot, but it is reset to 9600 baud. I didn't use any other related programs to change settings. So I guess I have to write a script to change the rate at bootup. – mike Mar 13 '16 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.