November 09, 2024, 04:08:34 AM

MOD-RS485-ISO

Started by centra, February 01, 2013, 03:08:00 PM

Previous topic - Next topic

harry85

i tried this, but still not working. so no output at all:

RX code:
#define DE  4
#define _RE 36

#define SEND    1
#define RECEIVE 0

//#define MODE  SEND
#define MODE  RECEIVE

void Enable (int Mode)
{
  digitalWrite (DE, Mode);
  digitalWrite (_RE, Mode);
}

void setup()
{
  Serial.begin (9600);

  pinMode (DE, OUTPUT);
  pinMode (_RE, OUTPUT);

  Enable (MODE);
}

void loop ()
{

  //Serial.print("ping");
  while (Serial.available() > 0)
    Serial.print((char)Serial.read());
}

Stanimir5F

Quote from: harry85 on September 20, 2023, 03:44:04 PM...
and receiver or RX code:


#define DE  4  //TX
#define _RE 36 //RX

#define SEND    0
#define RECEIVE 1


//#define MODE  SEND
#define MODE  RECEIVE


void Enable (int Mode)
{
  //digitalWrite (DE, Mode);
  digitalWrite (_RE, Mode);
}

void setup()
{
  Serial.begin (9600);

  pinMode (DE, OUTPUT);
  pinMode (_RE, OUTPUT);

  Enable (MODE);
}
...

I noticed that you have changed the values of the macros SEND/RECEIVE as well as changing the MODE (commenting SEND and uncommenting the RECEIVE). By doing so the two programs are basically the same since SEND and RECEIVE are supposed to be constants used later in the "Enable" function.

Try ONLY commenting the MODE macro. In other words:
Sender:
#define DE  4
#define _RE 36

#define SEND    1
#define RECEIVE 0

#define MODE  SEND
//#define MODE  RECEIVE

void Enable (int Mode)
{
  digitalWrite (DE, Mode);
  digitalWrite (_RE, Mode);
}

void setup()
{
  Serial.begin (9600);

  pinMode (DE, OUTPUT);
  pinMode (_RE, OUTPUT);

  Enable (MODE);
}

void loop ()
{

  Serial.print("ping");
  while (Serial.available() > 0)
    Serial.print((char)Serial.read());
}

And receiver:
#define DE  4  //TX
#define _RE 36 //RX

#define SEND    1
#define RECEIVE 0

//#define MODE  SEND
#define MODE  RECEIVE

void Enable (int Mode)
{
  //digitalWrite (DE, Mode);
  digitalWrite (_RE, Mode);
}

void setup()
{
  Serial.begin (9600);

  pinMode (DE, OUTPUT);
  pinMode (_RE, OUTPUT);

  Enable (MODE);
}

void loop ()
{

  //Serial.print("ping");
  while (Serial.available() > 0)
    Serial.print((char)Serial.read());
}
May the Source be with You!

harry85

#17
Hi this example still not working, with your proposal.

i try to do this example with two olimex iso poe instead of a nodeMCU.

the example you can find here -> : https://microcontrollerslab.com/rs485-serial-communication-esp32-esp8266-tutorial/   

i tried this example with a nodeMCU and it could work. Now i tried to set it up with an olimex poe iso, and it did not work at all.

i assume that the addition TTL RS485 module in between need to know where the TX and RX and Enable_Pin is exactly?

could you please help me with this example provided in the url?  :)

Thanks


Stanimir5F

First of all - I want to apologize for a mistake I made and I mislead you!

The pins DE and _RE are not meant to be the UART TX/RX pins (4/36) as I initially got confused but the pins that goes to the respective signals on the RS485. In my case (I tested it with MOD-RS485 which has a UEXT) the pins were DE=14 (UEXT.9), _RE=5 (UEXT.10). In your case these could (and most likely will) be different and you have to check which are the pins connected to the DE and RE on your module.

And the other thing is since on ESP32-POE-ISO the UART0 that goes to the USB is not the same as the one that goes to the UEXT a different "Serial" is needed. In case you are using the UART1 on the UEXT (TX=4 and RX=36) then you have to use Serial1 for the communication between the modules.

So with these 2 things in consideration try with the following code (of course with edited DE, _RE numbers and the respective value SEND/RECEIVE for the MODE macro):


#define DE  14
#define _RE  5

#define SEND    1
#define RECEIVE 0

//#define MODE  SEND
#define MODE  RECEIVE

#define USB_Serial  Serial
#define UEXT_Serial Serial1

void Enable (int Mode)
{
  digitalWrite (DE, Mode);
  digitalWrite (_RE, Mode);
}

void setup()
{
  USB_Serial.begin (115200);
  UEXT_Serial.begin (115200);

  pinMode (DE, OUTPUT);
  pinMode (_RE, OUTPUT);

  Enable (MODE);
}

void loop ()
{
#if (MODE==SEND)
  while (USB_Serial.available() > 0)
    UEXT_Serial.print((char)USB_Serial.read());
#elif (MODE==RECEIVE)
  while (UEXT_Serial.available() > 0)
    USB_Serial.print((char)UEXT_Serial.read());
#endif
}

With this code programmed on 2 ESP32-POE-ISO boards (one as sender mode and the other as receiver) and connected properly the boards this should create a one direction communication. Meaning that if you open 2 terminals and input a character on the sender one you will receive that character on the receiver terminal.

I hope this helps you!
May the Source be with You!

LubOlimex

Technical support and documentation manager at Olimex

harry85

wow. Thanks allot, it worked out  :) 


how can i adjust following part at the receiver to receive the serial message and get some output like:

Received message: 123
Received message: 567
...

the part which should be adjusted:

void loop ()
{
#if (MODE==SEND)
  while (USB_Serial.available() > 0)
    UEXT_Serial.print((char)USB_Serial.read());
#elif (MODE==RECEIVE) // receiver gets the message
  while (UEXT_Serial.available() > 0)
    USB_Serial.print((char)UEXT_Serial.read());
#endif
}



thanks in advance.


Stanimir5F

I am happy to see that it worked! Like I said I got confused earlier and gave you wrong advice the first time.

Anyway on your question now... I am not 100% if I understand what exactly your goal is. My understanding is that you want to see the messages not character by character (as it is with the example I showed you) but instead a whole message (probably a line or something like that).
If it's not that please can you clarify what specifically you mean. But if it is then what you could do something like this:
On the receiver side not to print the character as they are received but instead keep them in a buffer (array long enough to store the maximum length of a message). And when a newline ('\r' or '\n') character is received then you close that array with a 0 (to be null terminated string) and then print the whole string at once.
Should look something like this:

#define STRING_MAX_LENGTH  100
char Buffer[STRING_MAX_LENGTH], BuffIndex=0;  // global variables
.....
#elif (MODE==RECEIVE)
  while (UEXT_Serial.available() > 0)
  {
    c = UEXT_Serial.read();
    if ((c=='\r') || (c=='\n'))
    {
      Buffer[BuffIndex] = 0;
      USB_Serial.printf ("Received message: %s\n\r", Buffer);
      BuffIndex=0;
    }
    else
    {
      Buffer[BuffIndex] = c;
      BuffIndex++;
    }
  }
#endif

And depending on the settings of your terminal program whether it sends only carriage return, only line feed or both you may have to adjust the if condition. But generally speaking I think that should do the work (if I get your idea correctly).
May the Source be with You!

harry85

Perfect, thanks allot :)
this is exactly what i needed