works also in line mode so far
This commit is contained in:
56
src/mbusgw.c
56
src/mbusgw.c
@ -380,6 +380,7 @@ void printFrame(bool hexOut, t_longframe *frame) {
|
|||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "%02x %02x\n", frame->chksum, frame->stop);
|
fprintf(stderr, "%02x %02x\n", frame->chksum, frame->stop);
|
||||||
} else {
|
} else {
|
||||||
|
fprintf(stdout, "%c%c", 0, frame->length1 + 6);
|
||||||
fprintf(stdout, "%c%c%c%c%c%c%c",
|
fprintf(stdout, "%c%c%c%c%c%c%c",
|
||||||
frame->start1, frame->length1, frame->length2, frame->start2,
|
frame->start1, frame->length1, frame->length2, frame->start2,
|
||||||
frame->c, frame->a, frame->ci);
|
frame->c, frame->a, frame->ci);
|
||||||
@ -387,6 +388,7 @@ void printFrame(bool hexOut, t_longframe *frame) {
|
|||||||
fprintf(stdout, "%c", frame->userdata[i]);
|
fprintf(stdout, "%c", frame->userdata[i]);
|
||||||
}
|
}
|
||||||
fprintf(stdout, "%c%c", frame->chksum, frame->stop);
|
fprintf(stdout, "%c%c", frame->chksum, frame->stop);
|
||||||
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,11 +397,12 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
|
|
||||||
bool hexOut = false;
|
bool hexOut = false;
|
||||||
|
bool lineMode = false;
|
||||||
uint8_t addr = 0;
|
uint8_t addr = 0;
|
||||||
uint8_t cmd = 0x5b;
|
uint8_t cmd = 0x5b;
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "hvxc:a:")) != -1) {
|
while ((opt = getopt(argc, argv, "lhvxc:a:")) != -1) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
case 'h':
|
case 'h':
|
||||||
fprintf(stderr, "mbusgw - interface access tool for meterbus gateway\n");
|
fprintf(stderr, "mbusgw - interface access tool for meterbus gateway\n");
|
||||||
@ -408,11 +411,21 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "-x ... Output as hex string in 'human-readable' form\n");
|
fprintf(stderr, "-x ... Output as hex string in 'human-readable' form\n");
|
||||||
fprintf(stderr, "-a addr ... Address of device to be queried\n");
|
fprintf(stderr, "-a addr ... Address of device to be queried\n");
|
||||||
fprintf(stderr, "-c cmd ... Command to be sent, default is 0x5b\n");
|
fprintf(stderr, "-c cmd ... Command to be sent, default is 0x5b\n");
|
||||||
|
fprintf(stderr, "-l ... Read cmd and addr from stdin unless\n");
|
||||||
|
fprintf(stderr, " 0x00 0x00 is provided and return the\n");
|
||||||
|
fprintf(stderr, " result on stdout.\n");
|
||||||
|
fprintf(stderr, " It is preceeds by 0x00 for okay and the\n");
|
||||||
|
fprintf(stderr, " length.\n");
|
||||||
|
fprintf(stderr, " In case of an error it will by 0xff followed\n");
|
||||||
|
fprintf(stderr, " by one octet with an error code\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose = true;
|
verbose = true;
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
lineMode = true;
|
||||||
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
hexOut = true;
|
hexOut = true;
|
||||||
break;
|
break;
|
||||||
@ -425,15 +438,17 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "hexOut %d, addr %02x, cmd %02x\n", hexOut, addr, cmd);
|
|
||||||
|
|
||||||
|
if (verbose) {
|
||||||
fprintf(stderr, "Opening device\n");
|
fprintf(stderr, "Opening device\n");
|
||||||
|
}
|
||||||
int fd = openSerial(DEFAULT_SERIAL_DEVICE, 2400);
|
int fd = openSerial(DEFAULT_SERIAL_DEVICE, 2400);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
myExit(-1);
|
myExit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Sending request\n");
|
if (! lineMode) {
|
||||||
|
fprintf(stderr, "Sending request %02x %02x\n", cmd, addr);
|
||||||
t_longframe *frame = request(fd, cmd, addr);
|
t_longframe *frame = request(fd, cmd, addr);
|
||||||
if (frame) {
|
if (frame) {
|
||||||
fprintf(stderr, "received something\n");
|
fprintf(stderr, "received something\n");
|
||||||
@ -444,10 +459,45 @@ int main(int argc, char *argv[]) {
|
|||||||
frame = NULL;
|
frame = NULL;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "some error occured\n");
|
fprintf(stderr, "some error occured\n");
|
||||||
|
if (! hexOut) {
|
||||||
|
fprintf(stdout, "%c%c", 0xff, 0);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
myExit(-1);
|
myExit(-1);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
while (1) {
|
||||||
|
fread(&cmd, 1, 1, stdin);
|
||||||
|
fread(&addr, 1, 1, stdin);
|
||||||
|
if ((cmd == 0) && (addr == 0)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (verbose) {
|
||||||
|
fprintf(stderr, "%02x %02x\n", cmd, addr);
|
||||||
|
}
|
||||||
|
t_longframe *frame = request(fd, cmd, addr);
|
||||||
|
if (frame) {
|
||||||
|
if (verbose) {
|
||||||
|
fprintf(stderr, "received something\n");
|
||||||
|
}
|
||||||
|
printFrame(false, frame);
|
||||||
|
free(frame->userdata);
|
||||||
|
frame->userdata = NULL;
|
||||||
|
free(frame);
|
||||||
|
frame = NULL;
|
||||||
|
} else {
|
||||||
|
if (verbose) {
|
||||||
|
fprintf(stderr, "some error occured\n");
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%c%c", 0xff, 0);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose) {
|
||||||
fprintf(stderr, "Closing device\n");
|
fprintf(stderr, "Closing device\n");
|
||||||
|
}
|
||||||
closeSerial(fd);
|
closeSerial(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user