DD2-monitor/serial/t.c

61 lines
1.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <errno.h>
#include "serial.h"
int verbosity;
/* ----------------------------------------------------- */
int openserial(char *dev)
{
int uart;
fprintf(stderr, "%s ( %s )\n", __func__, dev);
uart = open(dev, O_RDONLY | O_NOCTTY);
if (uart < 0)
{
perror("unable to open uart");
return -1;
}
return uart;
}
/* ----------------------------------------------------- */
int main (int argc, char *argv[])
{
int serial_in, foo;
unsigned char byte;
if (2 != argc) {
fprintf(stderr, "device name ?\n");
return 2;
}
serial_in = openserial(argv[1]);
fprintf(stderr, "prepare uart -> %d\n", serial_in);
if (serial_in < 0) {
exit(1);
}
for (;;) {
foo = read(serial_in, &byte, 1);
if (1 != foo) {
fprintf(stderr, "get byte : err is %d\n", errno);
}
else {
printf("%9ld %02x/%d\n", time(NULL), byte, byte);
}
}
return 0;
}
/* ----------------------------------------------------- */