Fixed communication

This commit is contained in:
Mark van Renswoude 2018-11-22 20:30:31 +01:00
parent 06b5a0e720
commit ff315ae20e
2 changed files with 24 additions and 5 deletions

View File

@ -9,6 +9,15 @@
#include <stdint.h> #include <stdint.h>
// 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 // How long the display should stay on once it's idle and showing the
// current step numbers. // current step numbers.
const uint32_t DisplayIdleTimeout = 5000; const uint32_t DisplayIdleTimeout = 5000;

View File

@ -7,6 +7,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include "global.h" #include "global.h"
#include "config.h"
#include "display.h" #include "display.h"
#include "protocol.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 // 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 // Source: http://www.robotroom.com/Asynchronous-Serial-Communication-2.html
Serial.begin(76800); Serial.begin(CommBaudRate);
pinMode(CommWriteEnablePin, OUTPUT);
comm.begin(); 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() void handleCommMessage()
{ {
uint8_t* data = comm.getData(); uint8_t* data = comm.getData();
@ -93,7 +103,7 @@ void handleCommMessage()
{ {
// Sender expects a response from us // Sender expects a response from us
const uint8_t msg[] = { ResponseUhmWhat, moduleIndex }; 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) void handlePing(uint8_t* data, uint8_t length)
{ {
const uint8_t msg[] = { ResponsePing, settings.getModuleIndex() }; 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() }; 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; uint8_t sensor2 = 0;
const uint8_t msg[] = { ResponseSetPWM, settings.getModuleIndex(), sensor1, sensor2 }; const uint8_t msg[] = { ResponseSetPWM, settings.getModuleIndex(), sensor1, sensor2 };
comm.sendMsg(msg, sizeof(msg)); sendCommMessage(msg, sizeof(msg));
} }