RS485 Missing flags and Byte size does not match

Hi

I am trying to use MAX485 to communicate with pic with baud rate of 115200 (can not be changed), sometime I am missing flags or data is short. I do not know the problem is with my code or with MAX485 with Arduino Mega.

Sample of result from serial Monitor
Note: lines that end with “–” That mean process data and rest is raw data

7E510BF65C7E //Raw data
 7E 5 10 BF 6 5C 7E--  //Process data
510BF65C7E7E // as you can see we are missing the 7E flag to start 

 7E 7E-- //Process data is wrong it should be 7E 5 10 BF 6 5C 7E
510BF65C7E510BF65C7E
 7E 5 10 BF 6 5C 7E--
7E510BF65CBF
 7E 5 10 BF 6 5C--
7E25FFAF164B17E510BF67E510BF65C7E7E   // Raw data is shorter than 25=>37 
 7E 25 FF AF 16 4 B1 7E 5 10 BF 6 7E 5 10 BF 6 5C 7E-- //  Process data I fixed
1
510BF65C7E
 7E 5 10 BF 6 5C 7E--
510BF65C7E7E // Raw data is missing flag

 7E 7E--
510BF65C7E510BF65C10
 7E 5 10 BF 6 5C--
7E510BF65C7E
 7E 5 10 BF 6 5C 7E--
510BF65C7E7Edouble 7E with msg 1

My code (code is large so I just cut the process part) :

/*
 * Communication.cpp
 *
 *  Created on: Jan 8, 2017
 *      Author: Amir
 */

#include "Communication.h"
#include "Checksum.h"

#include <Print.h>
#include <WString.h>
Checksum Getsum;
Communication::Communication(int pin1) {
	// TODO Auto-generated constructor stub

	Serial_TxControl = pin1;
	timec = 0;
	device_address = 0;
	checksum = 0x0;
	msgState = 0;
	prvmsg = 0;
	msgByte = 0;

	//	HardwareSerial Serial2;
}

void Communication::begin() {

	Serial2.begin(115200);
	pinMode(Serial_TxControl, OUTPUT);
	digitalWrite(Serial_TxControl, LOW);  // Init Transceiver

}
void Communication::Start() {

	//if (project_status == 1) {

	if (Serial2.available()) {

		byte val = Serial2.read();
		Serial.print(val, HEX);
		if (waitForFirst) {
			if (val == delimiter) {
				//					Serial.println("First");
//				Serial.print("//");
//				Serial.println(msgState);
				addToBuffer(val);
				waitForFirst = false;
				msgState++;
				prvmsg = val;

			}

		} else if (!waitForFirst) {
//			Serial.print("//");
//			Serial.println(msgState);
			if (msgState == 1 && val != delimiter) {

				msgByte = val;
				//				Serial.print("msgCnt: ");
				//				Serial.println(msgCnt,HEX);
			} else if (msgState == 1 && val == delimiter) {
Serial.println("double 7E with msg 1");
				mPos = 0;
				msgByte = 0;
				msgState = 0;
				addToBuffer(val);
				waitForFirst = false;
				msgState++;

			}

			if (msgState > 0) {

				msgState++;

				if (val == delimiter && msgState == msgByte + 2) {
					//					Serial.println("Last");
					this->addToBuffer(val);
					this->processMessage(message, mPos);
					mPos = 0;
					waitForFirst = true;
					msgState = 0;
					msgByte = 0;

				} else if (prvmsg == delimiter && val == delimiter) {
//					Serial.print("//");
//					Serial.println(msgState);
					this->processMessage(message, mPos);
					mPos = 0;
					msgByte = 0;
					msgState = 0;
					addToBuffer(val);
					waitForFirst = false;
					msgState++;

				} else if (msgState == msgByte + 2 && val != delimiter) {
					// massage is bad
					//					Serial.println(" massage is bad");

//					Serial.print("//");
//					Serial.println(msgState);
					this->processMessage(message, mPos);
					mPos = 0;
					waitForFirst = true;
					msgState = 0;
					msgByte = 0;

				}

				else {

					this->addToBuffer(val);
				}

			}

			prvmsg = val;
		}

	}

}
void Communication::addToBuffer(byte val) {

	if (mPos < maxMessage) {
		message[mPos++] = val;

	}

}

void Communication::processMessage(byte* values, byte len) {
	}

One thing I figure out was when I reset the microcontroller the process data is correct, for while and then same thing happen, data is missing flags or data is shorten what is suppose to be.

Shahreza:
One thing I figure out was when I reset the microcontroller the process data is correct, for while and then same thing happen, data is missing flags or data is shorten what is suppose to be.

not sure if this is what you are experiencing but It is something im working on with my code for MAX485
Source Article: https://www.gammon.com.au/forum/?id=11428

Flushing the output

A small "gotcha" caught me when testing with hardware serial. The following code didn't work properly:

 digitalWrite (ENABLE_PIN, HIGH);  // enable sending

sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW);  // disable sending







Whilst it worked fine with software serial, the code above "turns off" the RS485 chip too quickly, because the last byte is still being sent from the serial hardware port.

A couple of solutions worked:




digitalWrite (ENABLE_PIN, HIGH);  // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (660);
digitalWrite (ENABLE_PIN, LOW);  // disable sending

I am not sending any message, all I am doing is receiving, and ENABLE_PIN is always low
one more thing I have no control on the master.

thanks

Shahreza:
I am not sending any message, all I am doing is receiving, and ENABLE_PIN is always low
one more thing I have no control on the master.

thanks

possible Causes / Verification ideas:
have you connected the MAX485 straight to a serial port to see if the computer can seeing everything.
Make sure there are End of Line resistors on both ends.
polarity is correct.
Z