From ff315ae20e337cd7cf2d25635bb9bb2e3db34e53 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Thu, 22 Nov 2018 20:30:31 +0100 Subject: [PATCH] Fixed communication --- module/src/config.h | 9 +++++++++ module/src/main.cpp | 20 +++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/module/src/config.h b/module/src/config.h index f78eaaf..4d4e1ca 100644 --- a/module/src/config.h +++ b/module/src/config.h @@ -9,6 +9,15 @@ #include + +// Baud rate for the RS485 communication +// At 16 Mhz we pick a baud rate with a very acceptable 0.2% error rate +// Source: http://www.robotroom.com/Asynchronous-Serial-Communication-2.html +const uint32_t CommBaudRate = 76800; + +// Pin connected to the MAX485's Receiver and Driver Output Enable pins +const uint8_t CommWriteEnablePin = 2; + // How long the display should stay on once it's idle and showing the // current step numbers. const uint32_t DisplayIdleTimeout = 5000; diff --git a/module/src/main.cpp b/module/src/main.cpp index 663c736..2245662 100644 --- a/module/src/main.cpp +++ b/module/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include "global.h" +#include "config.h" #include "display.h" #include "protocol.h" @@ -22,7 +23,8 @@ void setup() // At 16 Mhz we pick a baud rate with a very acceptable 0.2% error rate // Source: http://www.robotroom.com/Asynchronous-Serial-Communication-2.html - Serial.begin(76800); + Serial.begin(CommBaudRate); + pinMode(CommWriteEnablePin, OUTPUT); comm.begin(); } @@ -51,6 +53,14 @@ void loop() +void sendCommMessage(const uint8_t* data, uint8_t size) +{ + digitalWrite(CommWriteEnablePin, HIGH); + comm.sendMsg(data, size); + digitalWrite(CommWriteEnablePin, LOW); +} + + void handleCommMessage() { uint8_t* data = comm.getData(); @@ -93,7 +103,7 @@ void handleCommMessage() { // Sender expects a response from us const uint8_t msg[] = { ResponseUhmWhat, moduleIndex }; - comm.sendMsg(msg, sizeof(msg)); + sendCommMessage(msg, sizeof(msg)); } } } @@ -102,7 +112,7 @@ void handleCommMessage() void handlePing(uint8_t* data, uint8_t length) { const uint8_t msg[] = { ResponsePing, settings.getModuleIndex() }; - comm.sendMsg(msg, sizeof(msg)); + sendCommMessage(msg, sizeof(msg)); } @@ -163,7 +173,7 @@ void handleSetPWM(uint8_t* data, uint8_t length) } const uint8_t msg[] = { ResponseSetPWM, settings.getModuleIndex() }; - comm.sendMsg(msg, sizeof(msg)); + sendCommMessage(msg, sizeof(msg)); } @@ -175,5 +185,5 @@ void handleGetSensors(uint8_t* data, uint8_t length) uint8_t sensor2 = 0; const uint8_t msg[] = { ResponseSetPWM, settings.getModuleIndex(), sensor1, sensor2 }; - comm.sendMsg(msg, sizeof(msg)); + sendCommMessage(msg, sizeof(msg)); }