Hi guys,
Theses days I'm working on a project which uses two RS485 network to communicate with other equipments. (a microcontroller controlled Step Motor and a weight transmitter). I wrote the code, That works perfectly on UNO. Problem till now is just some how like this: When I use Software Serial on UNO, the two network can't through changing the listening port to get the right value. Now I've changed my board to mega 2560. I can't even get the right value from each network correctly, which worked always on UNO with Software Serial. Cause the whole project is relative huge, I just put a modified small part about reading the weight transmitter's value here:
#include <SoftwareSerial.h>
SoftwareSerial RS485_Serial(4, 5);
int SignGewicht[3];
int Gewicht[3];
int RS485_text;
int RS485_SwitchPin = 8;
long time;
int UpdateZero = 1;
int WaageID = 0;
void setup(){
RS485_Serial.begin(38400);
Serial.begin(19200);
Serial.println("Start");
time = millis();
pinMode(RS485_SwitchPin, OUTPUT);
digitalWrite(RS485_SwitchPin, LOW);
}
void loop(){
if (UpdateZero == 1){
digitalWrite(RS485_SwitchPin, HIGH);
RS485_Serial.write("$01ZERO03\r");
digitalWrite(RS485_SwitchPin, LOW);
UpdateZero = 0;
}
//---Routine------------------------------------------------------
if (millis()-time > 100){
digitalWrite(RS485_SwitchPin, HIGH);
RS485_Serial.write("$01t75\r");
digitalWrite(RS485_SwitchPin, LOW);
// Serial.write("\n");
time = millis();
}
if (RS485_Serial.available()){
WaageEinlesen();
//Serial.write(RS485_Serial.read());
}
}
void WaageEinlesen(){
if (RS485_Serial.available()==14){
if (RS485_Serial.read()==38){
WaageID = ((RS485_Serial.read()-48)*10); //10er Stelle ID
WaageID = WaageID + (RS485_Serial.read()-48); // 1er Stelle ID
SignGewicht[WaageID] = RS485_Serial.read(); //"-" bei negativem Wert, "0" bei positivem
if (SignGewicht[WaageID] == 32){
Serial.println("Gewicht zu hoch!");
while (RS485_Serial.available()){
RS485_text = RS485_Serial.read();
}
return;
}
else if (SignGewicht[WaageID] == 45) SignGewicht[WaageID] = -1;
else SignGewicht[WaageID] = 1;
Gewicht[WaageID] = ((RS485_Serial.read()-48)*10000);
Gewicht[WaageID] = Gewicht[WaageID]+((RS485_Serial.read()-48)*1000);
Gewicht[WaageID] = Gewicht[WaageID]+((RS485_Serial.read()-48)*100);
Gewicht[WaageID] = Gewicht[WaageID]+((RS485_Serial.read()-48)*10);
Gewicht[WaageID] = (Gewicht[WaageID]+((RS485_Serial.read()-48)))*SignGewicht[WaageID];
Serial.println(Gewicht[WaageID]);
}
}
else{
Serial.println("ERROR Serial Read");
}
while (RS485_Serial.available()){
RS485_text = RS485_Serial.read();
}
}
P.S. Yeh, the communication mode is "Ask & Answer."
How can solve the problem. Is the problem only on the code or I've to change some hardware configuration to make it works?
Thank you!
-Start of the message, "01"-weight transmitter's address,


