浏览代码

修改读取线圈功能bug,数据位移多了一次

master
zcn1123 4 年前
父节点
当前提交
f18fa12953
共有 2 个文件被更改,包括 114 次插入20 次删除
  1. +64
    -0
      Modbus_communication/Modbus_RTU_Salve/Modbus_TCP_log.txt
  2. +50
    -20
      Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp

+ 64
- 0
Modbus_communication/Modbus_RTU_Salve/Modbus_TCP_log.txt 查看文件

@@ -274,3 +274,67 @@
2020-9-7 13:7:4 Send:09 8F 04 C4 31
2020-9-7 13:7:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C
2020-9-7 13:7:4 Send:09 8F 04 C4 31
2020-9-10 13:54:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:54:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:54:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:54:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:54:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:54:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:54:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:54:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:54:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:54:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:14 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:14 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:14 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:14 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:14 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:14 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:15 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:15 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:15 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:15 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:15 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:15 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:15 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:15 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:16 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:16 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:17 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:17 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:18 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:18 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:18 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:18 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:18 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:18 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:18 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:18 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:18 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:18 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:19 Send:09 01 03 FF FF 03 0C C7
2020-9-10 13:59:19 Recv:09 01 00 00 00 12 BD 4F
2020-9-10 13:59:19 Send:09 01 03 FF FF 03 0C C7

+ 50
- 20
Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp 查看文件

@@ -33,10 +33,11 @@ UINT8 Bitset_to_Uint8(unsigned int Bitset_Address, unsigned int Read_Number)
if (Read_Number >= 8)
{
unsigned int len = Bitset_Address + 8;
unsigned int len = Bitset_Address + 7;
for (unsigned int i = 0; i < 8; i++)
{
Date = Date << 1 | (int)Coil_date[len--];
Date = Date << 1;
Date = Date | (int)Coil_date[len--];
}
}
else
@@ -44,7 +45,8 @@ UINT8 Bitset_to_Uint8(unsigned int Bitset_Address, unsigned int Read_Number)
unsigned int len = Bitset_Address + Read_Number-1;
for (unsigned int i = 0; i < Read_Number; i++)
{
Date = Date << 1 | (int)Coil_date[len--];
Date = Date << 1;
Date = Date | (int)Coil_date[len--];
}
}
return Date;
@@ -343,27 +345,38 @@ bool Create_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message, DWO
}


HANDLE Input_Parameter()
{
HANDLE Handle_Com;
while (true)
{
string COMM = Input_COMM();
unsigned int Baud_Rate = Input_Baud_Rate();
BYTE Date_Bits = Input_Date_Bits();
BYTE Stop_Bits = Input_Stop_Bits(Date_Bits);
BYTE Parity = Input_Parity();
Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), Baud_Rate, Date_Bits, Stop_Bits, Parity);
if (Handle_Com == INVALID_HANDLE_VALUE)
{
cout << "初始化串口失败,请重新输入设备信息\n" << endl;
}
else
{
printf("初始化串口成功\n");
break;
}
}
return Handle_Com;
}

int Modbus_RTU_Salve(void)
{
string COMM = Input_COMM();
unsigned int Baud_Rate = Input_Baud_Rate();
BYTE Date_Bits = Input_Date_Bits();
BYTE Stop_Bits = Input_Stop_Bits(Date_Bits);
BYTE Parity = Input_Parity();
RTU_Enable = Input_RTU_Enable();
UINT8 Requst_Message[MAX_NUMBER];
UINT8 Response_Message[MAX_NUMBER];
HANDLE Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), Baud_Rate, Date_Bits, Stop_Bits, Parity);
if (Handle_Com == INVALID_HANDLE_VALUE)
{
cout << "初始化串口失败" << endl;
getchar();
return 0;
}
else
printf("初始化串口成功");
HANDLE Handle_Com = Input_Parameter();
Init_Coil_Register();
DWORD Read_len;
int Count = 0;
while (true)
{
PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存
@@ -374,15 +387,32 @@ int Modbus_RTU_Salve(void)
{
while (!SendData(Handle_Com, (char*)Response_Message, Response_Message_Len))
{
printf("发送失败,重新发送");
Count++;
printf("发送失败,重新发送第%d次\n",Count);
if (Count > 4)
{
printf("设备端口异常\n");
break;
}
}
printf("主站请求 :");
for (unsigned int i = 0; i < Read_len; i++)
{
printf("%02x ", Requst_Message[i]);
}
printf("\n");
printf("从站响应 :");
for (unsigned int i = 0; i < Response_Message_Len; i++)
{
printf("%02x ", Response_Message[i]);
}
printf("\n\n");
Log_Note(Requst_Message, 0, Read_len);
Log_Note(Response_Message, 1, Response_Message_Len);
}
else
continue;
}

}
CloseHandle(Handle_Com);
getchar();


正在加载...
取消
保存