Browse Source

modbus协议初始化

master
zwsd 3 years ago
parent
commit
439e9dc36b
  1. 5
      .vscode/c_cpp_properties.json
  2. 11
      .vscode/settings.json
  3. 23
      app/MDK-ARM/app.uvguix.zwsd_cad3
  4. 9
      app/MDK-ARM/app.uvprojx
  5. 181
      src/user_main.c

5
.vscode/c_cpp_properties.json

@ -13,8 +13,9 @@
"${workspaceFolder}/app/MDK-ARM", "${workspaceFolder}/app/MDK-ARM",
"${workspaceFolder}/app/Src", "${workspaceFolder}/app/Src",
"${workspaceFolder}/app/Drivers/STM32F1xx_HAL_Driver/Src", "${workspaceFolder}/app/Drivers/STM32F1xx_HAL_Driver/Src",
"${workspaceFolder}/zmodbus",
"${workspaceFolder}/zstm32uart_irq_rx_service"
"${workspaceFolder}/copyZ_zmodbus",
"${workspaceFolder}/copyZ_zstm32uart_irq_rx_service",
"${workspaceFolder}/src"
], ],
"defines": [ "defines": [
"USE_HAL_DRIVER", "USE_HAL_DRIVER",

11
.vscode/settings.json

@ -1,7 +1,12 @@
{ {
"files.associations": { "files.associations": {
"main.h": "c",
"new": "c",
"zstm32uart_irq_rx_service.h": "c"
"modbus_processer.h": "c",
"zmodbus_slave.h": "c",
"zport.h": "c",
"gpio.h": "c",
"usart.h": "c",
"zstm32uart_irq_rx_service.h": "c",
"zmodbus_common.h": "c",
"stdlib.h": "c"
} }
} }

23
app/MDK-ARM/app.uvguix.zwsd_cad3
File diff suppressed because it is too large
View File

9
app/MDK-ARM/app.uvprojx

@ -575,4 +575,13 @@
<files/> <files/>
</RTE> </RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>app</LayName>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project> </Project>

181
src/user_main.c

@ -7,22 +7,182 @@
#include "zboard.h" #include "zboard.h"
#include "zport.h" #include "zport.h"
// //
#include "../copyZ_zstm32uart_irq_rx_service/zstm32uart_irq_rx_service.h"
#include "gpio.h" #include "gpio.h"
#include "iwdg.h" #include "iwdg.h"
#include "main.h" #include "main.h"
#include "modbus_processer.h"
#include "tim.h" #include "tim.h"
#include "usart.h" #include "usart.h"
#include "../copyZ_zstm32uart_irq_rx_service/zstm32uart_irq_rx_service.h"
#include "modbus_processer.h"
#include "zmodbus_slave.h" #include "zmodbus_slave.h"
static uint8_t modbus_rx_buf[kmodbus_processer_rx_buf_size];
static uint8_t modbus_tx_buf[kmodbus_processer_tx_buf_size];
//
uint16_t g_auto_trigger_shut_when_flash = 1;
// state
uint32_t g_light_flicker_trigger_ticket;
bool g_flicker_on = false;
/***********************************************************************************************************************
* ========================================================GLOBAL======================================================== *
***********************************************************************************************************************/
typedef enum {
klight_control_mode_always_close = 0,
klight_control_mode_always_open,
klight_control_mode_flicker,
klight_control_mode_control_direct,
klight_control_mode_max,
} light_control_mode_t;
// config
uint16_t g_light_brightness = 0;
uint16_t g_light_control_mode = klight_control_mode_always_close;
uint16_t g_light_flicker_duration = 0;
void update_light_state() {
if (g_light_control_mode == klight_control_mode_always_close) {
port_set_led_brightness(0);
} else if (g_light_control_mode == klight_control_mode_always_open) {
port_set_led_brightness(g_light_brightness);
}
}
void trigger_one_time_flicker(uint32_t brightness, uint16_t duration_ms) {
//
g_light_flicker_trigger_ticket = HAL_GetTick();
g_light_flicker_duration = duration_ms;
g_flicker_on = true;
port_set_led_brightness(brightness);
if (g_auto_trigger_shut_when_flash > 0) {
if (port_extra_gpio_get_mode(0) == kegm_camera_trigger_signal) {
port_extra_gpio_set_level(0, true);
}
if (port_extra_gpio_get_mode(1) == kegm_camera_trigger_signal) {
port_extra_gpio_set_level(1, true);
}
}
}
void order_uart485_tx(uint8_t* rx, uint16_t len) { HAL_UART_Transmit(&ORDER_485_UART, rx, len, 0xffffffff); }
/***********************************************************************************************************************
* =======================================================UART======================================================== *
***********************************************************************************************************************/
static zstm32uart_t s_uarts[] = { static zstm32uart_t s_uarts[] = {
{ {
.huart = &ORDER_485_UART, .huart = &ORDER_485_UART,
} // } //
}; };
void modbux_process_rx(modbus_processer_context_t* context) {
// 10 and 03
// 0->15 |
// 0(16Byte) | (0->65536)
// 1(16Byte) | (0->100)
// 2(16Byte) | mode(0:1:2:)
// 3-4(32Byte) | reg3亮度(0->65535)reg4持续时间ms
// 7(16Byte) | GPIO, (0:,1:)
// ------------|-------------------------------
// 16->31 | GPIO1控制
// 16(16Byte) | GPIO功能(0:,1:)
// 17(16Byte) |
// ------------|-------------------------------
// 32->47 | GPIO2控制
// 32(16Byte) | GPIO功能(0:,1:)
// 33(16Byte) |
ModbusFunctionCode fcode = modbus_get_function_code(context);
if (fcode == ModbusOrder10) {
//
// REG0
if (modbus_if_register_exists_10(context, 0)) {
uint16_t brightness = modbus_get_reg_10(context, 0);
g_light_brightness = brightness;
LOGD("set light_brightness %d", brightness);
update_light_state();
}
// REG1
if (modbus_if_register_exists_10(context, 1)) {
uint16_t brightness_100 = modbus_get_reg_10(context, 1);
brightness_100 = brightness_100 > 100 ? 100 : brightness_100;
uint16_t brightness = 65535 * (brightness_100 / 100.0);
g_light_brightness = brightness;
LOGD("set light_brightness %d%%(%d)", brightness_100, brightness);
update_light_state();
}
// REG2
if (modbus_if_register_exists_10(context, 2)) {
if (g_light_control_mode < klight_control_mode_max) {
g_light_control_mode = modbus_get_reg_10(context, 2);
LOGD("set light_control_mode %d", g_light_control_mode);
update_light_state();
} else {
LOGE("set light_control_mode fail,parameter error");
}
}
// REG3,4
if (modbus_if_register_exists_10(context, 3) && modbus_if_register_exists_03(context, 4)) {
//
if (g_light_control_mode == klight_control_mode_flicker) {
uint32_t brightness = modbus_get_reg_10(context, 3);
uint16_t duration_ms = modbus_get_reg_10(context, 4);
LOGD("trigger flicker %d %dms", brightness, duration_ms);
trigger_one_time_flicker(brightness, duration_ms);
} else {
LOGE("trigger flicker fail,mode != klight_control_mode_flicker");
}
}
// REG5
if (modbus_if_register_exists_10(context, 5)) {
uint16_t setvalue = modbus_get_reg_10(context, 5);
LOGD("port_set_led_brightness1 %d%%", setvalue);
port_set_led_brightness1(65535 * (setvalue / 100.0));
}
// REG6
if (modbus_if_register_exists_10(context, 6)) {
uint16_t setvalue = modbus_get_reg_10(context, 6);
LOGD("port_set_led_brightness2 %d%%", setvalue);
port_set_led_brightness2(65535 * (setvalue / 100.0));
}
modbus_send_10(context, Modbus_OK);
} else if (fcode == ModbusOrder03) {
//
if (modbus_if_register_exists_03(context, 0)) {
modbus_set_tx_reg_03(context, 0, g_light_brightness);
}
if (modbus_if_register_exists_03(context, 1)) {
modbus_set_tx_reg_03(context, 1, g_light_brightness / 65535.0 * 100);
}
if (modbus_if_register_exists_03(context, 2)) {
modbus_set_tx_reg_03(context, 2, g_light_control_mode);
}
modbus_send_03(context, Modbus_OK);
}
}
modbus_processer_t s_modbus_processer = {
.modbus_device_id = MODBUS_DEVICE_ID,
.modbus_processer_rx_buf = modbus_rx_buf,
.modbus_processer_rx_buf_size = sizeof(modbus_rx_buf),
.modbus_processer_tx_buf = modbus_tx_buf,
.modbus_processer_tx_buf_size = sizeof(modbus_tx_buf),
/*************************************/
.modbus_baundrate_one_packet_delay_us = kmodbus_baundrate_one_packet_delay_us,
.port_enter_critical = port_enter_critical,
.port_exit_critical = port_exit_critical,
.port_delay_us = port_delay_us,
.tx = order_uart485_tx,
/*************************************/
.process_rx = modbux_process_rx,
};
/** /**
* @brief * @brief
*/ */
@ -31,15 +191,22 @@ void port_do_debug_light_state() {
if (port_haspassedms(lastprocess) > 300) { if (port_haspassedms(lastprocess) > 300) {
lastprocess = HAL_GetTick(); lastprocess = HAL_GetTick();
HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN); HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN);
// static bool state = false;
// state = !state;
// port_extra_gpio_set_level(0, state);
// port_extra_gpio_set_level(1, state);
}
}
/**
* @brief
*/
void HOOK_ZUART_RxCpltCallback(UART_HandleTypeDef* huart, uint8_t rxdata) {
if (huart == &ORDER_485_UART) {
//485
modbus_processer_push_data(rxdata);
} }
} }
void user_main() { void user_main() {
uart_service_start_all_uart_rx(s_uarts, ARRARY_SIZE(s_uarts)); uart_service_start_all_uart_rx(s_uarts, ARRARY_SIZE(s_uarts));
modbus_processer_init(&s_modbus_processer);
while (true) { while (true) {
port_do_debug_light_state(); port_do_debug_light_state();

Loading…
Cancel
Save