Nodemcu with rs485

I am trying to use a rs485 with nodemcu. Im using max485 module to connect it.but in my code modbus.begin() could not run and it is giving me error of invalid conversion from int to serialmode -fpermissive. why is that ?

this is my code

#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DFRobot_EC.h"
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>    
#include <LiquidCrystal_I2C.h> 

LiquidCrystal_I2C lcd(0x27, 20, 4);

#define TROUBLESHOOT
#define RX            3   //pin 1 MAX485
#define RE            15   //pin 2&3 MAX485
#define TX            1   //pin 4 MAX485
#define modbus        Serial1

uint8_t test=0,counter,crc_l,crc_h,address[20];
uint16_t crc, ec, ecCal;
uint32_t i,j,data,timer1;
String message;

void setup()
{
  Serial.begin(115200);
  modbus.begin(9600,SERIAL_8N1,RX,TX);
  pinMode(RE,OUTPUT);
  digitalWrite(RE,LOW);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("EC SENSOR MODBUS");

  Serial.begin(115200);
  Serial.println("EC SENSOR MODBUS");
  delay(1000);
  lcd.clear();
}

void loop()
{
  if(millis()-timer1>=1000)
  {
    timer1=millis();
    Modbus_request(0x01,0x03,0x00,0x02);
  }

  if(modbus.available())
  {
    i=0;
    while(modbus.available()) address[i++]=modbus.read();

    while(address[0]==0)
    {
      for(j=0;j<i;j++) address[j]=address[j+1];
      i--;
    }

    #if defined (TROUBLESHOOT)
      if(i>1)
      {
        for(j=0;j<i;j++)
        {
          if(address[j]<0x10) Serial.print("0");
          Serial.print(address[j],HEX);
          Serial.print(" ");
        }
        Serial.println();  
      }
    #endif   

    if(i>2)
    {
      crc=CRC16(i-2);
      crc_l=crc&0xFF;
      crc_h=(crc>>8)&0xFF;      
    }
    
    if((address[i-2]==crc_l)&(address[i-1]==crc_h))
    {
      ecCal=256*address[3]+address[4];
      ec=256*address[5]+address[6];
      Serial.println("Calibration Value=" + String(ecCal) +"us/cm EC Value=" + String(ec) + "us/cm");
    }
  }

    lcd.setCursor(7,1);
    lcd.print("     ");

    lcd.setCursor(7,2);
    lcd.print("     ");

    lcd.setCursor(0,0);
    lcd.print("Calibration Value=" + String(ecCal) +"us/cm EC Value=" + String(ec) + "us/cm");
    
}

void Modbus_request(unsigned char id,unsigned char code,unsigned int addr,unsigned int quantity)
{
  address[0]=id;
  address[1]=code;
  address[2]=addr>>8;
  address[3]=addr;
  address[4]=quantity>>8;
  address[5]=quantity;
  crc=CRC16(6);
  address[6]=crc&0xFF;
  address[7]=(crc>>8)&0xFF;

  #if defined (TROUBLESHOOT)
    for(i=0;i<8;i++)
    {
      if(address[i]<0x10) Serial.print("0");
      Serial.print(address[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  #endif

  digitalWrite(RE,HIGH);
  delay(10);
  for(i=0;i<8;i++) modbus.write(address[i]);
  delay(10);     
  digitalWrite(RE,LOW);
}

int CRC16(int DataLength)
{
  unsigned int i,j,CheckSum;
  CheckSum=0xFFFF;
  for (j=0;j<DataLength;j++){
    CheckSum=CheckSum^address[j];
    for(i=0;i<8;i++){
      if((CheckSum)&0x0001==1) CheckSum=(CheckSum>>1)^0xA001;
      else CheckSum=CheckSum>>1;}}
  return CheckSum;   
}

and this is my error message :

Arduino: 1.8.13 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

WARNING: library LiquidCrystal_I2C claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp8266 architecture(s).

C:\Users\LENOVO\OneDrive\Documents\Arduino\ecnewww\ecnewww.ino: In function 'void setup()':

ecnewww:29:37: error: invalid conversion from 'int' to 'SerialMode' [-fpermissive]

   modbus.begin(9600,SERIAL_8N1,RX,TX);

                                     ^

In file included from C:\Users\LENOVO\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/Arduino.h:245:0,

                 from sketch\ecnewww.ino.cpp:1:

C:\Users\LENOVO\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/HardwareSerial.h:87:10: error:   initializing argument 3 of 'void HardwareSerial::begin(long unsigned int, SerialConfig, SerialMode, uint8_t)' [-fpermissive]

     void begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin)

          ^

exit status 1

invalid conversion from 'int' to 'SerialMode' [-fpermissive]



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Sorry for the inconvenience... i ve fixed the forum.. can you take a look at it..please?

Please post the full error message using code tags when you do. You can copy it using the "Copy error message" button in the IDE

there i ve included the error message as well.thanks :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.