LowPowerLab Forum

Hardware support => Moteino => Topic started by: K1JOS on August 15, 2014, 12:28:44 AM

Title: MoteinoMega Problem Not Working with SoftwareSerial
Post by: K1JOS on August 15, 2014, 12:28:44 AM
I just my MoteinoMegas.  I am trying to read RS485 serial data that I am converting using a MAX483 to my Mega.  The test code (only for serial testing, no RFM enabled) works fine with a plain Arduino but I get nothing with either of my MoteinoMega's.

The RS485 serial data is a fixed 5 byte packet and I can see it and decode it easily on my scope on both the RS485 A-B differential side as well as on the MAX483 output pin that is going to the Mega.  The levels are about +4vdc.  The baud is 9600, and serial is 8N1.  My Mega doesn't see any serial available even though I clearly see the signal on the Mega Digital pin 13 coming from the MAX483.  I confirmed the baud rate (9600) with my scope and it works fine with Arduino:  I have the board selection correct and libraries now all installed correctly.  Am I using digital pins on the Mega dedicated for other tasks??


#include <SoftwareSerial.h>
#define SERIAL_BAUD 9600 // for PC communications using Serial 0 port
#define SERIAL1_BAUD  9600 // for Arduino

#define SSerialRX        12  // on Arduino 12 - Receive pin  from MAX483 RO pin 1
#define SSerialTX        13  // on Arduino 7  - Transmit pin to MAX483 DI pin 4
#define SSerialTxControl 14  // on Arduino 8  - RS485 Direction control on both MAX483 RE and DE. Flips each to reciprocally enable/disable 
#define RS485Transmit    HIGH
#define RS485Receive     LOW

SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

byte buffer[5];
int cnt =0;

void setup() {
  Serial.begin(SERIAL_BAUD);
  pinMode(SSerialTxControl, OUTPUT);   
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver   
  RS485Serial.begin(9600);   // set the data rate
}

void loop() {
  //should find 5 bytes to read
  while (RS485Serial.available())  //Look for data from other device
  {
    Serial.println("RS485 Data in Buffer");
    if (cnt < 5){
      buffer[cnt] = RS485Serial.read();    // Read byte into array from cnt = 0-4
      Serial.println(cnt);
      Serial.println(buffer[cnt],HEX);
      cnt++;
      Serial.println(cnt);
    }
    if (cnt == 5){  // all bytes read
      Serial.println("Completed");
      Serial.println(buffer[5]);        // Show on Serial Monitor
    }
  }
  cnt =0;
  delay(100);
}


Title: Re: MoteinoMega Problem Not Working with SoftwareSerial
Post by: Felix on August 15, 2014, 07:07:02 AM
How come you're not using the hardware serial since there's an extra one on pins 10&11?
Also I haven't tested software serial on MEGA, not sure if that supports every chip out there.
Title: Re: MoteinoMega Problem Not Working with SoftwareSerial
Post by: K1JOS on August 15, 2014, 08:30:56 AM
I read on the Arduino forums that flow control like XON/XOFF was not supported by the Arduino's and their serial buffer is very small.  I have no control over the format used by the external commercial device (PIC based) I am trying to communicate with.  It uses RS485 (decodes like RS232) differential on 2 wires in half duplex.  Control is all in software by having a fixed header byte and EOT byte at the end. the data is in the middle three bytes.  To gain access to it I need to put a RS485 chip like the MAX483 .. to control flow, the MAX483 needs a third wire from the Arduino to turn on RX and turn off TX.  It has two enable/disable pins in opposite states, so only one wire needed from the Mega to flip from RX to TX.  I wasnt sure how to mix this third wire in software with the internal Mega hardware serial.  I am new at this so any advise greatly appreciated.  I dont have a regular Mega but could get one to see how that works.  Could it be my digital pin selection that is at issue?

Title: Re: MoteinoMega Problem Not Working with SoftwareSerial
Post by: Felix on August 16, 2014, 06:59:16 PM
It's possible but I doubt it. If it works on a atmega328p it should work on an atmega1284p so I would try software serial on a mega328 first and see how that works out since it's such a popular chip. The difference might be in the softwareserial library. There has never been a atmega1284p based official arduino to my knowledge, and only recently optiboot has added support for it. Hence I don't think there is extensive support for that particular chip, even though a majority of libraries might work on it by default or with small tweaks.
Title: Re: MoteinoMega Problem Not Working with SoftwareSerial
Post by: oric_dan on August 24, 2014, 02:36:02 AM
A little late on this reply, but from my experience of using Arduinos for about 4-years now, you can forget about using SoftSerial for any serious communications. And *especially* at such a slow baudrate as 9600.

Unless someone has actually come along and fixed it "at long last", it shuts off interrupt-processing for the entire time it's reading the soft-serial RX pin, which for 9600 bps means about 10 * 1/9600 = 1.04 MILLISECONDS per character received. Duh. This is not the way to write interrupt service routines.