USB Serial Communication In C/C++

Hello all, I am new to using USB port communications on the Mac; and I was wondering how do I open read and write to an Arduino or similar board with C++ on the MacOS? I know how to do this in python but I HAVE to do it in C/C++ for my particular project.

I have read docs like these but they are very outdated: https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/USBBook/USBOverview/USBOverview.html#//apple_ref/doc/uid/TP40002644-BBIHAIAG

Anything will help

thanks!

I think I know what you mean but is this what you mean???

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/asio/serial_port_base.hpp>
#include <boost/asio/streambuf.hpp>

using namespace std;
using namespace boost::asio;

int main()
{
    io_service io;
    serial_port sp(io, "/dev/ttyACM0");
    sp.set_option(serial_port_base::baud_rate(9600));
    sp.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
    sp.set_option(serial_port_base::parity(serial_port_base::parity::none));
    sp.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
    sp.set_option(serial_port_base::character_size(8));

    streambuf buf;
    write(sp, buffer("Hello World")
    read_until(sp, buf, "");

    string data = buffer_cast<const char*>(buf.data());
    cout << data << endl;
}

or perhaps



#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
    int fd;
    char *portname = "/dev/tty.usbmodem1421";
    char buf[256];
    int n;
    int i;
    int count = 0;
    int baudrate = B9600;
    struct termios toptions;

    fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("serialport_init: Unable to open port ");
        return -1;
    }

    if (tcgetattr(fd, &toptions) < 0) {
        perror("serialport_init: Couldn't get term attributes");
        return -1;
    }
    speed_t brate = baudrate; // let you override switch below if needed
    switch(baudrate) {
        case 4800:   brate=B4800;   break;
        case 9600:   brate=B9600;   break;
        #if defined B14400
        case 14400:  brate=B14400;  break;
        #endif
        case 19200:  brate=B19200;  break;
        #if defined B28800
        case 28800:  brate=B28800;  break;
        #endif
        case 38400:  brate=B38400;  break;
        case 57600:  brate=B57600;  break;
        case 115200: brate=B115200; break;
    }
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    // no flow control
    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c
[...]

oflag &= ~OPOST; // make raw

    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;

    if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
        perror("init_serialport: Couldn't set term attributes");
        return -1;
    }

    while (1) {
        n = read(fd, buf, 255);
        if (n > 0) {
            buf[n] = 0;
            printf("read %i bytes: %s

", n, buf);
        }
        if (count == 0) {
            n = write(fd, "Hello!

", 6);
            if (n < 0) {
                perror("Write failed");
            }
            count++;
        }
        usleep(100000);
    }

    return 0;
}


USB Serial Communication In C/C&#43;&#43;
 
 
Q