Reading RS-232 data from M800 using an Arduino Uno

Discussion and support for MoTeC's previous generation ECUs.

Reading RS-232 data from M800 using an Arduino Uno

Postby jimbojohn on Thu Sep 12, 2013 6:25 pm

Hi everyone!!

I have been trying to read the data coming out of the RX pin of the M800 ECU using the pin 10 of the Arduino Uno ( which I have previously set as a virtual RX pin). The Arduino module is connected to my laptop. The ECU is connected to another laptop via a CAN to USB converter and is sending dummy sensor data to the Arduino. I have looked at the data sending protocol of the ECU, but have not been successful yet to extract anything but nonsense. Here's my code:Please before going through the code have a look at the data sending protocol of the ECU which I have attached.


/*
Software serial multple serial test

Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.

The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)

Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart's example

This example code is in the public domain.

*/
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

#define head0 0x82
#define head1 0x81
#define head2 0x80
#define marker1 0xFC
#define marker2 0xFB
#define marker3 0xFA
int rpm[2];
int RPM = 0;
char ET[2];
float EngTemp;


void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}


Serial.println("Goodnight moon!"); // test

// set the data rate for the SoftwareSerial port
mySerial.begin(19200);
//mySerial.println("Hello, world?");
}

void loop() // run over and over
{
if (mySerial.read() == head0){
Serial.println("RPM");
if (mySerial.read() == marker2){
if (mySerial.read() == marker3){


//skip next byte
// mySerial.read();
//RPM read next 2 bytes
//rpm[0] = mySerial.read();
//rpm[1] = mySerial.read();
//RPM = rpm[0] << 8 && rpm[1];
Serial.println("RPM");




}
}
}
if (mySerial.available())
Serial.println(mySerial.read());
}//}}}}
Attachments
PSAU0015 MoTeC M800 Set 3 Data Protocol.zip
(941.39 KiB) Downloaded 1004 times
jimbojohn
 
Posts: 4
Joined: Wed Sep 11, 2013 3:55 am

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby David Ferguson on Fri Sep 13, 2013 5:13 am

I would expect an M800 to TRANSMIT it's data on it's TX pin, and RECEIVE data on the RX pin.
David Ferguson
Veracity Racing Data
David Ferguson
Pro User
 
Posts: 1362
Joined: Fri Nov 07, 2008 2:45 am
Location: Paso Robles, California

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby jimbojohn on Mon Sep 16, 2013 3:25 am

Good call. Sorry that was my mistake, I apologize for my clumsiness. I have actually connected the TX of the ECU to RX of the Arduino. You see, I am quite sure that I receive stuff from ECU, but cant receive them in the right way. So, all I see is nonsense, but I am sure it is something which is being received from ECU. I am almost certain of it. The problem is that I dont know how to check what data is being sent and when they are sent.
jimbojohn
 
Posts: 4
Joined: Wed Sep 11, 2013 3:55 am

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby David Ferguson on Mon Sep 16, 2013 4:59 am

Why don't you start by printing to your console all the bytes you receive (in hex). Use something like:
uchar dataByte, buf[16];

dataByte = mySerial.read();
sprintf(buf,"0x%x ", dataByte); // for debugging only
Serial.println(buf); // do this for debugging only

Then you can look at what comes in and decide what to do with it.

switch (dataByte == head0){
head0:
Serial.printIn(" : head0 found\n"); // for debugging only
break;
head1:
Serial.printIn(" : head1 found\n"); // for debugging only
break;

// add more decoding here...

default:
Serial.printIn("\n"); // it's just a value so end the line (for debugging only)...
}

I hope that helps....
David Ferguson
Pro User
 
Posts: 1362
Joined: Fri Nov 07, 2008 2:45 am
Location: Paso Robles, California

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby jimbojohn on Tue Sep 17, 2013 10:54 pm

Hi there!! me again.

I actually made some slight changes to your code(cause I had to due to some errors) tested it. Your code actually makes a lot of sense to me and when I looked at it I thought "this should work". So I really appreciate your effort.

For some reason the result I got out of "serial.println(buf)" was independent from "dataByte" and made no sense. However it occurred to me that it changes when I change the size of "buf" (which is 16). So when it was "buf[16}" it spit out "0x8d4" on Arduino's serial monitor and when I changed "16" to "15" it changed to some other value. I believe it has something to to with the way sprintf() works, but I couldnt figure what it is. My aim was to only try the first half of the code so I can see the results that are thrown out of the ECU.
I also tried using "dataByte" as an "unsigned char" without 16 elements, but each time "buf" throw out "0xff". Just thought to mention that.
Here's the modified code that I used:


#include "stdlib.h"
//#include "stdafx.h" // Apparently the source of this couldnt be found
#include "Arduino.h"
#include <SoftwareSerial.h>

char head0 = 0xFC;
char head1 = 0xFB;
char head2 = 0xFA;
char rpm[2];
int RPM = 0;
char ET[2];
float EngTemp;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup(){
Serial.begin(19200);
mySerial.begin(19200);
unsigned char dataByte[16];
char buf[16]; //I had to make it char cause sprint() would give me error in unsigned char* to char* conversion, but apparently this does not influence the sign of dataByte


for (int i =0; i<17;i++){

dataByte[i]=mySerial.read();
}
//dataByte = Serial.read();
sprintf(buf,"0x%x ", dataByte); // for debugging only
Serial.print(buf); // This seems to be absolutely independent of whats in dataByte (there maybe something wrong with sprintf()), but I get 0x8d4 when buf has 16 members and this value changes as I change 16
}


/* for( int j = 0; j<17; j++){
switch (dataByte[j] == head0){
head0:
Serial.println(" : head0 found\n"); // for debugging only
break;
head1:
Serial.println(" : head1 found\n"); // for debugging only
break;

// add more decoding here...

default:
Serial.println("\n"); // it's just a value so end the line (for debugging only)...
}
*/

}
void loop(){
/*if (mySerial.available()){
Serial.println(mySerial.read());}*/
}
jimbojohn
 
Posts: 4
Joined: Wed Sep 11, 2013 3:55 am

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby David Ferguson on Wed Sep 18, 2013 3:01 am

Sorry but I'm not going to be able to teach you how to program in C in this forum.

Because you declared dataByte to be a an array, when you pass it to sprintf, you are passing a pointer to the array. The reason you get the same value is because the array is at a constant address.

Try this:

for (int i=0; i<16;i++){ // note, array is 16 elements long, so the index goes from 0 to 15 not 0 to 16!

dataByte[i]=mySerial.read();
}

// to prove the following code you might want to initialize the dataByte array to known values like this:
// for (int i=0; i<16;i++){
// dataByte[i]=i;
// }

for (int i=0; i< (sizeof(dataByte) / sizeof (dataByte[0]); i++){ // notice how I made the loop count automatically track the size of the dataByte array?
sprintf(buf,"0x%x ", dataByte[i]); // for debugging only
Serial.print(buf);
}

Please practice on your own, or use internet resources like stackoverflow.com to get help in programming topics.
David Ferguson
Veracity Racing Data
David Ferguson
Pro User
 
Posts: 1362
Joined: Fri Nov 07, 2008 2:45 am
Location: Paso Robles, California

Re: Reading RS-232 data from M800 using an Arduino Uno

Postby jimbojohn on Thu Sep 19, 2013 2:17 pm

Thank you very much for your help. Sorry if I violated the rules a little bit for asking a lot of programing related questions. This forum has helped me a lot with my progress than other forums have.

Cheers :D
jimbojohn
 
Posts: 4
Joined: Wed Sep 11, 2013 3:55 am


Return to M400, M600, M800 and M880 ECUs

Who is online

Users browsing this forum: No registered users and 22 guests