User Tools

Site Tools


how_tos:serial_port_programming

Serial Port Programming

WattmonPRO and WattmonMEGA come with an RS-232 serial interface on pins 7 and 8 and an RS-485 port on an RJ45 connector. The device is configured by default for RS-232 voltage levels and by using a simple DB9 cable can be connected to any serial device. For specific devices that require TTL levels, a jumper setting inside the device can bypass the level converter IC and it can thus be used to interface with sensors or other devices at 3.3V DC.

Using the RS-232 Serial Port

To open the serial port, use the fseropen() function. The serial port is treated like a file after opening and standard file functions such as fread(), fwrite(), fgets() and filesize() apply.

An example of using the serial port is shown below:

<?
  // Open the serial port at 115200 baud in non-blocking mode
  $fp=fseropen(115200,0);
  if (!$fp) {
    print("Serial open failed");
  } else {
    $res=fwrite($fp,"Hello from wattmon");
    if ($res) {
      print("An error occurred while trying to write to the port");
    }
    fclose($fp);
  }
?>

Jumper Settings Inside Wattmon

Settings for RS-232 Mode < Settings for RS-232 Mode Settings for TTL Mode < Settings for TTL Mode

RS-232 Cable Connection

In order to connect to a device you will need to procure a DB9 cable (male or female) and connect it up as shown in the following diagram: RS-232 DB9 Connection to Wattmon

Using the RS-485 Serial Port

In order to use the RS-485 port, you need to disable modbus polling. This is done from the Devices > Options > Edit Communication Settings as shown here:

Disable Modbus Polling

To open the RS-485 port, use the f485open() function. The RS-485 serial port is treated like a file after opening and standard file functions such as fread(), fwrite(), fgets() and filesize() apply.

An example of using the RS-485 serial port is shown below:

<?
  // Open the 485 port at 115200 baud with no parity
  $fp=f485open(115200,0);
  if (!$fp) {
    print("Serial open failed");
  } else {
    $res=fwrite($fp,"Hello from wattmon");
    if ($res) {
      print("An error occurred while trying to write to the port");
    }
    fclose($fp);
  }
?>

RS-485 Cable Connection

Use a standard RJ-45 cable and cut it to connect your RS-485 device to WattmonPRO or WattmonMEGA.

PIN Color Description
1,2 Orange GND
4 Blue D+ / A
5 White-Blue D- / B

RJ45 Connection Pinout

how_tos/serial_port_programming.txt · Last modified: 2021/09/13 05:57 (external edit)