DD2-monitor/serial/t.c

59 lines
1.1 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");
exit(1);
}
return uart;
}
/* ----------------------------------------------------- */
int main (int argc, char *argv[])
{
int serial_in, foo;
unsigned char byte;
if (2 != argc) {
fprintf(stderr, "give me a device name, please.\n");
return 2;
}
serial_in = openserial(argv[1]);
fprintf(stderr, "openserial -> %d\n", serial_in);
for (;;) {
foo = read(serial_in, &byte, 1);
if (1 != foo) {
fprintf(stderr, "get byte : got %d, err is %d\n", foo, errno);
}
else {
printf("%9ld $%02x ", time(NULL), byte);
if (isprint(byte)) putchar(byte);
puts("");
}
}
return 0;
}
/* ----------------------------------------------------- */