Hi everyone!
Just a little introduction to my problem:
- need to connect two devices. Control unit “Amap99”, which will be the master, and “ArduinoMega2560”, which is going to be a slave. To solve this problem, I decided to use interface/bus “RS485”. Btw I’m so sorry for my terminology …
- I manage create a program for “Amap99”, which can successfully send everything I want it to, through “RS485”. It can also without any problem receive data, again through “RS485” (all in one program). Everything has been tested on oscilloscope and looks fine (I mean the data it is sending out).
- The problem is with “Arduino” – I can make a program, what will ONLY SEND data, and it works fine. And I can also make a program, what will ONLY RECEIVE data, and again – it works fine. But if I want to unite them – it will do some incredible things I though in this universe were impossible.
What I already have:
- “Amap99” is sending every three seconds the following string: “{A1}{BB}{41}{42}{CC}{DD}”
-I can also send “… {43}{44} …” instead of “… {41}{42} …”, by pressing a button. This all works fine so there’s no problem on the “Amap99”. - “Amap99” can also without problems receive any data send to him through “RS485”.
- “Arduino” can only receive the data in one program, and then send them to the PC through USB port. OR in other program it can send the following string to “Amap99” without problem: “{A1}{BB}{41}{42}{CC}{DD}”.
- But it can’t do both in ONE program.
What I’m using:
- TTL to RS485 converter, using MAX485 (which can be found here: Amazon.com)
- Arduino ATmega2560
- Compact control system Amap99 (everything for it is on this side: AMiT Automation)
- "Hercules utility Setup", for watching data sended from Arduino through USB port.
- If I get it right the converter should be used like this:
1)To receive I need to CLOSE the SerialTxControlPin for few milliseconds.
2)To send data I need to OPEN the SerialTxConctrolPin for few milliseconds.
Is this the right way to use it?
What I want to achieve with “Arduino”:
- I need the “Arduino” to act as a slave. So whenever it receive, some data it needs to immediately respond. For this example I just want it to send back what it got through the “RS485”.
- So when it receives “{A1}{BB}{41}{42}{CC}{DD}”, I want it to send back the exact same string. This is something I just can’t fit into one program and make it work properly. - In one program I can send the data, without any problem. And in other program I can receive them. But just can’t do both!
- So pleaseeee – if you would have any suggestion for the following programs I would be reaaaally thankful!
[1st. program] This following code I used as a "receiver":
#include <SoftwareSerial.h>
#define SSerialTxControl 3 // Pin for MAX485 - HIGH send, LOW receive
int byteReceived;
int rx_buff[64];
int rx_lenght = 0;
void setup()
{
Serial.begin(9600); //Serial communication for USB.
Serial3.begin(9600); //Serial communication for RS48 - 14 pin as Tx and 13 pin as Rx.
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, LOW); //Just receive data from RS485
}
void loop() {
digitalWrite(SSerialTxControl, LOW); //Just receive data from RS485
}
void serialEvent3()
{
//When receive data read it.
while (Serial3.available())
{
//Save it into variable "rx_buff"
rx_buff[rx_lenght] = Serial3.read();
rx_lenght++;
}
if(rx_buff[0] != 0x00)
{
for(int i=0;i<rx_lenght;i++){Serial.write(rx_buff[i]);} //Then just send it one by one through USB to the PC
rx_lenght = 0;
}
else
{
rx_lenght = 0;
}
}
[2st. program] This one I use as a "sender":
#include <SoftwareSerial.h>
#define SSerialTxControl 3
#define LED 13
int rx_buff[64]; //RX buffer
int tx_buff[64]; //TX buffer
unsigned int rx_lenght = 0;
void setup()
{
Serial3.begin(9600);
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(SSerialTxControl, HIGH); //Prepare MAX485 for sending data.
delayMicroseconds(28);
Serial3.write(0xA1); // First two starting bytes
Serial3.write(0xBB); // Just some another two bytes
Serial3.write(0x41); // Data
Serial3.write(0x42); // Data
Serial3.write(0xCC); // Just some another two bytes
Serial3.write(0xDD); // Stop bytes
delayMicroseconds(28);
digitalWrite(SSerialTxControl, LOW); //Prepare MAX485 for receiveing data
delay(500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(SSerialTxControl, HIGH); //Prepare MAX485 for sending data.
delayMicroseconds(28);
Serial3.write(0xA1); // First two starting bytes
Serial3.write(0xBB); // Just some another two bytes
Serial3.write(0x43); // Data
Serial3.write(0x44); // Data
Serial3.write(0xCC); // Just some another two bytes
Serial3.write(0xDD); // Stop bytes
delayMicroseconds(28);
digitalWrite(SSerialTxControl, LOW); //Prepare MAX485 for receiveing data
delay(500);
}
[3rd program] But when I want to unite them it doesn't work. It doens't receive data properly and it doesn't even send BIT to the "Amap99"
#include <SoftwareSerial.h>
#define SSerialTxControl 3 // Pin for MAX485 - HIGH send, LOW receive
volatile int rx_buff[64];
volatile int rx_length=0;
void setup()
{
Serial.begin(9600); //Serial communication for USB.
Serial3.begin(9600); //Serial communication for RS48 - 14 pin as Tx and 13 pin as Rx.
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, LOW); //Prepare MAX485 for receiveing data
}
void loop() {
digitalWrite(SSerialTxControl, LOW); //Prepare MAX485 for receiveing data
}
void serialEvent3 ()
{
while(Serial3.available()) //When you receive some data save it into the rx_buff[] variable
{
rx_buff[rx_length] = Serial3.read();
rx_length++;
}
for(int i=0;i<rx_length;i++) //Then send it to the USB
{
Serial.write(rx_buff[i]);
}
digitalWrite(SSerialTxControl, HIGH); //Prepare MAX485 for sending data.
delay(100); //Give him some time to open
for(int i=0;i<rx_length;i++)
{
Serial3.write(rx_buff[i]); //And then send the data you got back through the RS485
}
digitalWrite(SSerialTxControl, LOW); //Prepare MAX485 for receiveing data
delay(100);
rx_length=0;
}

