Reading RS232 with MAX3232 module

So I am working on a side project with force cells. I have 8-channel amplifier that outputs RS232 or RS485 and I would like to read that with Arduino Uno. I have the MAX3232 module to convert RS232 to TTL so I can read it with Arduino (If you think it would be easier to do it with RS485 I di not have preference on what to use, everything is new to me so there is no difference). As I am new to Arduino I was looking thru the forum for some existing solutions but I did not find any that would suit my implementation(not for RS232 nor for RS485(I have MAX485 chip for that)). Can someone please help me with the code on how to get the data from the amplifier.

I also read the Serial Basics)(https://forum.arduino.cc/t/serial-input-basics-updated/382007) on Arduino forum but that did not help me much.

The wiring is shown bellow.

And on the pictures below are all the Information's on serial communications that I got for the amplifier.



Thank you for all your replys in advance.

Perhaps the info makes more sense to you if you read about the MODBUS protocol first. The IDE offers a couple of MODBUS libraries.

You can try the following multiplexer scheme (Fig-1):


Figure-1:


Figure-2:

@GolamMostafa I have complete soldered unit which have only 4 outputs for RS232(VCC, RX, TX, GND) so I can not made the wiring you suggested. I will try to understand more about MODBUS as @DrDiettrich suggested. Currently I did not made any progress at all.

Do you like to try my multiplexing setup of Post-3? If yes, then the following is the complete wiring and test program.

1. Wiring diagram (Fig-1) among UNO, TTL <------->RS232 Converter, 4052 multiplexer, and users' RS232 devices.


Figure-1:

2. Functional diagram of 4052 multiplxer.
4052FunctionalDiag
Figure-2:

3. Schematic diagram of TTL <-----> RS232 Converter Module.


Figure-3:

4. Wiring procedures:
(1) Power down to all devices.

(2) Complete wiring connection among UNO, TTL <---> RS232 Converter, 4052 MUX as per Fig-1.

(3) With a multi-meter find "plus side (+)" of C1 (+10V). Solder one side of a jumper wire at the "plus side' of C1 and connect other side of the jumper wire with Pin-16 of 4052.

(4) With a multi-meter find "minus side ( - )" of C4 (-10V). Solder one side of a jumper wire at the "minus sid" of C4 and connect other side of the jumper wire with Pin-7 of 4052.

5. Connect RS232-0 device (Top one) to 4052 mux as per wiring of Fig-1.

6. Upload the following sketch in UNO.

#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5);  //SRX = DPin-4, STX = DPin-5

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  //----------------
  digitalWrite(2, LOW);  //UNO-2 is selected via multiplexer
  digitalWrite(3, LOW);
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    Serial.print(x);
  }
}

7. Check that some message/data has appeared on the Serial Monitor of UNO.

8. If step-7 works, then connect other RS232 devices one-by-one. Add codes with the Sketch of UNO to select the mux channesl to select the RS232-1, RS232-2, and RS232-3 devices.

Sorry to hear that :frowning:

Obviously you have several problems with your project:

  • multiple Serial connections require a software Serial or an Arduino Mega with multiple Serial channels in hardware
  • RS-232 or RS-485 converter, where the full duplex RS-232 converter is easier to install
  • a MODBUS library that allows to transmit commands to and receive data from your device.

@GolamMostafa thank you for your contribution. Currently I do not have multiple Arduinos to try what you suggested but I will order another one. Will try to understand what you suggested in the mean time.

1 Like

@DrDiettrich...so to clarify what hardware I have besides Arduino UNO.

RS232 module:

RS485 module:

RS232/RS485 shield:

In the last few days I tried to understand more about MODBUS protocol. I found several MODBUS libraries that i can use but in none of them I was able to found similar example to my implementation.

Would be possible for you to write some code how I can read holding registers on serial connection as I am not able to do it at the moment.

I also found some material on AltSoftSerial and SoftwareSerial that I will will have to use as UNO have only one HardwareSerial.

Will I be abble to make it work with the hardware that I have or do I need something else?

No, sorry. I have never used MODBUS myself and don't have any MODBUS device for testing.

Hopefully someone else can do it. I am completely lost in that topic :grinning:

Ok so a question if you can answer it based on the documentation that I provided in the first post.

I managet to make the Modbus connection work with the following sketch.

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 5); // RX, TX

#define MAX485_DE      3
#define MAX485_RE_NEG  2

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  mySerial.begin(9600);
  // Modbus slave ID 1
  node.begin(1, mySerial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
  uint16_t result;
  uint16_t data[6];
  
  //read holding registers
  result = node.readHoldingRegisters(0x50, 2);

  if (result == node.ku8MBSuccess)
  {
      int channel_1 = node.getResponseBuffer(0x01);
      Serial.println(channel_1);
      Serial.println();
  }

  delay(1000);
}

My amplifier have 8 output channels. I am able to read channel 1 with the commands in the provided code. Do you maybe know how to access the registers of other channels. I can not understand that from the documentation that I have.

Guessing, try:

 int channel2 = node.getResponseBuffer(0x02);

As I understand this you read 2 registers and each register has 16 bits. Read the remaining data from the ResponseBuffer.

this does not read the channel 2. If I read it I get response "0".

Channel 1 is reading the data correctly.

How should I do that?

Please post your code as readable text.

I was just trying to show the response that I get. Sorry for that.

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 5); // RX, TX

#define MAX485_DE      3
#define MAX485_RE_NEG  2

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  mySerial.begin(9600);
  // Modbus slave ID 1
  node.begin(1, mySerial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
  uint16_t result;
  uint16_t data[6];
  
  //read holding registers
  result = node.readHoldingRegisters(0x50, 4);

  if (result == node.ku8MBSuccess)
  {
      int channel_1 = node.getResponseBuffer(0x01);
      Serial.print("Channel 1: ");
      Serial.println(channel_1);
      int channel_2 = node.getResponseBuffer(0x02);
      Serial.print("Channel 2: ");
      Serial.println(channel_2);
      Serial.println();
  }

  delay(1000);
}

The response is shown below:
2021-10-11 07_21_07-Window

Do you have any idea what to do next?

Where can we find the documentation for your MODBUS library?
In detail I wonder about the arguments and result type of getResponseBuffer().

During the weekend I contacted the seller and just now I got the documentation for the Modbus RTU. Based on the descriptions on reading multiple channel registers I calculated the addresses and am able to read other channels.

Modbus RTU.pdf (335.8 KB)

The next step for me is to learn how to perform the zeroing of the load cels and other opperations like that that require input from arduino. I guess that has to do something with writing to the registers.

@GolamMostafa i am succesfuly getting the reading from the modbus connection. As I am using ModbusMaster library for communication I was reading the documentation how I can write to registers but none of the commands is doing it successfully. Can you maybe help me with the code:

#include <ModbusMaster.h>
#include <SoftwareSerial.h>


SoftwareSerial mySerial(4, 5); // RX, TX

#define MAX485_DE      3
#define MAX485_RE_NEG  2

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  mySerial.begin(9600);
  // Modbus slave ID 1
  node.begin(1, mySerial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

  //Perform-zero holding registers
  /*
    Channel address calls(HEX):
    CH1: 0x005E
    CH2: 0x0252
    CH3: 0x0446
    CH4: 0x063A
    CH5: 0x082E
    CH6: 0x0A22
    CH7: 0x0C16
    CH8: 0x0E0A
  */
  node.writeMultipleRegisters(0x005E, 1);
  node.writeMultipleRegisters(0x0252, 1);

  //sensitivity of sensor
  /*
    Channel address calls(HEX):
    CH1: 0x002E
    CH2: 0x0222
    CH3: 0x0416
    CH4: 0x060A
    CH5: 0x07FE
    CH6: 0x09F2
    CH7: 0x0BE6
    CH8: 0x0DDA
  */

  uint16_t sen_cell_1 = 1.8012; 
  uint16_t sen_cell_2 = 1.6585; 

  node.setTransmitBuffer(0, sen_cell_2);
  node.writeMultipleRegisters(0x0222, 1);

}

bool state = true;

void loop()
{
  int16_t result;
  
  //read holding registers
  /*
    Channel address calls(HEX):
    CH1: 0x0050
    CH2: 0x0244
    CH3: 0x0438
    CH4: 0x062C
    CH5: 0x0820
    CH6: 0x0A14
    CH7: 0x0C08
    CH8: 0x0DFC
  */
  result = node.readHoldingRegisters(0x0050, 2);

  if (result == node.ku8MBSuccess)
  {   
      int channel_1 = node.getResponseBuffer(0x0001);
      node.clearResponseBuffer();
      Serial.print("Channel 1: ");
      Serial.println(channel_1);
      Serial.println();
  }

  result = node.readHoldingRegisters(0x0244, 2);

    if (result == node.ku8MBSuccess)
  {   
      int channel_2 = node.getResponseBuffer(0x0001);
      node.clearResponseBuffer();
      Serial.print("Channel 2: ");
      Serial.println(channel_2);
      Serial.println();
  }

  delay(1000);
}

The documentation for amplifier is pinned here: Modbus RTU.pdf (335.8 KB)