——Na DWIN Developer Forum
I tenei putanga, ka whakamohio atu ki a koe te keehi puna tuwhera whakawhiwhi tohu o te Huinga Kaiwhakawhanake DWIN - te ruuma maara atamai. I whakatinanahia e nga miihini te mata atamai T5L ki te whakahaere i nga mahi whakawera me te mana pamahana ma te kawa Modbus. Ka taea hoki te whakatika i te mana hiko kia rite ki te mahi rama. Ka taea e te punaha te whakahaere aunoa i runga i nga tawhā kua whakatakotoria ki te mata me te penapena i nga rekoata hitori he.
1.UI Whakaatu Rawa
![asvdfb (2)](https://ecdn6.globalso.com/upload/p/1355/source/2024-03/65f96ba08612736786.jpg)
![asvdfb (1)](https://ecdn6.globalso.com/upload/p/1355/source/2024-03/65f96ba210c7924337.jpg)
2.UI Hoahoa
1.C51 Hoahoa
Ko nga waehere matua mo te whiwhi me te whakahou i nga raraunga penei i te pāmahana, te haumākū, me te teitei i runga i te atanga matua, me te whakamahi i te modbus rtu ki te whakahaere i nga waahanga whakahaere pāmahana, nga motuka, te kitenga whakaoho, me etahi atu miihini pononga e whai ake nei
Tohutoro waehere atanga matua:
#include "main_win.h"
#include "modbus.h"
#include "sys_params.h"
#include "func_handler.h"
#include "uart2.h"
#whakauru
#whakauru
#tautuhia TEMP_HUM_SLAVE_ADDR 2
#tautuhia TEMP_HUM_VAL_MAX_NUM 2
#define ALERT_BIT_MAX_NUM 30
#tautuhia ALERT_BYTE_NUM (ALERT_BIT_MAX_NUM/8+((ALERT_BIT_MAX_NUM%8)!=0))
#define GET_ALERT_BIT(val, pos) ((val[pos/8]>>(pos%8))&0x01)
momotuhi hanganga{
char date[17];
u8 desc;
}MATARA;
#define ALERT_TABLE_LEN 20
pateko u8 btn_sta[MAIN_WIN_BTN_MAX_NUM] = {0};
static u8 btn_addr[MAIN_WIN_BTN_MAX_NUM] = {50, 51, 52, 69, 53, 54, 55, 70, 56, 57, 58, 59};
u16 matua_win_val[MAIN_WIN_VAL_MAX_NUM];
u16 temp_hum_val[TEMP_HUM_VAL_MAX_NUM] = {0};
u16 date_val[MAIN_WIN_DATE_MAX_NUM] = {0};
u8 alert_val[ALERT_BYTE_NUM] = {0};
u8 old_alert_val[ALERT_BYTE_NUM] = {0};
ALERT alert_table[ALERT_TABLE_LEN];
u16 tau_matohi = 0;
bit is_main_win = 0;
whakakorea main_win_update()
{
}
whakakorea matua_win_disp_date()
{
u8 anake;
len = sprintf(common_buf, "%u:%u", (u16)date_val[3], (u16)date_val[4]);
common_buf[len+1] = 0;
sys_write_vp(MAIN_WIN_DATE_VP, common_buf, len/2+2);
}
whakakorea matua_win_process_alert()
{
u8 i;
mo(i=0;i
{
mena(GET_ALERT_BIT(old_alert_val, i))
haere tonu;
mena(GET_ALERT_BIT(alert_val, i))
{
ki te(tau_matohi>=ALERT_TABLE_LEN)
tau_matohi = ALERT_TABLE_LEN-1;
ripanga_matohi[tau_matohi].desc = i+1;
sprintf(ripanga_matohi[tau_matohi].te ra, "%u/%u/%u %u:%u",
date_val[0], date_val[1], date_val[2], date_val[3], date_val[4]
);
tau_ara++;
}
}
memcpy(old_alert_val, alert_val, sizeof(alert_val));
}
whakakorea matua_win_disp_alert()
{
u16 a;
me u16;
u16 anake = 0;
noa_buf[0] = 0;
mo(i=0;i
{
val = 0;
mehemea(i
{
val = tepu_mataara.desc;
len += sprintf(common_buf+len, "%s\r\n", alert_table.date);
}
sys_write_vp(ALERT_WIN_DESC_START_VP+i, (u8*)&val, 1);
}
common_buf[len+1] = 0;
sys_write_vp(ALERT_WIN_DATE_VP, common_buf, len/2+2);
}
whakakorea main_win_init()
{
mānu fixed_val;
u8 i;
is_main_win = 1;
main_win_val[5] = (u16)(temp_hum_val[0]/10.0+0.5f);
main_win_val[6] = (u16)(temp_hum_val[1]/10.0+0.5f);
mo(i=0;i
{
mehemea(i==0)
haere tonu;
sys_write_vp(MAIN_WIN_WIND_SPEED_VP+MAIN_WIN_VAL_OFFSET*i, (u8*)&main_win_val, 1);
}
fixed_val = matua_win_val[0]/WIND_SPEED_SCALE+FLOAT_FIX_VAL;
sys_write_vp(MAIN_WIN_WIND_SPEED_VP, (u8*)&fixed_val, 2);
}
whakakorea matua_win_click_handler(u16 btn_val)
{
taupū u8;
mena(btn_val==0x0B)
{
matua_win_disp_alert();
hoki mai;
}
taupū = btn_val-1;
btn_sta[taupū] = !btn_sta[taupū];
if((taupū==3)||(taupū==7))
btn_sta[taupū] = 1;
modbus_write_bit(btn_addr[index], btn_sta[index]?0xFF00:0x0000);
btn_val = btn_sta[taupū];
sys_write_vp(MAIN_WIN_BTN_STA_START_VP+MAIN_WIN_BTN_STA_OFFSET*taupū, (u8*)&btn_val, 1);
if(taupū==9)
is_main_win = 0;
atu mena((taupū==3)||(taupū==7))
{
while(sys_get_touch_sta());
modbus_write_bit(btn_addr[index], 0x0000);
}
}
whakakorea matua_win_msg_handler(u8 *msg,u16 msg_len)
{
u8 f_code = msg[MODBUS_RESPOND_POS_FUNC_CODE];
u8 data_len = msg[MODBUS_RESPOND_POS_DATA_LEN];
u8 i;
u8 whakawhäiti;
msg_len = msg_len;
ki te(!ko_mana_win)
hoki mai;
if((f_code==MODBUS_FUNC_CODE_03)&&(raunga_len==MAIN_WIN_VAL_MAX_NUM*2))
{
offset = MODBUS_RESPOND_POS_DATA;
mo(i=0;i
{
main_win_val = SYS_GET_U16(msg[offset], msg[offset+1]);
whakawehe += 2;
}
main_win_update();
} atu mena((f_code==MODBUS_FUNC_CODE_01)&&(data_len==ALERT_BYTE_NUM))
{
offset = MODBUS_RESPOND_POS_DATA;
mo(i=0;i
{
alert_val = msg[offset];
whakawhenumi++;
}
matua_win_process_alert();
} atu mena((f_code==MODBUS_FUNC_CODE_03)&&(raunga_len==TEMP_HUM_VAL_MAX_NUM*2))
{
offset = MODBUS_RESPOND_POS_DATA;
mo(i=0;i
{
temp_hum_val = SYS_GET_U16(msg[offset], msg[offset+1]);
whakawehe += 2;
modbus_write_word(5+i, temp_hum_val);
}
main_win_update();
} atu mena((f_code==MODBUS_FUNC_CODE_03)&&(raunga_raunga==MAIN_WIN_DATE_MAX_NUM*2))
{
offset = MODBUS_RESPOND_POS_DATA;
mo(i=0;i
{
date_val = SYS_GET_U16(msg[offset], msg[offset+1]);
whakawehe += 2;
}
matua_win_disp_date();
}
}
whakakorea main_win_read_temp_hum()
{
u8 old_slave_addr = SLAVE_ADDR;
sys_params.user_config[5] = TEMP_HUM_SLAVE_ADDR;
modbus_read_words(0, TEMP_HUM_VAL_MAX_NUM);
sys_params.user_config[5] = old_slave_addr;//Whakahokia
}
whakakorea matua_win_handler()
{
haki u8 pateko = 0;
mena(ko_mana_win)
{
if(ara_panui_wa==ALERT_READ_PERIOD)
{
wa_panui_mataara = 0;
modbus_read_bits(510, ALERT_BIT_MAX_NUM);
hoki mai;
}
ki te(rā_whakahou_wa==DATE_UPDATE_PERIOD)
{
rā_whakahou_wa = 0;
modbus_read_words(180, MAIN_WIN_DATE_MAX_NUM);
hoki mai;
}
kara = !kara;
mena(kara)
modbus_read_words(0, MAIN_WIN_VAL_MAX_NUM);
atu
matua_win_read_temp_hum();
}
}
tohutoro waehere modbus rtu:
#include "modbus.h"
#include "crc16.h"
#include "sys_params.h"
#define UART_INCLUDE "uart2.h"
#define UART_INIT uart2_init
#define UART_SEND_BYTES uart2_send_bytes
#define UART_BAUD 9600
#tautuhia MODBUS_RECV_TIMEOUT (u8)(35000.0f/UART_BAUD+2)
#tautuhia MODBUS_SEND_INTERVAL 150
#include UART_INCLUDE
moka pateko is_modbus_recv_complete = 0;
pateko u8 modbus_recv_buff[270];
static u16 modbus_recv_len = 0;//Tapeke roa o nga paita kua whakaaetia
pateko u8 modbus_recv_timeout = 0;//Whakaaetia te wa waipuke
pateko u16 modbus_send_interval = 0;
MODBUS_PACKET pākete;
kore te modbus_init()
{
UART_INIT(UART_BAUD);
}
whakakorea modbus_send_bytes(u8 *paita,u16 len)
{
UART_SEND_BYTES(paita,len);
}
whakakorehia te modbus_recv_byte(u8 paita)
{
mena(ko_modbus_recv_complete)
hoki mai;
mena(modbus_recv_len
modbus_recv_buff[modbus_recv_len++] = paita;
}
whakakorehia te modbus_check_recv_timeout()
{
mena(modbus_recv_timeout)
{
modbus_recv_timeout--;
mena(modbus_recv_timeout==0)
{
is_modbus_recv_complete = 1;
}
}
}
u8 modbus_send_packet(u8 *pakete)
{
u16 anake;
u16 crc;
u8 func_code = pākete[1];
while(modbus_send_interval);
if(func_code==MODBUS_FUNC_CODE_10)
{
((MODBUS_10_PACKET*)packet)->byte_num = ((MODBUS_10_PACKET*)pakete)->word_num*2;
len = 9+((MODBUS_10_PACKET*)pakete)->byte_num;
} atu mena(func_code==MODBUS_FUNC_CODE_0F)
{
len = ((MODBUS_0F_PACKET*)pakete)->bit_num;
((MODBUS_0F_PACKET*)pakete)->byte_num = len/8+(len%8?1:0);
len = 9+((MODBUS_0F_PACKET*)pakete)->paita_num;
} atu
{
len = rahinga(MODBUS_PACKET);
}
crc = crc16(pakete,len-2);
pākete[len-2] = (u8)(crc>>8);
pākete[len-1] = (u8)crc;
modbus_send_bytes(pakete,len);
modbus_send_interval = MODBUS_SEND_INTERVAL;
hoki 0;//Angitu
}
waho kore modbus_msg_handler(u8 *msg,u16 msg_len);
kore te modbus_handler()
{
u16 crc;
ki te(! is_modbus_recv_complete)
hoki mai;
//Tirohia te uara crc
crc = ((u16)modbus_recv_buff[modbus_recv_len-2]
if(crc16(modbus_recv_buff,modbus_recv_len-2)==crc)
{
modbus_msg_handler(modbus_recv_buff,modbus_recv_len);
}
modbus_recv_len = 0;
is_modbus_recv_complete = 0;
}
u8 modbus_send_fcode(u8 fcode, u16 addr, u16 len)
{
packet.slave_addr = SLAVE_ADDR;
packet.func_code = fcode;//Waehere mahi
packet.start_addr = addr;//Wāhitau
packet.data_len = len;//Uara kua tuhia
len = modbus_send_packet((u8*)&packet);
hoki mai len;
}
Te wa tuku: Hanuere-12-2024