This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nrf51822 with Modbus(TTL to RS485)

I am trying to send and receive a response from a machine using  Modbus (TTL to Rs485) and nrf51822.

Connections: R0 -> RX(P0.11) ,D1->TX(p0.09), RE&DE->P0.19,VCC->5V 

Just the same as to connect with Arduino (Have implemented with Arduino mega)

I am using Keil for programming and trying to send and receive in hexadecimal format, but I am not sure if even command is transmitted and obviously I am not getting any response.

I am not sure about connections also. Attaching code.

Please let me know where I am wrong.

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"

//#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}
static uint8_t data_array[6];
void uart_event_handle(app_uart_evt_t * p_event)
{
   
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || (index > 4))
            {
               // err_code = ble_nus_string_send(&m_nus, data_array, index);
//                if (err_code != NRF_ERROR_INVALID_STATE)
//                {
//                    APP_ERROR_CHECK(err_code);
//                }

                index = 0;
							nrf_gpio_pin_set(21);
							nrf_delay_ms(100);
							nrf_gpio_pin_clear(21);
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

/**
 * @brief Function for main application entry.
 */
 uint8_t check_status[5] = {0x41,0x8E,0x7E,0x7F,0x6A};
int main(void)
{
    uint32_t err_code;

    bsp_board_leds_init();

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          APP_UART_FLOW_CONTROL_DISABLED,
          false,
          UART_BAUDRATE_BAUDRATE_Baud9600
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_event_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);

//#ifndef ENABLE_LOOPBACK_TEST
    printf("\r\nStart: \r\n");
			nrf_gpio_cfg_output(19);
			nrf_gpio_cfg_output(21);
			    

     while(true)
	   {

			 nrf_gpio_pin_set(19);
		   nrf_delay_ms(55);
			for(int i=0; i<5;i++)
			{
				app_uart_put(check_status[i]);
			}
			nrf_delay_ms(2);
			nrf_gpio_pin_clear(19);
			nrf_delay_ms(3000);
	   }
       
  }

Parents
  • Hi

    Can you try describing your issue in more detail? Please make sure that the TX and RX pins are connected correctly to both devices. If you're not sure about your connections, please share your hardware setup (schematics and a high-res picture of your setup) and I will take a look.

    I suggest you try to get it working with our UART example before making any changes to the code. Just to see that you're able to get it running.

    Best regards,

    Simon

Reply
  • Hi

    Can you try describing your issue in more detail? Please make sure that the TX and RX pins are connected correctly to both devices. If you're not sure about your connections, please share your hardware setup (schematics and a high-res picture of your setup) and I will take a look.

    I suggest you try to get it working with our UART example before making any changes to the code. Just to see that you're able to get it running.

    Best regards,

    Simon

Children
Related