Re: Slave publish callback
Posted: Tue 4. Jan 2022, 15:30
Seems, that example is working. Now I don't understand what's wrong. I try to look the code in more detail. I will let you know.
Aleš
Aleš
Support, Knowledge Base, and FAQ
https://forum.peak-system.com/
Code: Select all
int handleRet(DWORD ret, std::string msg) {
if (ret == 0) cout << "OK! " + msg + " success.\n";
else cout << "ERROR! " + msg + " failed (err_code=" + toString(ret) + ").\n";
return ret;
}
std::atomic<int> counter = 1;
std::atomic<bool> quit = false;
std::string getNewData() {
std::string data = toString(counter);
std::string remaining = std::string(4 - data.size(), ' ');
return remaining.append(data);
}
HLINCLIENT cli;
HLINHW hw_pointer;
void simpleUpdator() {
//Check if connected HWs
HLINHW arr[5];
USHORT arr_sz = sizeof(arr) / sizeof(arr[0]);
if (handleRet(LIN_GetClientParam(cli, clpConnectedHardware, arr, arr_sz), "Getting connected HWs") == errOK) {
int hw_count = (int)arr[0];
cout << "Connected " << hw_count << " HWs.\n";
for (int i = 1; i < hw_count + 1; i++) {
cout << i << "_HW HANDLE=" << (int)arr[i] << endl;
}
}
while (quit == false) {
TLINRcvMsg r;
TLINError err = LIN_Read(cli, &r);
if (err == errOK)cout << err << " " << (int)r.FrameId << " type=" << (int)r.Type << endl;
if (err == errOK && r.Type == mstStandard && r.hHw == hw_pointer && r.FrameId == 0x42) {
std::string new_data = stringToCharArrString(getNewData());
BYTE arr[2] = { 0 };
for (size_t i = 0; i < 2; i++)arr[i] = new_data[i];
DWORD ret = LIN_UpdateByteArray(cli, hw_pointer, 0x02, 0, 2, arr);
if (ret != 0) std::cout << "ERROR! Updating field failed, returned=\"" << ret << "\"." << endl;
counter++;
}
Sleep(1);
}
int rec_msgs = 0;
if (handleRet(LIN_GetClientParam(cli, clpReceivedMessages, &rec_msgs, sizeof(rec_msgs)), "Getting received msgs") == errOK) {
cout << "Received " << rec_msgs << endl;
}
cout << "Updator finished!" << endl;
}
int main()
{
LPSTR buff = new CHAR[5]{ 'C','L','I','\n',0 };
handleRet(LIN_RegisterClient(buff, 0, &cli), "Registration of client.");
delete[] buff;
//Connect to channel
HLINHW *li = nullptr;
int list_count = 0;
int ch_count = 0;
TLINError ret = handleRet(LIN_GetAvailableHardware(NULL, 0, &ch_count), "Get HWs count"); //Get connected HWs
if (ret == errOK) {
HLINHW * list = new HLINHW[ch_count];
ret = handleRet(LIN_GetAvailableHardware(list, ch_count * sizeof(HLINHW), &ch_count), "Get HW list");
if (ret == errOK) {
hw_pointer = list[0]; //Channel 1
ret = handleRet(LIN_ConnectClient(cli, hw_pointer), "Connect HW to client");
if (ret == errOK) {
ret = handleRet(LIN_InitializeHardware(cli, hw_pointer, modSlave, 19200), "Init HW");
}
}
delete[] list;
}
//Adding message ID=0x02
std::string doubleHexString = stringToCharArrString(getNewData());
size_t sz = doubleHexString.size();
TLINFrameEntry frame;
frame.FrameId = 0x02;
frame.Length = (BYTE)sz;
frame.Direction = dirPublisher;
for (size_t i = 0; i < sz; i++) frame.InitialData[i] = (int)doubleHexString[i];
frame.ChecksumType = cstEnhanced;
frame.Flags = FRAME_FLAG_RESPONSE_ENABLE;
handleRet(LIN_SetFrameEntry(cli, hw_pointer, &frame), "Adding LIN frame");
//Adding SLAVE thread
std::thread t(simpleUpdator);
getchar();
quit = true;
t.join();
//Delete when SLAVE ends
handleRet(LIN_DisconnectClient(cli, hw_pointer), "Delete lin channel");
handleRet(LIN_RemoveClient(cli), "Delete client");
cout << "ENDED" << endl;
}
Code: Select all
// Set the client filter with the mask.
m_LastLINErr = m_objPLinApi->SetClientFilter(m_hClient, m_hHw, m_lMask);