custom baudrate in native C application

If you have specific questions or problems with any of my Freeware applications, post them here.
Post Reply
dominggus
Posts: 4
Joined: Wed May 31, 2023 1:02 am

custom baudrate in native C application

Post by dominggus »

Hi Roger,

I am writing a native C application that runs on linux on a Raspberry Pi 4 (arm64 and armv7), but it should also run on my MacBook Pro (intel). The application is using a serial port: FTDI chip -> RS485 chip which is communicating to a bunch of microcontrollers over a bus.

The problem is that the source code that to open the serial port works on linux, but NOT on OSX.
The binary built for raspberry armv7 and arm64 work and when I on my Macbook (intel) boot into Ubuntu, the code also opens the serial port properly.

When I compile and run the source code on OSX, it seems like the application cannot receive the bytes properly:
I can see that the microcontroller receives bytes properly, and also sends data back properly, but in OSX nothing is received (the TX led of the FTDI adapter also does not blink, RX does however).

So, it's kind of a long stretch but since termios.h documentation for OSX is pretty sparse I am quite desperate, and since CoolTerm is working so smoothly on all platforms, I'd like to ask you:

How did you manage to support custom baudrates in CoolTerm on OSX ? or more specifically: how can I open up a serial port on OSX with a 921600 baudrate in native C?
dominggus
Posts: 4
Joined: Wed May 31, 2023 1:02 am

Re: custom baudrate in native C application

Post by dominggus »

I can share what we have now:

Code: Select all

#include "termios.h"

connect("/dev/tty.usbserial-A50285BI", B921600);

void connect(const char *port, uint32_t baudrate)
{
    printf("Open port: %s\n", port);

    serialPort = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (serialPort == -1)
    {
        perror("FAILED ");
        exit(1);
    }

    fcntl(serialPort, F_SETOWN, getpid());
    fcntl(serialPort, F_SETFL, (FNDELAY | FASYNC));

    struct termios options;

    tcgetattr(serialPort, &options);

    cfsetispeed(&options, (speed_t) baudrate);
    cfsetospeed(&options, (speed_t) baudrate);

    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CRTSCTS;

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    options.c_iflag &= ~(IXON | IXOFF | IXANY | INPCK | ISTRIP | IGNBRK | BRKINT | PARMRK | IGNCR | ICRNL);

    options.c_oflag &= ~OPOST;

    options.c_cc[VMIN] = 0;
    options.c_cc[VTIME] = 0;

    tcsetattr(serialPort, TCSANOW, &options);
}
dominggus
Posts: 4
Joined: Wed May 31, 2023 1:02 am

Re: custom baudrate in native C application

Post by dominggus »

ah, i just found out you write CoolTerm in Xojo, which probably means you're using https://documentation.xojo.com/api/hard ... ction.html, so you probably won't have an answer to my question - which is also ok though :)
User avatar
roger
Site Admin
Posts: 431
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: custom baudrate in native C application

Post by roger »

Yes, this is exactly what I'm using. It works very well, cross-platform, with (almost) no headaches :-)
dominggus
Posts: 4
Joined: Wed May 31, 2023 1:02 am

Re: custom baudrate in native C application

Post by dominggus »

one last question, though bit offtopic:

I read on reddit that xojo in general offers cross-platform, though you still have to create a project per platform, and code sharing between them is pretty limited - how is your experience with xojo from a developers perspective?
User avatar
roger
Site Admin
Posts: 431
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: custom baudrate in native C application

Post by roger »

dominggus wrote: Thu Jun 01, 2023 1:15 am I read on reddit that xojo in general offers cross-platform, though you still have to create a project per platform, and code sharing between them is pretty limited - how is your experience with xojo from a developers perspective?
That is not really accurate. CoolTerm is a single Desktop project, and I generate all the binaries for macOS, Windows, Linux, and RaspberryPi from the same project.
It's different for mobile platform of course. You can write apps for iOS and Android, but you would do that with a mobile project, rather than a Desktop project.
Post Reply