Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created July 13, 2018 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save projectgus/e3ca94ead47c53c565c3a62029bd183a to your computer and use it in GitHub Desktop.
Save projectgus/e3ca94ead47c53c565c3a62029bd183a to your computer and use it in GitHub Desktop.
rxtest & txtest for CP2102N
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
int main(int argc, const char *argv[])
{
assert(argc == 2);
while (1) {
printf("Opening serial port %s for RX...\n", argv[1]);
int fd = open (argv[1], O_RDWR | O_NOCTTY | O_SYNC);
assert(fd >= 0);
struct termios tty = { 0 };
assert(tcgetattr (fd, &tty) == 0);
cfsetospeed (&tty, B115200);
cfsetispeed (&tty, B115200);
tty.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
INLCR | PARMRK | INPCK | ISTRIP | IXON);
tty.c_oflag = 0;
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
tty.c_cflag &= ~(CSIZE | PARENB | PARODD | CRTSCTS);
tty.c_cflag |= CS8 | CLOCAL | CREAD;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
assert(tcsetattr (fd, TCSANOW, &tty) == 0);
char buf[8+1] = { 0 };
printf("Reading max %d bytes from port...\n", sizeof(buf)-1);
int n = read(fd, buf, sizeof(buf)-1);
printf("Read %d (%s)\n", n, buf);
assert( tcflush(fd, TCIFLUSH) == 0 );
close(fd);
printf("Closed port @ %d.\n", (int)time(NULL));
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
assert(argc == 2);
printf("Opening serial port %s for TX...\n", argv[1]);
int fd = open (argv[1], O_RDWR | O_NOCTTY | O_SYNC);
assert(fd >= 0);
struct termios tty = { 0 };
assert(tcgetattr (fd, &tty) == 0);
cfsetospeed (&tty, B115200);
cfsetispeed (&tty, B115200);
char buf[8];
memset(buf, 'U', sizeof(buf));
printf("Writing to port...\n");
int r;
do {
r = write(fd, buf, sizeof(buf));
} while(r >= 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment