|
|
@@ -0,0 +1,115 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <termio.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/ioctl.h> |
|
|
|
#include <errno.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <getopt.h> |
|
|
|
#include <string.h> |
|
|
|
#include <syslog.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <err.h> |
|
|
|
#include <linux/serial.h> |
|
|
|
|
|
|
|
int serial_open(const char *device, int rate) |
|
|
|
{ |
|
|
|
struct termios options; |
|
|
|
struct serial_struct serinfo; |
|
|
|
int fd; |
|
|
|
|
|
|
|
/* Open and configure serial port */ |
|
|
|
if ((fd = open(device,O_RDWR|O_NOCTTY)) == -1) |
|
|
|
err(1, "%s", device); |
|
|
|
|
|
|
|
serinfo.reserved_char[0] = 0; |
|
|
|
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) |
|
|
|
err(1, "TIOCGSERIAL"); |
|
|
|
serinfo.flags &= ~ASYNC_SPD_MASK; |
|
|
|
serinfo.flags |= ASYNC_SPD_CUST; |
|
|
|
serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate; |
|
|
|
if (serinfo.custom_divisor < 1) |
|
|
|
serinfo.custom_divisor = 1; |
|
|
|
if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0) |
|
|
|
err(1, "TIOCSSERIAL"); |
|
|
|
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) |
|
|
|
err(1, "TIOCGSERIAL 2"); |
|
|
|
if (serinfo.custom_divisor * rate != serinfo.baud_base) { |
|
|
|
warnx("actual baudrate is %f", |
|
|
|
(float)serinfo.baud_base / serinfo.custom_divisor); |
|
|
|
} |
|
|
|
|
|
|
|
fcntl(fd, F_SETFL, 0); |
|
|
|
tcgetattr(fd, &options); |
|
|
|
cfsetispeed(&options, B38400); |
|
|
|
cfsetospeed(&options, B38400); |
|
|
|
cfmakeraw(&options); |
|
|
|
options.c_cflag |= (CLOCAL | CREAD); |
|
|
|
options.c_cflag &= ~CRTSCTS; |
|
|
|
if (tcsetattr(fd, TCSANOW, &options) != 0) |
|
|
|
err(1, "TCSANOW"); |
|
|
|
|
|
|
|
return fd; |
|
|
|
} |
|
|
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
|
{ |
|
|
|
char *device=strdup("/dev/ttyUSB3"); |
|
|
|
int rate=1000000; |
|
|
|
int fd; |
|
|
|
int getopt_index; |
|
|
|
int i; |
|
|
|
char buf[1024]; |
|
|
|
|
|
|
|
static struct option long_opts[] = { |
|
|
|
{ "device", required_argument, NULL, 'd' }, |
|
|
|
{ "rate", required_argument, NULL, 'r' }, |
|
|
|
{ "help", no_argument, NULL, 'h' }, |
|
|
|
{ 0, 0, 0, 0 } |
|
|
|
}; |
|
|
|
int help=0; |
|
|
|
char c; |
|
|
|
|
|
|
|
while((c=getopt_long(argc,argv,"d:r:h?", |
|
|
|
long_opts,&getopt_index))!=-1) { |
|
|
|
switch(c) |
|
|
|
{ |
|
|
|
case 'd': |
|
|
|
free(device); |
|
|
|
device = strdup(optarg); |
|
|
|
break; |
|
|
|
case 'r': |
|
|
|
rate = atoi(optarg); |
|
|
|
if(rate == 0) |
|
|
|
errx(1,"invalid rate: %s",optarg); |
|
|
|
break; |
|
|
|
case 'h': |
|
|
|
case '?': |
|
|
|
default: |
|
|
|
help = 1; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(help) { |
|
|
|
fprintf(stderr,"Zoom Nilm Client\n"); |
|
|
|
fprintf(stderr,"usage: %s [options]\n\n",*argv); |
|
|
|
fprintf(stderr," -d, --device /dev/ttyUSB3 serial port\n"); |
|
|
|
fprintf(stderr," -r, --rate 1000000 baud rate\n"); |
|
|
|
fprintf(stderr," -h, --help this cruft\n"); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
if ((fd = serial_open(device, rate)) == -1) |
|
|
|
errx(1, "serial_open failed"); |
|
|
|
|
|
|
|
for (i = 0; i < sizeof(buf); i++) |
|
|
|
buf[i] = 'U'; |
|
|
|
while (1) { |
|
|
|
write(fd, buf, sizeof(buf)); |
|
|
|
} |
|
|
|
|
|
|
|
/////////////////////// |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|