📄 modbus.cpp
字号:
#include "modbus.h"
//#include "extern.h"
#define READ_DI 0x02
#define READWORD 0x03
#define READ_AI 0x04
#define SET_COIL 0x05
void CModbus::send_yxyc()
{
INT16U temp;
askbuf[0] = station_add; //modbusaddr;
askbuf[1] = READWORD;
askbuf[2] = 0x00; // data address
askbuf[3] = 0x63; // data address
askbuf[4] = 0x00; // high byte
askbuf[5] = 0x10; // read word number
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = READWORD;
rece_end = 2;
//send_add = station_add;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}
void CModbus::initial(INT8U portno)
{
int i;
for(i = 0; i<=255; i++){
mauchhi[i] = auchhi[i];
mauchlo[i] = auchlo[i];
}
myportno = portno;
start_add = port_set[myportno].start_addr;
end_add = port_set[myportno].end_addr;
send_cycle = 1;
for (i = start_add; i <= end_add; i++) {
LINK_flag[i] = 0;
fail_count[i] = 0;
send_state[i] = 0;
}
station_add = start_add;
}
INT16U CModbus::crc16(INT8U *msg, INT8U len)
{
INT8U uindex;
INT8U uchhi=0xff;
INT8U uchlo=0xff;
while (len--) {
uindex = uchhi ^ *msg++;
uchhi = uchlo ^ mauchhi[uindex];
uchlo = mauchlo[uindex];
}
return (uchhi << 8 | uchlo);
}
void CModbus::processREADWORD(INT8U *buf)
{
int i;
INT16U temp, yx_no;
sclock sclk;
soe_record_t temp_soe;
yxbw_record_t temp_yx_bw;
for (i = 0; i < _TotalYx; i++) {
if( ((myportno == (yx_define[i].portno - 1)) &&
(yx_define[i].devno == buf[0]) && (yx_define[i].info < 16))) {
temp = ((buf[4] << 8) & 0xff00) + buf[3];
temp = (temp >> yx_define[i].info) & 0x01;
yx_no = i & 0xfff;
if (get_yx_dot(yx_no) ^ temp) {
yx_update_db(i, temp);
temp_soe.status_switch_no = yx_no & 0xfff;
if (temp)
temp_soe.status_switch_no |= 0x8000;
else temp_soe.status_switch_no &= 0x7fff;
GetClock(&sclk);
temp_soe.day = sclk.day;
temp_soe.hour = sclk.hour;
temp_soe.minute = sclk.minute;
temp_soe.second = sclk.second;
temp_soe.msecond = sclk.msecond;
insert_soe(&temp_soe);
temp_yx_bw.func_code = yx_no / 32;
temp_yx_bw.yx_code[0] = dbdata.Yx[yx_no / 32 * 2];
temp_yx_bw.yx_code[1] = dbdata.Yx[yx_no / 32 * 2 + 1];
insert_yx_bw(&temp_yx_bw);
}
}
}
for (i = 0; i < _TotalYc; i++) {
if( ((myportno == (yc_define[i].portno - 1)) &&
(yc_define[i].devno == buf[0]) && (yc_define[i].info < 10))) {
temp = buf[yc_define[i].info * 2 + 3];//yong yu ding wei
temp = ((temp << 8) & 0xff00) + buf[yc_define[i].info * 2 + 4];
yc_update_db(i, temp / 10);//10kv;/1000
}
}
}
void CModbus::processREAD_DI(INT8U *buf)
{
int i;
INT32U temp;
INT16U yx_no,temp1,temp2;
yxbw_record_t temp_yx_bw;
for (i = 0; i < _TotalYx; i++) {
if( ((myportno == (yx_define[i].portno - 1)) &&
(yx_define[i].devno == buf[0]) && (yx_define[i].info < 14))) {
temp1 = (((INT16U)buf[4] << 8) & 0xff00) + buf[3];
/* temp2 = (((INT16U)buf[6] << 8) & 0xff00) + buf[5];
temp = (((INT32U)temp2 << 16) & 0xffff0000) + temp1;*/
temp1 = (temp1 >> yx_define[i].info) & 0x01;
yx_no = i & 0xfff;
if (get_yx_dot(yx_no) ^ temp1) {
yx_update_db(i, temp1);
temp_yx_bw.func_code = yx_no / 32;
temp_yx_bw.yx_code[0] = dbdata.Yx[yx_no / 32 * 2];
temp_yx_bw.yx_code[1] = dbdata.Yx[yx_no / 32 * 2 + 1];
insert_yx_bw(&temp_yx_bw);
}
}
}
}
void CModbus::processREAD_AI(INT8U *buf)
{
int i;
INT32U temp;
INT16U temp1,temp2, yx_no,value;
yxbw_record_t temp_yx_bw;
if (buf[2] == 0x24)
{
for (i = 0; i < _TotalYc; i++) {
if( ((myportno == (yc_define[i].portno - 1)) &&
(yc_define[i].devno == buf[0]) && (yc_define[i].info <= 8))) {
temp1 =recbuf[yc_define[i].info * 4 + 3];
temp1 = ((temp1 << 8) & 0xff00) +recbuf[yc_define[i].info * 4 + 4];
temp2 =recbuf[yc_define[i].info * 4 + 5];
temp2 = ((temp2 << 8) & 0xff00) +recbuf[yc_define[i].info * 4 + 6];
temp = ((INT32U(temp1) << 16) & 0xffff0000) + temp2;
//value = int(temp/1000);
yc_update_db(i, temp/1000);
}
}
}
else if (buf[2] == 0x14)
{
for (i = 0; i < _TotalYc; i++)
{
if( ((myportno == (yc_define[i].portno - 1)) &&
(yc_define[i].devno == buf[0]) &&
(9 <= yc_define[i].info <=15)))
{
temp1 = buf[(yc_define[i].info-9) * 4 + 3];
temp1 = ((temp1 << 8) & 0xff00) + buf[(yc_define[i].info-9) * 4 + 4];
temp2 = buf[(yc_define[i].info-9) * 4 + 5];
temp2 = ((temp2 << 8) & 0xff00) + buf[(yc_define[i].info-9) * 4 + 6];
temp = ((temp1 << 16) & 0xffff0000) + temp2;
yc_update_db(i, INT16U(temp / 1000));
}
}
}
else if (buf[2] == 0x20){
for(i = 0; i<_TotalYx; i++){
if(((myportno == (yx_define[i].portno - 1))&&
(yx_define[i].devno == buf[0])&&(yx_define[i].info>19)&&
(yx_define[i].info<35)))
{
int flag = 2*(yx_define[i].info-20)+3;
if((recbuf[flag]==0x00)&&(recbuf[flag+1]==0x01))
temp1 = 0x0001;
else if((recbuf[flag]==0x00)&&(recbuf[flag+1]==0x00))
temp1 = 0x0000;
/* temp2 = (((INT16U)buf[6] << 8) & 0xff00) + buf[5];
temp = (((INT32U)temp2 << 16) & 0xffff0000) + temp1;*/
yx_no = i & 0xfff;
if (get_yx_dot(yx_no) ^ temp1) {
yx_update_db(i, temp1);
temp_yx_bw.func_code = yx_no / 32;
temp_yx_bw.yx_code[0] = dbdata.Yx[yx_no / 32 * 2];
temp_yx_bw.yx_code[1] = dbdata.Yx[yx_no / 32 * 2 + 1];
insert_yx_bw(&temp_yx_bw);
}
}
}
}
}
void CModbus::processSET_COIL(INT8U *buf)
{
if (myportno == port_yk_info.yk_des && buf[0] == port_yk_info.dev_no){
if (
(buf[1] == 0x05) && (recbuf[2] == 0x00) && (recbuf[4] == 0xff) && (recbuf[5] == 0x00) &&
( ((recbuf[3] == 0x10) && (port_yk_info.yk_cmd == YKCCLOSE))
||((recbuf[3] == 0x0f) && (port_yk_info.yk_cmd == YKCTRIP)) )
){
port_yk_info.Y_CHECK = TRUE;
port_yk_info.check_result = YKROK;
}
else {
port_yk_info.Y_CHECK = TRUE;
port_yk_info.check_result = FALSE;
}
}
}
void CModbus::change_addr()
{
station_add = station_add+1;
if (station_add > end_add)
{
station_add = start_add;
send_cycle++;
}
}
void CModbus::send_yx()
{
INT16U temp;
askbuf[0] = station_add;
askbuf[1] = READ_DI;
askbuf[2] = 0x00;
askbuf[3] = 0x00;
askbuf[4] = 0x00;
askbuf[5] = 0x0e; //14
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = READ_DI;
rece_end = 2;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, SOE_COLOR);
}
void CModbus::send_yc()
{
INT16U temp;
askbuf[0] = station_add;
askbuf[1] = READ_AI;
askbuf[2] = 0x00;
askbuf[3] = 0x00;
askbuf[4] = 0x00;
askbuf[5] = 0x12;//0x0a
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = READ_AI;
rece_end = 2;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, YC_COLOR);
}
void CModbus::send_ymyc()
{
INT16U temp;
askbuf[0] = station_add;
askbuf[1] = READ_AI;
askbuf[2] = 0x00;
askbuf[3] = 0x16;
askbuf[4] = 0x00;
askbuf[5] = 0x0e;
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = READ_AI;
rece_end = 2;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, YM_COLOR);
}
void CModbus::send_leds()
{
INT16U temp;
askbuf[0] = station_add;
askbuf[1] = READ_AI;
askbuf[2] = 0x00;
askbuf[3] = 0x28;
askbuf[4] = 0x00;
askbuf[5] = 0x10;
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = READ_AI;
rece_end = 2;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, YK_COLOR);
}
void CModbus::send_yk_preset()
{
INT16U temp;
askbuf[0] = port_yk_info.dev_no;
askbuf[1] = SET_COIL;
askbuf[2] = 0x00;
if (port_yk_info.yk_cmd == YKCTRIP) {
askbuf[3] = 0x0f;
}
else if (port_yk_info.yk_cmd == YKCCLOSE){
askbuf[3] = 0x10;
}
askbuf[4] = 0xff;
askbuf[5] = 0x00;
temp = crc16(askbuf, 6);
askbuf[6] = (temp >> 8) & 0xff;
askbuf[7] = temp & 0xff;
currentCMD = SET_COIL;
rece_end = 2;
com_transfer(myportno, 8, askbuf);
draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}
void CModbus::send_yk_exec()
{
INT16U temp;
askbuf[0] = port_yk_info.dev_no;
askbuf[1] = SET_COIL;
askbuf[2] = 0x00;
if (port_yk_info.yk_cmd == YKCTRIP) {
askbuf[3] = 0x0d;
}
else if (port_yk_info.yk_cmd == YKCCLOSE) {
askbuf[3] = 0x0c;
}
else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -