Newserial protocol -- r command

This is a forum for discussing the development and testing of alpha MS2/Extra code. Documentation
(Runs on MS2 and Microsquirt)

Moderators: jsmcortina, muythaibxr

Post Reply
robs
Master MS/Extra'er
Posts: 564
Joined: Sun Jan 17, 2010 4:26 pm
Location: Sydney, Australia

Newserial protocol -- r command

Post by robs »

I upgraded one of my cars to the latest release and it's running well now.

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);
}
(experimented with the pacing in the usleep() call, but MS2 seemed just as happy with full speed comms -- at least in this example)

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 
Worst come to worst I can use a06, but I'd rather not send a bunch or unwanted stuff through the serial port.

I'm probably missing something blindingly obvious, but would be grateful for a hint.

Have fun,

Rob.
jsmcortina
Site Admin
Posts: 39585
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: Newserial protocol -- r command

Post by jsmcortina »

That sounds very similar to an issue I ran into a week or so ago. Known working test code suddenly didn't work - after a Linux distro upgrade.
In my case I was testing MS3 and switched to the built-in FTDI USB-serial instead of the true serial connector on my PC. Suddenly it worked without a timeout.

I didn't have time to investigate further, but my initial conclusion was kernel driver breakage.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
racingmini_mtl
Super MS/Extra'er
Posts: 9128
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: Newserial protocol -- r command

Post by racingmini_mtl »

You're not sending the correct message length in the message header. The length you send is 13 (00 0d) but the message is 7 bytes long. The header and CRC32 are not included in the length.

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
robs
Master MS/Extra'er
Posts: 564
Joined: Sun Jan 17, 2010 4:26 pm
Location: Sydney, Australia

Re: Newserial protocol -- r command

Post by robs »

You're right Jean. Changing the size to 7 gets me the expected result.

The version of the Serial Protocol doc (2014-10-28) said:

Size: This is the big-endian 16bit size of the packet including the two size bytes and four CRC32 bytes.

But I did notice that the returned packets were Size=1 (the 0x80 underrun status), so maybe I should have guessed that the above should have said "excluding".

Anyhow, thanks guys. Easy enough to get the dash working now.

Have fun,

Rob.
Post Reply