Hello everyone,
I am trying to understand an issue I am seeing between my Arduino Mega 2560 and my Teensy 3.2. When I run the code below on the Arduino, it functions how I would expect it to while communicating with my VP Flowscope via modbus. However, when I implement the same code on my Teensy 3.2 I do not get anything in the serial monitor. If I unplug and re-plug in the 5V wire to the MAX485 chip I will get intermittent responses in the serial monitor though with correct data.
I am thinking this has to be a timing thing but I am new enough to these devices that I am not 100% sure the correct path to start diagnosing this. I would really appreciate some pointers.
Code:
#include <ModbusMaster.h>
#define MAX485_DE 3 //Enable transmit pin for MAX485 (both RE/DE tied together)
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_DE, OUTPUT);
digitalWrite(MAX485_DE, 0);
// Baudrate for serial monitor if you want to see responses
while (!Serial);
Serial.begin(38400);
//Baudrate of modbus device is 38400
while (!Serial1);
Serial1.begin(38400);
// Modbus slave ID is 9 and MAX485 is connected to RX1/TX1
node.begin(9, Serial1);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t result;
result = node.readHoldingRegisters(0x40, 1);
if (result == node.ku8MBSuccess)
{
Serial.print("Temperature in C = ");
Serial.println(node.getResponseBuffer(0x0)/10);
}
result = node.readHoldingRegisters(0x41, 1);
if (result == node.ku8MBSuccess)
{
Serial.print("Temperature in F = ");
Serial.println(node.getResponseBuffer(0x0)/10);
}
result = node.readHoldingRegisters(0x21, 1);
if (result == node.ku8MBSuccess)
{
Serial.print("Pressure in PSI = ");
Serial.println(node.getResponseBuffer(0x0)/10);
}
}