I'm having some bother getting my mini-dashboard to talk to it and it didn't take long to find out about the newserial protocol that has been migrated from MS3. The dash firmware uses the 'r' command so that it only downloads the stuff it needs. This command doesn't seem to work in "compatability mode". Nor if I wrap it in the new length+cmd+CRC package.
I threw together the following Linux program which
- uses Q to successfully match the protocol version
- uses r and fails to get the subset data (return status says underrun)
- uses A and successfully gets the 212 byte result (a06 works for its 112 byte result too)
Code: Select all
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
/*
* Request a variable sample from MS2
*/
#define SIGNATURE "MS2Extra comms342a2"
/*
* Open serial port in suitable mode for polling.
*/
static int
open_serial(const char *dev)
{
int fd;
struct termios tio;
if ((fd = open(dev, O_RDWR)) < 0)
{
return(-1);
}
if (tcgetattr(fd, &tio) < 0)
{
close(fd);
return(-1);
}
cfsetspeed(&tio, B115200);
tio.c_iflag |= IGNBRK;
tio.c_iflag &= ~(ISTRIP | ICRNL | IXON | IXOFF);
tio.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONOCR|ONLRET);
tio.c_cflag |= (CLOCAL | CREAD | CS8);
tio.c_cflag &= ~(CRTSCTS | CSTOPB | PARENB);
tio.c_cc[VMIN] = 0; /* simple timeout */
tio.c_cc[VTIME] = 3; /* three tenths of a second */
if (tcsetattr(fd, 0, &tio) < 0)
{
close(fd);
return(-1);
}
return(fd);
}
/*
* Paced write to the serial port.
*/
static void
do_send(int fd, const unsigned char *buf, int len)
{
int i;
unsigned char c;
printf("do_send: ");
for (i = 0; i < len; i++)
{
c = buf[i];
write(fd, &c, 1);
printf("%02x ", c);
fflush(stdout);
#if 0
usleep(200);
#endif
}
printf("\n");
}
static int
do_read(int fd, char *buf, int len)
{
int n;
int tot;
tot = 0;
for (;;)
{
if ((n = read(fd, buf, len)) == 0)
return(tot);
if (n < 0)
{
perror("do_read");
continue;
}
tot += n;
len -= n;
buf += n;
}
}
static void
do_report(const char *buf, int len)
{
int i;
printf("Received %d bytes\n", len);
for (i = 0; i < len; i++)
{
printf("0x%02x ", (unsigned char)buf[i]);
}
printf("\n");
}
int
main(int argc, char **argv)
{
int fd;
unsigned char buf[256];
unsigned char abuf[16];
int n;
int retval;
retval = 1;
if ((fd = open_serial("/dev/ttyS0")) < 0)
{
perror("/dev/ttyS0");
return(1);
}
if (write(fd, "Q", 1) < 0)
{
perror("write");
goto CLEANUP1;
}
if ((n = do_read(fd, buf, sizeof(buf))) <= 0)
{
fprintf(stderr, "no signature\n");
goto CLEANUP1;
}
if (strcmp(buf, SIGNATURE))
{
fprintf(stderr, "signature mismatch\n");
goto CLEANUP1;
}
printf("signature match\n");
#if 1
sprintf(abuf, "%c%cr%c%c%c%c%c%c%c%c%c%c", 0, 13, 0, 7, 0, 0, 0, 24,
0x47, 0xd7, 0x5a, 0x3a); /* CRC */
do_send(fd, abuf, 13);
#else
do_send(fd, "a\0\06", 3);
#endif
n = do_read(fd, buf, sizeof(buf));
do_report(buf, n);
sleep(2);
do_send(fd, "A", 1);
n = do_read(fd, buf, sizeof(buf));
do_report(buf, n);
retval = 0;
CLEANUP1:
close(fd);
return(retval);
}
Here's the output of a run:
Code: Select all
signature match
do_send: 00 0d 72 00 07 00 00 00 18 47 d7 5a 3a
Received 7 bytes
0x00 0x01 0x80 0x3f 0xba 0x6c 0xad
do_send: 41
Received 212 bytes
0x09 0xc5 0x07 0x9e 0x07 0x9e 0x00 0x00 0x00 0x00 0x00 0x00 0x93 0x93 0x01 0x01 0x03 0xd4 0x03 0xd2 0x00 0xca 0x02 0x4a 0x01 0x05 0x00 0x67 0x00 0x5c 0x00 0x5c 0x00 0x00 0x03 0xe8 0x03 0xe8 0x03 0xe7 0x00 0x64 0x00 0x00 0x00 0x64 0x03 0xf2 0x00 0x64 0x00 0x64 0x00 0x64 0x00 0x00 0x00 0x00 0xff 0xfa 0xff 0xed 0x00 0x2d 0x00 0x00 0x03 0xd4 0x00 0x64 0x00 0x00 0x00 0x00 0x00 0x3c 0x00 0x3c 0x00 0x00 0x00 0x00 0x01 0x8c 0x00 0x00 0x01 0x65 0x00 0x00 0x03 0xd4 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x55 0x00 0x37 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x03 0xd4 0x03 0xd4 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0xad 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xd3 0x00 0x21 0x00 0x00 0x00
I'm probably missing something blindingly obvious, but would be grateful for a hint.
Have fun,
Rob.