-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActisenseListenerSender-ESP32.ino
63 lines (50 loc) · 2.28 KB
/
ActisenseListenerSender-ESP32.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Demo: NMEA2000 library. Bus listener and sender.
// Sends all bus data to serial in Actisense format.
// Send all data received from serial in Actisense format to the N2kBus.
// Use this e.g. with NMEA Simulator (see. http://www.kave.fi/Apps/index.html) to send simulated data to the bus.
// I have plans to add receiving functionality and data forwarding to NMEA Simulator. Meanwhile you can define
// other stream to different port so that you can send data with NMEA Simulator and listen it on other port with
// Actisense NMEA Reader.
#include <Arduino.h>
//#define N2k_CAN_INT_PIN 2
//#define USE_N2K_CAN 1
//#define N2k_SPI_CS_PIN 10
#define ESP32_CAN_TX_PIN GPIO_NUM_5
#define ESP32_CAN_RX_PIN GPIO_NUM_4
#include <NMEA2000_CAN.h>
#include <N2kMsg.h>
#include <NMEA2000.h>
#include <ActisenseReader.h>
tActisenseReader ActisenseReader;
// Define READ_STREAM to port, where you write data from PC e.g. with NMEA Simulator.
#define READ_STREAM Serial
// Define ForwardStream to port, what you listen on PC side. On Arduino Due you can use e.g. SerialUSB
#define FORWARD_STREAM Serial
Stream *ReadStream=&READ_STREAM;
Stream *ForwardStream=&FORWARD_STREAM;
void setup() {
// Define buffers big enough
NMEA2000.SetN2kCANSendFrameBufSize(150);
NMEA2000.SetN2kCANReceiveFrameBufSize(150);
if (ReadStream!=ForwardStream) READ_STREAM.begin(115200);
FORWARD_STREAM.begin(115200);
NMEA2000.SetForwardStream(ForwardStream);
NMEA2000.SetMode(tNMEA2000::N2km_ListenAndSend);
// NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show bus data in clear text
if (ReadStream==ForwardStream) NMEA2000.SetForwardOwnMessages(false); // If streams are same, do not echo own messages.
// NMEA2000.EnableForward(false);
NMEA2000.Open();
// I originally had problem to use same Serial stream for reading and sending.
// It worked for a while, but then stopped. Later it started to work.
ActisenseReader.SetReadStream(ReadStream);
ActisenseReader.SetDefaultSource(75);
ActisenseReader.SetMsgHandler(HandleStreamN2kMsg);
}
void HandleStreamN2kMsg(const tN2kMsg &N2kMsg) {
//N2kMsg.Print(&Serial);
NMEA2000.SendMsg(N2kMsg,-1);
}
void loop() {
NMEA2000.ParseMessages();
ActisenseReader.ParseMessages();
}