Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bfs/serialPortComs/checksum.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
66 lines (48 sloc)
1.45 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
struct HexCharStruct // Fixes char to hex stuff? | |
{ | |
unsigned char c; | |
HexCharStruct(unsigned char _c) : c(_c) { } | |
}; | |
inline ostream& operator<<(ostream& o, const HexCharStruct& hs) | |
{ | |
return (o << hex << (int)hs.c); | |
} | |
inline HexCharStruct hex(unsigned char _c) | |
{ | |
return HexCharStruct(_c); | |
} | |
unsigned char calChecksum(const vector<unsigned char> msg) | |
{ | |
unsigned char sum = 0; | |
for (int i=1; i < msg.size(); i++) | |
{ | |
sum += msg[i]; | |
} | |
unsigned char check = 0x100 - (sum & 0xFF); | |
return check; | |
} | |
// int main() // Use vector so that it can be trivialy long. | |
// { | |
// unsigned char preamb = 0xFA; | |
// unsigned char BID = 0xFF; | |
// unsigned char MID = 0x30; | |
// unsigned char LEN = 0x00; | |
// //unsigned char check1 = 0x100 - ((BID + MID + LEN) & 0xFF); // This outputs the correct checksum. | |
// vector<unsigned char> mess; | |
// mess.push_back(preamb); | |
// mess.push_back(BID); | |
// mess.push_back(MID); | |
// mess.push_back(LEN); | |
// // Data chars would go here | |
// unsigned char check = calChecksum(mess); | |
// //cout << "Checksum is: " << hex(check) << endl; | |
// mess.push_back(check); | |
// cout << "GoToConfig message: "; | |
// for (unsigned char c: mess) // Could instead be adding these to the buffer | |
// cout << hex(c) << " "; | |
// cout << endl; | |
// return 0; | |
// } |