Arduino library which allow to communicate on J1850-VPW mode. It works with wire. Should work with radio and laser transmissions as well. You just have to choose tx and rx pins. It doesn't have either a header nor a parity check, just CRC check. All validations could be implemented at a higher protocol layer.
This library has been tested with wire connection between two Arduino and J1850 vpw bus of Chrysler Pacifica vehicle.
Use following schematics to connect Arduino to real J1850VPW bus of your vehicle:
Connections:
- IN -> pin of Arduino that is configured as RX pin for J1850VPW
- OUT -> pin of Arduino that is configured as TX pin for J1850VPW
- BUS -> J1850VPW bus (ex. vehicle)
#include "j1850vpw.h"
#define TX 9
#define RX 10
void handleError(J1850_Operations op, J1850_ERRORS err);
void setup()
{
Serial.begin(115200); // start serial port
J1850VPW::onError(handleError); // listen for errors
J1850VPW::init(RX, TX); // init transceiver
}
void loop()
{
static uint8_t buff[BS]; // buffer for read message
static uint8_t sendBuff[2] = {0x01, 0x02}; // message to send, CRC will be read and appended to frame on the fly
uint8_t dataSize; // size of message
// read all messages with valid CRC from cache
while (dataSize = J1850VPW::tryGetReceivedFrame(buff))
{
String s;
// convert to hex string
uint8_t *pData = buff;
for (int i = 0; i < dataSize; i++)
{
if (i > 0)
{
s += " ";
}
if (buff[i] < 0x10)
{
s += '0';
}
s += String(pData[i], HEX);
}
Serial.println(s);
}
// write simple message to bus
J1850VPW::send(sendBuff, sizeof(sendBuff));
delay(1000);
}
void handleError(J1850_Operations op, J1850_ERRORS err)
{
if (err == J1850_OK)
{
// skip non errors if any
return;
}
String s = op == J1850_Read ? "READ " : "WRITE ";
switch (err)
{
case J1850_ERR_BUS_IS_BUSY:
Serial.println(s + "J1850_ERR_BUS_IS_BUSY");
break;
case J1850_ERR_BUS_ERROR:
Serial.println(s + "J1850_ERR_BUS_ERROR");
break;
case J1850_ERR_RECV_NOT_CONFIGURATED:
Serial.println(s + "J1850_ERR_RECV_NOT_CONFIGURATED");
break;
case J1850_ERR_PULSE_TOO_SHORT:
Serial.println(s + "J1850_ERR_PULSE_TOO_SHORT");
break;
case J1850_ERR_PULSE_OUTSIDE_FRAME:
Serial.println(s + "J1850_ERR_PULSE_OUTSIDE_FRAME");
break;
case J1850_ERR_ARBITRATION_LOST:
Serial.println(s + "J1850_ERR_ARBITRATION_LOST");
break;
case J1850_ERR_PULSE_TOO_LONG:
Serial.println(s + "J1850_ERR_PULSE_TOO_LONG");
break;
default:
// unknown error
Serial.println(s + "ERR: " + String(err, HEX));
break;
}
}
- Some code from wiring sources of arduino to implement fast digital IO.