Created
March 8, 2018 14:51
-
-
Save tmplt/456d8ccb9976f90b456c1ed79c5001bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <locale.h> | |
#include <termios.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
/* #include "gui.h" */ | |
int configure_interface(int fd) { | |
struct termios tty; | |
memset(&tty, 0, sizeof(tty)); | |
// Get the current options for the port. | |
if (tcgetattr(fd, &tty) != 0) { | |
fprintf(stderr, "error %d from tcgetattr: %s\n", errno, strerror(errno)); | |
return -1; | |
} | |
// Set the baud rates to 19200 | |
/* cfsetispeed(&tty, B9600); */ | |
/* cfsetospeed(&tty, B9600); */ | |
/* tcflush(fd, TCIFLUSH); */ | |
tcflush(fd, TCIFLUSH); | |
// INPCK added.... | |
tty.c_cflag = B9600 | CS8 | CSTOPB | CREAD | CLOCAL | HUPCL | INPCK; | |
tty.c_lflag &= ~(ECHO | ECHONL | ICANON); | |
tty.c_cc[VTIME] = 10; | |
tty.c_cc[VMIN] = 1; | |
cfsetispeed(&tty, B9600); | |
cfsetospeed(&tty, B9600); | |
/* tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars */ | |
/* tty.c_iflag &= ~IGNBRK; // disable break processing */ | |
/* tty.c_lflag = 0; // no signaling chars, no echo, no canonical processing */ | |
/* tty.c_oflag = 0; // no remapping, no delays */ | |
/* tty.c_cc[VMIN] = 0; // XXX: read doesn't block */ | |
/* tty.c_cc[VTIME] = 5; // XXX: 0.5 seconds read timeout */ | |
/* tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl */ | |
/* tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls, enable reading */ | |
/* tty.c_cflag &= ~(PARENB | PARODD | CSTOPB); // disable parity */ | |
if (tcsetattr(fd, TCSANOW, &tty) != 0) { | |
fprintf(stderr, "error %d from tcsetattr: %s\n", errno, strerror(errno)); | |
return -1; | |
} | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
/* gui_loop(); */ | |
char *portname = "/dev/ttyUSB0"; | |
int fd = open(portname, O_RDWR /* | O_NOCTTY | O_SYNC */); | |
if (fd < 0) { | |
fprintf(stderr, "error %d opening %s: %s", errno, portname, strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
if (configure_interface(fd) != 0) | |
return EXIT_FAILURE; | |
/* bool buf[4]; */ | |
/* uint8_t buf = 0; */ | |
int n = 0; | |
uint8_t data = 1; | |
(void)data; | |
uint8_t buf; | |
(void)buf; | |
/* int i = 0, n = 0; */ | |
/* memset(buf, '\0', sizeof(buf)); */ | |
while (true) { | |
n = write(fd, &data, sizeof(data)); | |
if (n == 0) | |
fprintf(stderr, "error: wrote 0 byte(s)!\n"); | |
else | |
printf("wrote %d byte(s)\n", n); | |
/* n = read(fd, &buf, sizeof(uint8_t)); */ | |
/* (void)n; */ | |
/* printf("read: %d\n", buf); */ | |
sleep(1); | |
} | |
close(fd); | |
return EXIT_SUCCESS; | |
} | |
// vim: noexpandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment