Skip to content

Commit

Permalink
Modbus Tick and crc
Browse files Browse the repository at this point in the history
  • Loading branch information
yan9a committed Jun 18, 2021
1 parent ade1485 commit 8b2098d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/ceModbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ uint16_t ceModbus::CRC16(char* s, size_t len, uint16_t crc)
size_t i, j;
for (i = 0; i < len; i++, s++) {
crc ^= ((unsigned int)(*s)) & 0xFF;
for (j = 0; j < 8; j++)
{
for (j = 0; j < 8; j++) {
if (crc & 0x0001) crc = (crc >> 1) ^ 0xA001;
else crc >>= 1;
}
Expand Down Expand Up @@ -92,7 +91,10 @@ size_t ceModbus::ReceiveRxFrame(char c)
uint16_t computed_crc = 0xFFFF;//initialize CRC
computed_crc = this->CRC16(rb, RxN, computed_crc);//calculate crc
// printf("\nComputed crc: %02X \n",computed_crc);
if (computed_crc == 0) { this->RxN -= 2; return (this->RxN); }//if crc is correct return bytes excluding crc
if (computed_crc == 0) {
//this->RxN -= 2;
return (this->RxN); //if crc is correct return bytes including crc
}
else { this->RxN = 0; }//discard the frame
}
return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/ModbusCmd/src/ModbusCmd.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File: ModbusCmd.cpp
// Description: Modbus RTS communication for RS485 switches
// WebSite: http://cool-emerald.blogspot.sg/2017/05/serial-port-programming-in-c-with.htmlactList
// WebSite: http://cool-emerald.blogspot.sg/2017/05/serial-port-programming-in-c-with.html
// MIT License (https://opensource.org/licenses/MIT)
// Copyright (c) 2021 Yan Naing Aye

Expand Down Expand Up @@ -397,7 +397,7 @@ void MyFrame::ModbusCmd(vector<uint8_t> cmd)

void MyFrame::OnTimer(wxTimerEvent& WXUNUSED(event))
{

this->_app->_modbus->Tick();
}

void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
Expand Down

0 comments on commit 8b2098d

Please sign in to comment.