serial : a small test main()

This commit is contained in:
2018-12-12 20:07:49 +01:00
parent 933365b883
commit cda7b7f45e
7 changed files with 54 additions and 9 deletions

View File

@@ -1,9 +1,9 @@
OPT = -Wall -DDEBUG_LEVEL=1
serial.o: serial.c serial.h Makefile
gcc -Wall -c $<
gcc ${OPT} -c $<
t: t.c serial.o Makefile
gcc -Wall $< serial.o -o $@
gcc ${OPT} $< serial.o -o $@

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
@@ -84,7 +85,7 @@ if (uart0== -1)
baudbits = baudrate2const(baudrate);
#if DEBUG_LEVEL
fprintf(stderr, "%d -> %x\n", baudrate, baudbits);
fprintf(stderr, "%d -> 0x%04x\n", baudrate, baudbits);
#endif
tcgetattr(uart0, &options);
options.c_cflag = baudbits | CS8 | CLOCAL | CREAD; //<Set baud rate
@@ -98,7 +99,7 @@ return uart0;
}
/* -------------------------------------------------------------------- */
/*
* maybe we have to check for timeout ?
* this function have NO timeout !
*/
int getbyte(int fd)
{
@@ -115,3 +116,24 @@ return (int)byte;
}
/* -------------------------------------------------------------------- */
/* timeout is in milliseconds */
int getbyte_to (int fd, int to_ms)
{
unsigned char byte;
struct timeval timeout;
timeout.tv_sec = to_ms / 1000;
timeout.tv_usec = (to_ms % 1000) * 1000;
#if DEBUG_LEVEL
fprintf(stderr, "timeout %6d is %4ld %6ld\n", to_ms,
timeout.tv_sec, timeout.tv_usec);
#endif
return -3;
}
/* -------------------------------------------------------------------- */

View File

@@ -1,8 +1,15 @@
/*
* serial.h
* --------
*
*/
int prepare_UART(char *port, int bauds);
int getbyte(int fd);
/* timeout is exprimed in milliseconds. */
int getbyte_to (int fd, int to_ms);

View File

@@ -7,14 +7,20 @@
int main (int argc, char *argv[])
{
int serial_in;
int byte, foo;
int byte, foo, to;
serial_in = prepare_UART("/dev/ttyS0", 9600);
fprintf(stderr, "prepare uart -> %d\n", serial_in);
for (foo=0; foo<20; foo++) {
byte = getbyte(serial_in);
printf("%6d %02x\n", foo, byte);
to = (foo+1) * 666;
byte = getbyte_to(serial_in, to);
if (byte < 0) {
fprintf(stderr, "get byte : err is %d\n", byte);
}
else {
printf("%6d %6d %02x\n", foo, to, byte);
}
}
return 0;