RS485 and atmega328 problems....

HI, i have some wierd problem
I have made a PCB for atmega328, max485, some optocouplers and so on…
Everithing works as expected…
BUT
When i connect RS485 to my pcb on max485 something strange happenes

  1. on pc (using usb to rs485 converter) in terminal i se aLOT of traffic going on… BUT from where is this on the line is only PC and my PCB and my PCB dosent send anything, the PC should send anything, so my idea is there sould be some interference going on?
  2. after a minute or so my PCB stops working (dead)
  3. my pcb is then dead and i have to reupload my sketch… then works again…

OK i know this is not a lot of informations but have any one any idea?
How to start debuging?

first you need to share hardware schematic

secondly you need to paste the code

if you have provision for serial Tx & Rx line. Just send the modbus function through it with your code. Check whether response get properly. CRC calculation does properly.

the code

byte deviceId  = 'A';

byte inpPins[5] = {14,15,16,17,7};
//byte outPins[5] = {13,12,11,10,9};
byte outPins[5] = {9,10,11,12,13};
byte ledPins[5] = {6,5,4,3,2};
byte currState[5]= {0,0,0,0,0};
byte lastState[5]= {0,0,0,0,0};

int enablePin = 8; // ebanble pin for slave

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String inputString2 = "";         // a string to hold incoming data
boolean stringComplete2 = false;  // whether the string is complete

byte outPinStatusIndex = 0;
byte outGoDownPinIndex = 1;
byte outGoUpPinIndex   = 2;
byte inpLimitDownPinIndex = 0;
byte inpLimitUpPinIndex   = 1;

bool goDown = false;
bool goUp   = false;
char lastAct= 'x';//u==up d == down
void setup() {
  for(int i=0; i < 5; i++){
    pinMode(inpPins[i],INPUT);
  }  
  for(int i=0; i < 5; i++){
    pinMode(outPins[i],OUTPUT);
  }  
  for(int i=0; i < 5; i++){
    pinMode(ledPins[i],OUTPUT);
  }
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);//reciver
  Serial.begin(9600);
  delay(4000);
  //digitalWrite(enablePin, HIGH);//sender
 // delay(1000);
 // Serial.println("readdy");
  //delay(1000);
 
 // delay(1000);

}

void loop() {

  if(goDown){
   // if(lastAct!='d'){
    //  lastAct='d';
      digitalWrite(outPins[1],HIGH);
      if(digitalRead(inpPins[inpLimitDownPinIndex])){      
        goDown=false;
        stopMove();
      }
  //  }else{
   //   goDown=false;
   //   stopMove();    
   // }
  }else
  if(goUp){// && lastAct!='u'){
 //   if(lastAct!='u'){
   //   lastAct='u';
      digitalWrite(outPins[1],LOW);
      if(digitalRead(inpPins[inpLimitUpPinIndex])){
        goUp=false;
        stopMove();
      }
  //  }else{
  //    goUp=false;
  //   stopMove();
  //  }
  }else{
    digitalWrite(outPins[outPinStatusIndex],LOW);
    delay(20);
    digitalWrite(outPins[outPinStatusIndex],HIGH);
    delay(20);
  }



  if (stringComplete) {
    delay(20);
    //Serial.println(inputString);
    
    if (inputString.charAt(2) == deviceId) {
      if(inputString.charAt(3) == '1'){//go down
        //digitalWrite(enablePin, HIGH);//sender
        //Serial.println("go DOWN");
        //delay(10);
        //digitalWrite(enablePin, LOW);//reciver

        digitalWrite(outPins[outGoDownPinIndex],HIGH);
        digitalWrite(outPins[outGoUpPinIndex],LOW);
        goDown=true;
        goUp=false;
      }else
      if(inputString.charAt(3) == '2'){//go up
        //digitalWrite(enablePin, HIGH);//sender
        // delay(10);
         //Serial.println("go UP");
         //delay(10);
        digitalWrite(enablePin, LOW);//reciver
         
        digitalWrite(outPins[outGoDownPinIndex],LOW);
        digitalWrite(outPins[outGoUpPinIndex],HIGH); 
        goDown=false;
        goUp=true;        
      }else
      if(inputString.charAt(3) == '0'){//STAY
        stopMove();
        goDown=false;
        goUp=false;
      }
    } 
    inputString = "";
    stringComplete = false;
  }
}//main loop

void stopMove(){
  //Serial.println("STOP");
  digitalWrite(outPins[outGoDownPinIndex],LOW);
  digitalWrite(outPins[outGoUpPinIndex],LOW);
}

void serialEvent() {/*   SerialEvent occurs whenever a new data comes in the  hardware serial RX.  This routine is run between each  time loop() runs, so using delay inside loop can delay  response.  Multiple bytes of data may be available.  */
  while (Serial.available()) {
    char inChar = (char)Serial.read();     // get the new byte:
      inputString += inChar;    // add it to the inputString:
      if (inChar == '\n') {    // if the incoming character is a newline, set a flag    // so the main loop can do something about it:
          if (inputString.charAt(0) == 'X' && inputString.charAt(1) == 'X' ){
              stringComplete = true;
          }//if
      }//if
  }//while
}//serial event

The code is simple…
listen to com port and when
XXA1 (go up until the xternal signal is high)
XXA2 (go down until external signal is high)
XXA0 (turn of all outputs)
if nothing is going on the LED is blinking

pcb.pdf (200 KB)

byte deviceId  = 'A';

have you seen modbus syntax
http://www.simplymodbus.ca/

I dont understand your code here.I have attached library for reference. Unzip the code & test it.

They impliment for read holding register & write register function.

I could not able to get what your trying to do above. if explain project in detail i wil try to modified here.

ModbusRtu.zip (189 KB)

Modbus rtu is to slow and ascii there is no modbus slave ascii library for arduino so....