Skip to content

POSIX Based Testing

Warren Gay edited this page Nov 13, 2015 · 2 revisions

Testing under Linux/Unix/Mac OSX and even Cygwin, you can use the following template to open and use the serial port device connected to your ESP8266.

Include Files

#include <stdlib.h>     
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <assert.h>

Open the device

int fd;

fd = open("/dev/whatever",O_RDWR);
if ( fd == -1 ) {
    fprintf(stderr,"%s: Opening serial device %s for r/w\n",
        strerror(errno),
        opt_device);
        exit(3);
}

Set Raw Mode

int rc, baudrate = 115200;

rc = tcgetattr(fd,&ios);
assert(!rc);
cfmakeraw(&ios);
cfsetspeed(&ios,baudrate);
ios.c_cflag |= CRTSCTS;         // Hardware flow control on
    
rc = tcsetattr(fd,TCSADRAIN,&ios);
if ( rc == -1 ) {
    fprintf(stderr,"%s: setting raw device %s to baud_rate %d\n",
        strerror(errno),
        "/dev/whatever",
        baudrate);
    exit(2);
}
Clone this wiki locally