Go Down

Topic: DUE Problem With Serial Ports And rs485 board (Read 742 times) previous topic - next topic

KeeWay

I have a Arduino Due already setup and working with my Nextion LCD display and now i have been trying to add controls to my VFD via the rs485 board.
I followed the instructions from this site since it is the same VFD i have. Looking at the code it uses the sofwareserial.h
and from what I can gather it will not work with the DUE so I have purchased a mega2560 and it tests out fine via softwareserial only.

I want to use this on the actual hardware serial ports but so far on both the mega and due i cant get it to work on any of the serial ports onboard, is there something i am missing?

I have moved the Nextion LCD between serial ports and it works fine so i know the ports/boards are good it is just the rs485 board only seems to only work via the softwareserial.

I also have swapped around the RX/TX connections with no luck.

here is the original working VFD/rs485 code using the software serial which works fine on my mega board :

Code: [Select]

#include <SoftwareSerial.h>
 
// RS485 connections
// D8 = DI (TX)
// D9 = DE
// D10 = RE
// D11 = RO (RX)
 
// adjust speed
String     speed=":010203030BB837\r\n";
 
 
char* read_speed=":01040300";
char* change_speed=":010203030BB8";
char* stop=":01030108";
char* start=":01030101";
 
 
SoftwareSerial myserial(11,8); // RX, TX
 
void setup() {
 Serial.begin(9600);
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);
 myserial.begin(9600);
 digitalWrite(9,LOW); digitalWrite(9,LOW);
}
 
void setSpeed(int v) {
    Serial.print("Set new speed "); Serial.println(v,DEC);
    char* n=change_speed;
  //char n[13];
  //strcpy(n,change_speed);
  n[9]=toHexa((v/256)>>4);
  n[10]=toHexa((v/256)&0xf);
  n[11]=toHexa((v&0xff)>>4);
  n[12]=toHexa((v&0xff)&0xf);
Serial.println(n);
  query(n); // set new speed
}
 
void loop() {
 // while(myserial.available()) Serial.write(myserial.read());
  delay(1000);
 
  query(start);
  delay(10000);
 
  //delay(5000);
  for(long v=1000; v<16000; v+=1000) {setSpeed(v); delay(5000); }
 
  query(stop);
  delay(10000);
}
 
void transmit(String msg) {
  // activate driver
  digitalWrite(9,HIGH); digitalWrite(9,HIGH);
  delay(50);
  myserial.print(msg);
  delay(1);
  digitalWrite(9,LOW); digitalWrite(9,LOW);
}
 
char hexa(char byte) { // return hexa value of that ASCII char
  if(byte<='9') return byte & 0x0f;
  if(byte<='F') return (byte & 0x0f) + 9; // A=0x41 --> 0x0a
}
 
char toHexa(int i) {
 if(i<10) return 0x30 | i;
 return 0x41 + i - 10;
}
 
char crc(char* buffer) {
 int l=strlen(buffer);
 int i=1;
 int chk=0;
 while(i<l) { Serial.print(hexa(buffer[i])<<4|hexa(buffer[i+1]),HEX); chk+= ( hexa(buffer[i])<<4|hexa(buffer[i+1]) ); i+=2; Serial.print(" ");}
 Serial.print(":"); Serial.println(chk,HEX);
  Serial.println(0x100-chk,HEX);
 return (0x100-chk) & 0xff;
}
 
void query(char* cmd) {
  char lrc = crc(cmd);
  String msg = cmd;
  msg+=toHexa((lrc&0xf0)>>4);
  msg+=toHexa(lrc&0x0f);
  msg+="\r\n";
  Serial.print(msg);
  transmit(msg);
}


here is the modified code to try and make it work with Serial2 on the onboard serial with the mega and due boards:

Code: [Select]

//#include <SoftwareSerial.h>
 
// RS485 connections
// D8 = DI (TX)
// D9 = DE
// D10 = RE
// D11 = RO (RX)
 
// adjust speed
String     speed=":010203030BB837\r\n";
 
 
char* read_speed=":01040300";
char* change_speed=":010203030BB8";
char* stop=":01030108";
char* start=":01030101";
 
 
//SoftwareSerial myserial(11,8); // RX, TX
 
void setup() {
 Serial.begin(9600);
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);
 //myserial.begin(9600);
 Serial2.begin(9600);
 digitalWrite(9,LOW); digitalWrite(9,LOW);
}
 
void setSpeed(int v) {
    Serial.print("Set new speed "); Serial.println(v,DEC);
    char* n=change_speed;
  //char n[13];
  //strcpy(n,change_speed);
  n[9]=toHexa((v/256)>>4);
  n[10]=toHexa((v/256)&0xf);
  n[11]=toHexa((v&0xff)>>4);
  n[12]=toHexa((v&0xff)&0xf);
Serial.println(n);
  query(n); // set new speed
}
 
void loop() {
 // while(myserial.available()) Serial.write(myserial.read());
  delay(1000);
 
  query(start);
  delay(10000);
 
  //delay(5000);
  for(long v=1000; v<16000; v+=1000) {setSpeed(v); delay(5000); }
 
  query(stop);
  delay(10000);
}
 
void transmit(String msg) {
  // activate driver
  digitalWrite(9,HIGH); digitalWrite(9,HIGH);
  delay(50);
  //myserial.print(msg);
  Serial2.print(msg);
  delay(1);
  digitalWrite(9,LOW); digitalWrite(9,LOW);
}
 
char hexa(char byte) { // return hexa value of that ASCII char
  if(byte<='9') return byte & 0x0f;
  if(byte<='F') return (byte & 0x0f) + 9; // A=0x41 --> 0x0a
}
 
char toHexa(int i) {
 if(i<10) return 0x30 | i;
 return 0x41 + i - 10;
}
 
char crc(char* buffer) {
 int l=strlen(buffer);
 int i=1;
 int chk=0;
 while(i<l) { Serial.print(hexa(buffer[i])<<4|hexa(buffer[i+1]),HEX); chk+= ( hexa(buffer[i])<<4|hexa(buffer[i+1]) ); i+=2; Serial.print(" ");}
 Serial.print(":"); Serial.println(chk,HEX);
  Serial.println(0x100-chk,HEX);
 return (0x100-chk) & 0xff;
}
 
void query(char* cmd) {
  char lrc = crc(cmd);
  String msg = cmd;
  msg+=toHexa((lrc&0xf0)>>4);
  msg+=toHexa(lrc&0x0f);
  msg+="\r\n";
  Serial.print(msg);
  transmit(msg);
}


appreciate any help you can give...

thanks
James

ard_newbie

#1
Jan 12, 2018, 05:59 am Last Edit: Jan 12, 2018, 06:33 am by ard_newbie
You have to set USART0(Serial1) or USART1(Serial2) in RS485 mode, you could see this thread as a good starting point for an example sketch:
https://forum.arduino.cc/index.php?topic=451676.0

As a RS 485 transceiver, you could use this one (3.3V) :
https://www.ebay.com/itm/RS485-Board-3-3V-SP3485-RS-485-Transceiver-Evaluation-Development-Module-Kit/261235559628?hash=item3cd2da4ccc:m:mW9Qx86yFKXYeZSGoUWVI3Q

KeeWay

ard_newbie thanks for the info, i looked at the first link and being that i am rather new to all this i was getting kinda lost in the code.

the rs485 board you referenced is a 3.3v board so maybe that is where i am missing something, then based on your recommendation the current rs485 board i have will not work because it is 5v?

thanks
James

ard_newbie


The Arduino DUE is 3.3V compliant only. I guess your 5V transceivers might work with logic level shifters.

Go Up