Sunday, June 28, 2015

DGH Modules Communicate with Arduino Mega2560

An sample program and Communications Library is available from DGH Corporation for use with the Arduino Mega2560.  The sample program illustrates how to setup the Mega2560 serial communications port, build a command message for the DGH module, transmit the command and receive the response message.  The program transmits and receives data through the Mega2560 Serial1 port using pins #18 and #19.  The Mega2560 serial pins are TTL level and therefore will required a serial communications interface driver chip in order to connect to the DGH module.  Select a communications driver chip that will establish the proper signal levels for either RS-232 or the RS-485 standard.

The sample source code is heavily commented and easy to follow. It utilizes a DGHClass Communications library that includes individual functions for sending analog and digital I/O commands such as Read Data (RD), Analog Output (AO), Digital Input (DI), Digital Output (DO) and Read Events (RE).  The DGHClass Library is easy to follow and may be expanded to include additional module commands.

The DGHClass Library also includes a non-blocking serial processor function that transmits commands and receives response characters.  The non-blocking serial processor uses a state machine to control the serial I/O process. The state machine has four seperate states: DoNothing, Transmit, Receive and MessageAvailable.  The Transmit State sends a command to the module and then returns back to the main loop.  The Receive State will check for available characters, store them in a response buffer and detect the end of a message.  The Message Available state indicates that a valid message has been received. This state is also monitored in the program main loop() and data can be parsed when detected.

Simply set the baud rate in Setup() and place the three function calls in the main loop() of the Arduino Mega2560 program for the fastest command/response processing. See the abbreviated sample code below. 

#include "dgh.h"

// Define DGH Class

// Program Setup Function
void setup() {
  // Initialize Serial1 on Mega2560 to 19,200

// Main Loop
void loop() {
  // **************************************
  // STEP 1 :  Trigger Any Command
  // **************************************  
  if (DGH.getCommState() == DGH_IOSTATE_NONE)
    // Transmit A Command
  // **************************************
  // STEP 2 :  ALWAYS CALL the Non-Blocking Communications Processor
  // **************************************  
  // ************************************** 
  // STEP 3 :  Check for Response Message
  // **************************************  
  if (DGH.msgAvailable()) {
    // Process Response
    if (strlen(DGH._rxbuf) > 0) {
      // Have Valid Response
      // Parse the Data Value
    // Reset the Status Bit - Ready to Send Next Command

To download the fully commented version of the sample program and the DGHClass Library then click on the following link :

In conclusion, the sample program and library allows an Arduino Mega2560 to communicate with a DGH Module using the DGH ASCII protocol.  The library performs all the important analog and digital I/O functions for controlling DGH modules.  The sample program can be easily expanded to develop a more complicated application and the DGHClass library can be easily integrated into an existing application.

TESTING NOTE: When using a brand new module from DGH, set the baud rate in the example Setup() to 300, complile the code and start communicating with the module without performing any module setup changes.  Higher baud rates will be more beneficial in the final application.

No comments:

Post a Comment