Hello community.
As in the topic: I use Arduino to communicate with vibration sensor via RS485. I've made UART<>RS485 converter, using MAX485, as below.
of course GND of Arduino, converter and sensor are connected.
When I use SoftwareSerial, everything works great. When I send communication frame to sensor (fonction startpomiaru()) I receive answer from sensor:
:FF00000120
Which is ok. I have tested it on both Arduino Duemilanove and Mega R3 - it works. Code below
int pinControl = 9;
char kontrol;
char znak;
SoftwareSerial mySerial(10, 11); // RX, TX
void startpomiaru(){ // start measure
digitalWrite(pinControl,HIGH);
delay(1);
mySerial.write(58); //:
mySerial.print("0101000111");
mySerial.write(13); //cr
mySerial.write(10); //lf
delay(1);
digitalWrite(pinControl,LOW);
}
void setup() {
pinMode(pinControl, OUTPUT);
pinMode(12, OUTPUT);
mySerial.begin(38400);
Serial.begin(115200);
Serial.println("elo elo");
digitalWrite(pinControl, LOW);
digitalWrite(12, LOW); // power supply for sensor
}
void loop() {
if(Serial.available()){ //read from keyboard
kontrol = Serial.read();
//Serial.println("");
switch(kontrol){
case 's': // start measure
startpomiaru();
break;
}
}
while(mySerial.available()>0){
znak = mySerial.read();
Serial.write(znak);
}
}
Problem becomes, when I try to use hardware serial - instead Software Serial. Code for this is just the same, only "mySerial" replaced with Serial1.
int pinControl = 20;
char kontrol;
char znak;
//SoftwareSerial mySerial(10, 11); // RX, TX
void startpomiaru(){ // start measure
digitalWrite(pinControl,HIGH);
delay(1);
Serial1.write(58);
Serial1.print("0101000111");
Serial1.write(13);
Serial1.write(10);
delay(1);
digitalWrite(pinControl,LOW);
}
void setup() {
pinMode(pinControl, OUTPUT);
pinMode(17, OUTPUT);
Serial.begin(115200);
Serial.println("elo elo");
digitalWrite(pinControl, LOW);
digitalWrite(17, LOW); // power supply for sensor
Serial1.begin(38400);
}
void loop() {
if(Serial.available()){ //odczytanie danych z klawerki
kontrol = Serial.read();
//Serial.println("");
switch(kontrol){
case 's': // start measure
startpomiaru();
break;
}
}
while(Serial1.available()){
znak = Serial1.read();
Serial.write(znak);
}
}
I receive something like this:ÜÃÏÏÏ
I have tried for all serial ports (1,2,3) with the same result.
Moreover i have to use hardware serial, because SoftSerial can't keep up with the pace, when sensor sends big amount of measurement data - it receives only the size of buffer.
I have tested all serial ports in my mega - they all works
Thanks in advance for any ideas.