Last active
November 18, 2016 13:12
-
-
Save Rolias/9240803 to your computer and use it in GitHub Desktop.
An example of one way to safely share a buffer between two tasks using the NetBurner version of uCOS.
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
//In the TCP Task | |
void TcpServer::ProcessMessage(int num_bytes_read) | |
{ | |
//There will always be room for the terminator because we only read a max of 1 less than full size | |
_rxBuffer[num_bytes_read++] = '\0'; | |
if (_debuggingTcp) cout << "tcp:Read bytes:" << num_bytes_read << " string read = " << _rxBuffer << endl; | |
_messageWaiting = true; //Set this first and process pending on mbox MUST call SetMessageProcessed(); | |
BYTE err_code = OSMboxPost(&_mailbox, static_cast<void*> (_rxBuffer)); //Put a message in the Mailbox. | |
if ((err_code != OS_NO_ERR) && _debuggingTcp) cout << "OSMboxPost error in tcpServer " << endl; | |
//Wait for response from task that processes the message. | |
err_code = OSSemPend(&_msgProcessed,MAX_WAIT_MSG_HANDLED_TICKS); | |
if (err_code != OS_NO_ERR) cout << "OSSemPend Error in TcpServer " <<endl; | |
} | |
//In the main event task which is using data passed in from the TCP Task | |
void Startup::MainEventLoop() | |
{ | |
const bool FOREVER = true; | |
const int MAILBOX_WAIT_TICKS = 20; | |
OSTimeDly(MAILBOX_WAIT_TICKS); | |
OS_MBOX& tcp_mailbox = TcpServer::GetMailbox(); | |
while (FOREVER) | |
{ | |
BYTE err; | |
void * pdata_from_tcp = OSMboxPend( &tcp_mailbox, MAILBOX_WAIT_TICKS, &err); | |
if (err != OS_TIMEOUT) | |
{ | |
Debug::Write("!",dl_TACITURN); | |
string puser_command = (char*) pdata_from_tcp; | |
ParseIncomingMsg(puser_command); | |
} | |
else | |
{ | |
Debug::Write(".",dl_VERBOSE); | |
} | |
} | |
} | |
void Startup::ParseIncomingMsg(string userMsg) | |
{ | |
cout << userMsg; | |
OS_SEM& msg_processed_sem = TcpServer::GetSemaphore(); | |
OSSemPost(&msg_processed_sem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Sir,
For a long time I was looking for something to refer to for cpp programming on net burner. You examples come as a big relief. It will be great if you could provide one complete example on how this works. A few questions though:
Regards