Advertisement
RybaSG

Untitled

Jun 4th, 2021
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. void Serial::Task(void* arg)
  2. {
  3.     Serial* pThis = reinterpret_cast<Serial*>(arg);
  4.  
  5.     while (pThis->m_EnableWorkingThreads)
  6.     {
  7.         timeval timeout;
  8.         timeout.tv_sec = 0;
  9.         timeout.tv_usec = 1000 * 100;
  10.  
  11.         FD_ZERO(&pThis->fds_read);
  12.         FD_SET(pThis->m_SerialPortFileDescriptor, &pThis->fds_read);
  13.  
  14.         int result = select(pThis->m_SerialPortFileDescriptor + 1, &pThis->fds_read, &pThis->fds_write, nullptr, &timeout);
  15.         printf("select result: %d\n", result);
  16.         if (result <= 0)
  17.         {
  18.             printf("Select failed or timeout occured: %d\n", result);
  19.             continue;
  20.         }
  21.  
  22.         if (FD_ISSET(pThis->m_SerialPortFileDescriptor, &pThis->fds_write))
  23.         {
  24.             printf("FD_ISSET(pThis->m_SerialPortFileDescriptor, &pThis->fds_write)\n");
  25.             while (!pThis->m_WriteBuffer.empty())
  26.             {
  27.                 // printf("Write\n");
  28.                 BytesVector message = pThis->m_WriteBuffer.pop_front();
  29.  
  30.                 int bytesWritten = write(pThis->m_SerialPortFileDescriptor, message.data(), message.size());
  31.                 if (bytesWritten < 0 || bytesWritten != message.size())
  32.                 {
  33.                     // TODO: Debug log
  34.                     printf("========== WRITE FAILED ==========\n");
  35.                 }
  36.             }
  37.         }
  38.  
  39.         if (FD_ISSET(pThis->m_SerialPortFileDescriptor, &pThis->fds_read))
  40.         {
  41.             printf("FD_ISSET(pThis->m_SerialPortFileDescriptor, &pThis->fds_write)\n");
  42.             int bytesRead = read(pThis->m_SerialPortFileDescriptor, pThis->m_BytesChunk.data(), pThis->m_BytesChunk.size());
  43.             printf("BytesRead:%d\n", bytesRead);
  44.             if (bytesRead <= 0)
  45.             {
  46.                 printf("Read Failed: %d\n", result);
  47.                 // TODO: Read failed
  48.                 continue;
  49.             }
  50.  
  51.             // If received less than MINIMAL_BYTES_CHUNK bytes, resize buffer.
  52.             if (bytesRead < MINIMAL_BYTES_CHUNK && bytesRead > 0)
  53.             {
  54.                 pThis->m_BytesChunk.resize(bytesRead);
  55.             }
  56.  
  57.             std::cout << std::endl;
  58.             for (auto x: pThis->m_BytesChunk)
  59.             {
  60.                 printf("%02x ", x);
  61.             }
  62.  
  63.             pThis->m_ReadBuffer.push_back(std::move(pThis->m_BytesChunk));
  64.             pThis->m_BytesChunk.resize(MINIMAL_BYTES_CHUNK);
  65.         }
  66.     }
  67. }
  68.  
  69. void Serial::Send(BytesVector&& payload)
  70. {
  71.     // bool isWriteBufferEmpty = m_WriteBuffer.empty();
  72.  
  73.     m_WriteBuffer.push_back(std::move(payload));
  74.  
  75.     // If write buffer was not empty that means asynchronous write process
  76.     // is in progress and calling Write() method is unnecessary.
  77.     // if (isWriteBufferEmpty)
  78.     // {
  79.         Write();
  80.     // }
  81. }
  82.  
  83. void Serial::Write()
  84. {
  85.     FD_ZERO(&fds_write);
  86.     FD_SET(m_SerialPortFileDescriptor, &fds_write);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement