jgatvl:
Hi All,
I am using a Mega, with a RS485 module, and trying to communicate with a digital flowmeter. I can see that messages are being transmitted, but I don't see anything coming back. Any ideas what might be going on here?Mega Module Flowmeter
5V VCC
GND GND
19 DI
18 RO
3 DE&RE
A 485+
B 485-/
/-----( Import needed libraries )-----/
#include <SoftwareSerial.h>
/-----( Declare Constants and Pin Numbers )-----/
#define SSerialRX 19 //Serial Receive pin
#define SSerialTX 18 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/-----( Declare objects )-----/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/-----( Declare Variables )-----/
int byteReceived;
int byteSend;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(19200);
Serial.println("YourDuino.com SoftwareSerial remote loop example");
Serial.println("Use Serial Monitor, type in upper window, ENTER");
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(9600); // set the data rate
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if (Serial.available())
{
byteReceived = Serial.read();
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(byteReceived); // Send byte to Remote Arduino
digitalWrite(Pin13LED, HIGH); // Show activity
delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
}
if (RS485Serial.available()) //Look for data from other Arduino
{
digitalWrite(Pin13LED, LOW); // Show activity
byteReceived = RS485Serial.read(); // Read received byte
Serial.write(byteReceived); // Show on Serial Monitor
delay(10);
}
}//--(end main loop )---
It looks like you have TX and RX reversed,
Arduino Pin 18 it TX1, which should be connected to DI.
Arduino Pin 19 is RX1, which should be connected to RO.
Also, because your are switching from RX to TX modes, the RO signal goes from Active to TriState, and since the MAX485 it is not driving RO while it is Tri-Stated, The signal drifts to LOW, which cause the Arduino's UART(SERIAL1) to think it is seeing a new block of bits, It starts building a byte, Usually 0xFF. To inhibit this erroneous action, put a weak(10k-50k) pullup resistor between VCC and RX1(pin 18).
Why are you using SoftwareSerial.h, Just use Serial1
/*-----( Import needed libraries )-----*/
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(19200);
Serial.println("YourDuino.com SoftwareSerial remote loop example");
Serial.println("Use Serial Monitor, type in upper window, ENTER");
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
Serial1.begin(9600);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if (Serial.available())
{
byteReceived = Serial.read();
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
Serial1.write(byteReceived); // Send byte to Remote Arduino
digitalWrite(Pin13LED, HIGH); // Show activity
Serial1.flush(); //delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
}
if (Serial1.available()) //Look for data from other Arduino
{
digitalWrite(Pin13LED, LOW); // Show activity
byteReceived = Serial1.read(); // Read received byte
Serial.write(byteReceived); // Show on Serial Monitor
Serial.flush();// delay(10);
}
}//--(end main loop )---
Chuck.