Browsed by
Tag: communication

How to test / read data from console / RS232

How to test / read data from console / RS232

RS232 commnication is not necessary, but is very useful. You can write some code and you want to switch on a led. The easier way to know what is happening if the led isn’t turning on, is through RS232, and watching what are the values of the vars,…
 
So, if you have purchased an Ardbox, you’ll need to initialize communications through:

  Serial.begin(9600);
  while (!Serial) {}  

and after that, you’ll be able to send any value to the RS232 port, in order to show it, through the Arduino IDE console, minicom,…, or any other Serial Console that you have in your Laptop.

  Serial.println("MyVar value");

If you have an M-Duino, you don’t need the

  while (!Serial) {}  

line.

I2C Communication

I2C Communication

In order to communicate through I2C, we have to do two type of programs. We have to create some code for the Master device (PLC in our case), and another piece of code for the Slave devices, such as PLC, or any other device (RTC clock, ADC,…, that has an I2C available port).

Master Code:

#include <Wire.h>
void setup()
{
  Wire.begin();

  Serial.begin(9600); 
  delay(500);
}
void loop()
{
  Serial.println("Initial");

  Wire.requestFrom(2, 1);  // request 1 byte from device #2 
  while(Wire.available())  { 
    char c = Wire.read();
    Serial.println(c);
  } 
  delay(100) ;
}

Slave code:

#include <Wire.h>

void setup()
{
  Wire.begin(2);                // join i2c with the device #2
  Wire.onRequest(requestEvent);
}

void loop(){
  delay(100);
}

void requestEvent()
{
  Wire.write("k"); // respond with 1-byte-message
}                       

This is a minimum code to communicate through I2C. In this case we have a Master PLC that asks for a character. The Master asks Slave (#2). When Slave receives the request, respond with one “k” letter. We will see all of them through the RS232 console.

How to Modbus-Ethernet

How to Modbus-Ethernet

  • Modbus is a Serial communications protocol and it is used for communicating with several industrial devices. It may run over RS485, Ethernet, or whatever hardware interface.
  • If we have an ethernet connection between a lot of devices, we can use this hardware connection (eth), and over it, we can use our own protocol (custom protocol,…), or for instance, Modbus protocol. The way is the same as using RS485, but hardware connections will become much easier, the speed will be higher, and the possible frame errors will be controlled by TCP/IP layers.
  • So, if you know Modbus, you have an easier way to implement it!.