Abd Alrhman Hammal
Published © GPL3+

Hexabitz Commnication Modules

Hexabitz H1AR2 Mini USB-B-to-UART with 2x H1DR0 RS232 Serial Transceiver to controlling Delta plc SE series.

AdvancedProtip2 hours228
Hexabitz Commnication Modules

Things used in this project

Hardware components

Mini USB-B to UART Converter Module (H1AR20)
Hexabitz Mini USB-B to UART Converter Module (H1AR20)
×1
Hexabitz RS232 converter (H1DR0)
×2
DELTA PLC SE
×1
LED (generic)
LED (generic)
×4
Resistor 10k ohm
Resistor 10k ohm
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

WPLSoft Delta software

Story

Read more

Code

main programe.

C/C++
follow Modicon Modbus/ASCII instruction
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	
/*
		MODIFIED by Hexabitz for BitzOS (BOS) V0.2.3 - Copyright (C) 2017-2019 Hexabitz
    All rights reserved
*/
///////////p6 usart1 --->>> rs 232

/* Includes ------------------------------------------------------------------*/
#include "BOS.h"


/* Private variables ---------------------------------------------------------*/
uint8_t y=0;
bool y0=0,y1=0,y2=0,y3=0;
uint8_t modbus_mes[17]={0};
/* Private function prototypes -----------------------------------------------*/
uint8_t HEX_2_ASCII(uint8_t dec);
uint8_t ASCII_2_hex(uint8_t ascii);
void MODBUS_ASCII(uint8_t address_slave, uint8_t functio_code, uint8_t start_reg_h,uint8_t start_reg_l, uint8_t number_of_reg_h,uint8_t number_of_reg_l);


/* Main functions ------------------------------------------------------------*/

int main(void)
{


  /* MCU Configuration----------------------------------------------------------*/

  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
   HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all user peripherals */

	/* Initialize BitzOS */
	BOS_Init();

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {


  }


}

/*-----------------------------------------------------------*/

/* User Task */
void UserTask(void * argument)
{
	//AddPortButton(MOMENTARY_NO,P1);
	AddPortButton(MOMENTARY_NO,P4);
	AddPortButton(MOMENTARY_NO,P2);
	AddPortButton(MOMENTARY_NO,P3);
	AddPortButton(MOMENTARY_NO,P5);
	//AddPortButton(MOMENTARY_NO,P6);
	//////////////////////////////////////////////////on module
	SetButtonEvents(P2, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p2->p3
	SetButtonEvents(P4, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p4->p1
	SetButtonEvents(P3, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p3->p4
	SetButtonEvents(P5, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p5->p5
	//SetButtonEvents(P1, 1, 0, 0, 0, 0, 0, 0, 0, 0);///p1->p2
	//SetButtonEvents(P6, 1, 0, 0, 0, 0, 0, 0, 0, 0);///p6->p6
  /* Infinite loop */
  for(;;)
  {
		
	}
}
void buttonClickedCallback(uint8_t port)
{	
	if(port == P4)
	{
		y0=1;
		y1=0;
		y2=0;
		y3=0;
	}
	else if(port == P2)
	{
		y0=0;
		y1=1;
		y2=0;
		y3=0;
	}
	else if(port == P3)
	{
		y0=0;
		y1=0;
		y2=1;
		y3=0;
	}
	else if(port == P5)
	{
		y0=0;
		y1=0;
		y2=0;
		y3=1;


	}
	y=y3<<3 | y2<<2 |y1<<1 | y0;
	
	MODBUS_ASCII(0x02, 0x06, 0x10, 0x04, 0x00, y); //writing regester D4 for output  ->y0 >> ON
	
}


void buttonDblClickedCallback(uint8_t port)
{	
		y0=0;
		y1=0;
		y2=0;
		y3=0;
		y=y3<<3 | y2<<2 |y1<<1 | y0;
	
  	MODBUS_ASCII(0x02, 0x06, 0x10, 0x04, 0x00, y); //writing regester D4 for output  ->y0 >> ON
	
}
void MODBUS_ASCII(uint8_t address_slave, uint8_t functio_code, uint8_t start_reg_h,uint8_t start_reg_l, uint8_t number_of_reg_h,uint8_t number_of_reg_l)
{
	
	
	
  modbus_mes[0]=0x3A; /// :
	
	if(address_slave<=0x0F)
	{
		modbus_mes[1]=HEX_2_ASCII(0x00);
		modbus_mes[2]=HEX_2_ASCII(address_slave);
	}
	else
	{
		modbus_mes[1]=HEX_2_ASCII(address_slave/0x10);
		modbus_mes[2]=HEX_2_ASCII(address_slave%0x10);
	}
	
	modbus_mes[3]=HEX_2_ASCII(0x00);
	
	modbus_mes[4]=HEX_2_ASCII(functio_code);
	
	modbus_mes[5]=HEX_2_ASCII(start_reg_h/0x10);
  modbus_mes[6]=HEX_2_ASCII(start_reg_h%0x10);
	
	modbus_mes[7]=HEX_2_ASCII(start_reg_l/0x10);
  modbus_mes[8]=HEX_2_ASCII(start_reg_l%0x10);
  
	
	modbus_mes[9] =HEX_2_ASCII(number_of_reg_h/0x10);
  modbus_mes[10]=HEX_2_ASCII(number_of_reg_h%0x10);
	
	modbus_mes[11]=HEX_2_ASCII(number_of_reg_l/0x10);
  modbus_mes[12]=HEX_2_ASCII(number_of_reg_l%0x10);	
	
uint8_t nLRC = 0 ; // LRC char initialized

nLRC =address_slave+functio_code+start_reg_h+start_reg_l+number_of_reg_h+number_of_reg_l;
nLRC=0xFF-nLRC;
nLRC++;
	
	modbus_mes[13]=HEX_2_ASCII(nLRC/0x10);
	modbus_mes[14]=HEX_2_ASCII(nLRC%0x10);
	
	modbus_mes[15]=0x0D;
  modbus_mes[16]=0x0A;

	HAL_UART_Transmit(&huart1,modbus_mes,17,100);
	
}

uint8_t HEX_2_ASCII(uint8_t hex)
{
	uint8_t result;
	switch(hex)
	{
		case 0x00:
		result=0x30;	
		break;
		case 1:
		result=0x31;	
		break;
		case 2:
		result=0x32;	
		break;
		case 3:
		result=0x33;	
		break;
		case 4:
		result=0x34;	
		break;
		case 5:
		result=0x35;	
		break;
		case 6:
		result=0x36;	
		break;
		case 7:
		result=0x37;	
		break;
		case 8:
		result=0x38;	
		break;
		case 9:
		result=0x39;	
		break;
		
		case 0x0A:
		result=0x41;	
		break;
		case 0x0B:
		result=0x42;	
		break;
		case 0x0C:
		result=0x43;	
		break;
		case 0x0D:
		result=0x44;	
		break;
		case 0x0E:
		result=0x45;	
		break;
		case 0x0F:
		result=0x46;	
		break;		
			
		
	}
return result;
}

uint8_t ASCII_2_hex(uint8_t ascii)
{
	uint8_t result;
	switch(ascii)
	{
		case 0x30:
		result=0x00;	
		break;
		case 0x31:
		result=0x01;	
		break;
		case 0x32:
		result=0x02;	
		break;
		case 0x33:
		result=0x03;	
		break;
		case 0x34:
		result=0x04;	
		break;
		case 0x35:
		result=0x05;	
		break;
		case 0x36:
		result=0x06;	
		break;
		case 0x37:
		result=0x07;	
		break;
		case 0x38:
		result=0x08;	
		break;
		case 0x39:
		result=0x09;	
		break;
		
		case 0x41:
		result=0x0A;	
		break;
		case 0x42:
		result=0x0B;	
		break;
		case 0x43:
		result=0x0C;	
		break;
		case 0x44:
		result=0x0D;	
		break;
		case 0x45:
		result=0x0E;	
		break;
		case 0x46:
		result=0x0F;	
		break;		
			
		
	}
return result;
}



/*-----------------------------------------------------------*/

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

Credits

Abd Alrhman Hammal
5 projects • 16 followers
firmware programmer

Comments