Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:09:16+00:00 2026-06-15T01:09:16+00:00

I am a bit unsure about this question, but I constantly running into troubles

  • 0

I am a bit unsure about this question, but I constantly running into troubles with my current design, and would be really greatful, if someone could show be a different approach to this.

My program writes async commands to a device via rs232, while constantly reading and reacting to received data.

This works all nice and neat, but during my init-phase, I have to send a bunch of commands, which would have to wait for a response, before letting the program countinue.

Now, this part I have to do in plain C, and all I could come up with was to use global vars and while loops. But I find this really not pretty.

Take this as an pseudo-code example:

OnReceive(data){
 switch determineCommand(data)
   case CMD1:
      config.value1 = data.value1             
   case CMD2:
      config.value2 = data.value2
   default:
      print data
}

DoCommandChain(){
  Send(CMD1)
  If("Send(CMD1)" got its response){
    Send(CMD2)
  }
}

Now, the problem is “If XY got its response” – because I don’t want to use some vars to detect this and I can’t rely on a return value for Send(CMD1), because this is just an indicator, that CMD1 was send – not there was something corresponding received.

I am asking here, because I would like to know what I could search for / read about, to solve this mess in a nice way.

Right now, my best idea for this would be to somehow setup a timer-guarded-function to monitor, if a specific response is received. Then, depending on the status of this response, go to the next Send or retry the last.

Like this:

Instead of `Send(CMD1)` -> `DoUntilResponse(Send(CMD1),Timeout,NumberOfRetries)`

DoUntilResponse(function,Timeout,NumberOfRetries){
   registerExpectedResponse(CMD1, gotResponse) //awaiting some response for CMD1

   for(i=0 ; i!=Numberofretries; i++){
       if (Send(CMD1) == successfulSend ){
          while(not timeout){
             if gotResponse then break;
             }
          if gotResponse then break;
      }           
   }

}

Edit:

Just to clarify: I am not worried about the serial-connection, or about how to fire the OnReceive function – this is all working already. What I can’t get a clear idea of, is how to solve the above pseudo-code without the use of polling and preferably in pure C.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:09:17+00:00Added an answer on June 15, 2026 at 1:09 am

    Assuming that write and read operations are executed in different threads, use the following algorithm:

    Write thread.
        Register expected response.
        Send packet to the device
        Wait for Received event with timeout. If event is set, continue. Timeout - 
        report error and exit (or make additional trial).
    
    Read thread.
    For every received packet:
        Add it to the input buffer.
        Analyze buffer. If it contains valid response, set "Received" event, 
        releasing the Write thread, clear input buffer, and continue reading. 
        If input buffer contains unrecognized data, report error, clear buffer 
        and continue reading.
        If input buffer contains the beginning of expected response, 
        continue reading.
    

    Threading and event notification is OS-specific. If your C++ compiler doesn’t support multithreading, use portable library like Boost, or use OS-specific API. Notice that Read thread implements stream parsing logic, since serial communication is stream-oriented.

    Edit.

    “Register expected response” means: set some program variable(s) than mean “Command of type X is sent to device”. According to this variable, Read thread expects to receive the packet, which should be sent by device according to communication protocol (application-specific). Another received packet, which may be generally valid, should be treated as error, because this is not response to the command just sent to device.

    Another way: Set expected response size. In this case, once Read thread received expected amount of data, it sets Received event, leaving packet recognition task to the sender.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still a bit unsure how I would find this in the documentation, but
I'm doing my best to learn DDD, but I'm unsure about some really basic
Bit of an unusual question, but I have setup a field inside a MySQL
Bit of a javascript newbie so not sure if this question is easy or
Bit of a strange question here i know. but i wanted to know if
This is more a theoretical question than a practical one, but given I undestand
I am still learning quite a bit about programming so this may be an
I have the following piece of code. I am a bit unsure about retain/release
I found this java question on the internet and had some questions about it.
I'm planning to host a Rails application on Linode, but I'm still unsure about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.