init
This commit is contained in:
parent
a076aad170
commit
43acbb95df
58
src/main.c
58
src/main.c
@ -0,0 +1,58 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
|
||||
int main() {
|
||||
int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
perror("open error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct termios tty;
|
||||
tcgetattr(fd, &tty);
|
||||
|
||||
tty.c_cflag &= ~CSIZE; // clear bit size
|
||||
tty.c_cflag |= (CLOCAL | CREAD | CS8 | B115200);
|
||||
|
||||
tty.c_lflag = 0; // raw mode
|
||||
tty.c_iflag = 0;
|
||||
tty.c_oflag = 0;
|
||||
|
||||
tcsetattr(fd, TCSAFLUSH, &tty); // aplly settings
|
||||
|
||||
char linebuf[512];
|
||||
int linepos = 0;
|
||||
char buf[128];
|
||||
float v = 0.0f, a = 0.0f;
|
||||
while (1) {
|
||||
int n = read(fd, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = buf[i];
|
||||
|
||||
if (linepos < (int)sizeof(linebuf) - 1) {
|
||||
linebuf[linepos++] = c;
|
||||
}
|
||||
|
||||
if (c == '\n') {
|
||||
linebuf[linepos] = '\0';
|
||||
|
||||
float tmp_v, tmp_a;
|
||||
|
||||
// expected format: "12.34,56.78\n"
|
||||
if (sscanf(linebuf, "%f,%f", &tmp_v, &tmp_a) == 2) {
|
||||
v = tmp_v;
|
||||
a = tmp_a;
|
||||
printf("latest: %f %f\n", v, a);
|
||||
}
|
||||
linepos = 0; // clear buffer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
usleep(1000);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user