added devel folder..
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
all: client
|
||||
|
||||
client: main.c
|
||||
gcc -std=gnu99 main.c serial.c -o client
|
||||
|
||||
run: client
|
||||
./client /dev/ttyUSB0 9600
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "serial.h"
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
if (argc < 2) { fprintf(stderr, "Missing parameter <device>\n"); return 1; }
|
||||
|
||||
int baud = 9600;
|
||||
if (argc == 3) sscanf(argv[2], "%d", &baud);
|
||||
|
||||
int fd = serial_open(argv[1], baud);
|
||||
|
||||
// Wait for the Arduino to start
|
||||
usleep(1500000);
|
||||
|
||||
// Redirecting stdin/stdout to arduino
|
||||
while(1) {
|
||||
char buf[1];
|
||||
if (fread(buf, 1, 1, stdin) > 0) {
|
||||
serial_write(fd, buf[0]);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
char c;
|
||||
c = serial_read(fd, 10);
|
||||
if (c > 0) {
|
||||
putchar(c);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#define _POSIX_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "serial.h"
|
||||
|
||||
|
||||
/** Open a serial port */
|
||||
int serial_open(const char* port, const unsigned int baud)
|
||||
{
|
||||
int fd = open(port, O_RDWR | O_NONBLOCK);
|
||||
|
||||
struct termios tio;
|
||||
memset(&tio, 0, sizeof(tio));
|
||||
|
||||
speed_t bd = baud;
|
||||
switch(bd) {
|
||||
case 1200: bd = B1200; break;
|
||||
case 1800: bd = B1800; break;
|
||||
case 2400: bd = B2400; break;
|
||||
case 4800: bd = B4800; break;
|
||||
case 9600: bd = B9600; break;
|
||||
case 19200: bd = B19200; break;
|
||||
case 38400: bd = B38400; break;
|
||||
}
|
||||
|
||||
tio.c_cflag = bd | CS8 | CLOCAL | CREAD | CRTSCTS;
|
||||
tio.c_iflag = IGNPAR;
|
||||
|
||||
tcflush(fd, TCIFLUSH);
|
||||
tcsetattr(fd, TCSANOW, &tio);
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
||||
/** Close a serial port */
|
||||
int serial_close(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
|
||||
/** Write a byte. Returns false on failure */
|
||||
int serial_write(int fd, uint8_t b)
|
||||
{
|
||||
return write(fd, &b, 1);
|
||||
}
|
||||
|
||||
|
||||
int serial_puts(int fd, const char* str)
|
||||
{
|
||||
int len = strlen(str);
|
||||
return write(fd, str, len);
|
||||
}
|
||||
|
||||
|
||||
uint8_t serial_read(int fd, int timeout)
|
||||
{
|
||||
uint8_t b[1];
|
||||
do {
|
||||
int n = read(fd, b, 1);
|
||||
if (n == -1) return -1;
|
||||
if (n == 0) {
|
||||
usleep(1 * 1000); // wait 1 ms
|
||||
timeout--;
|
||||
continue;
|
||||
}
|
||||
return b[0];
|
||||
} while(timeout > 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int serial_flush(int fd)
|
||||
{
|
||||
sleep(2);
|
||||
return tcflush(fd, TCIOFLUSH);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/** Open a serial port */
|
||||
int serial_open(const char* port, const unsigned int baud);
|
||||
|
||||
|
||||
/** Close a serial port */
|
||||
int serial_close(int fd);
|
||||
|
||||
|
||||
/** Write a byte. Returns false on failure */
|
||||
int serial_write(int fd, uint8_t b);
|
||||
|
||||
|
||||
/** Send a string */
|
||||
int serial_puts(int fd, const char* str);
|
||||
|
||||
|
||||
/** Read a byte */
|
||||
uint8_t serial_read(int fd, int timeout);
|
||||
|
||||
|
||||
/** Flush */
|
||||
int serial_flush(int fd);
|
||||
Reference in New Issue
Block a user