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 8731935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:20:54+00:00 2026-06-13T09:20:54+00:00

I have to send a command over serial and receive back an answer based

  • 0

I have to send a command over serial and receive back an answer based on the command and do something based on the message received. I was told that I have to use callbacks as this is an asynchronous operation.

I have a 2 threads, one that can send messages and one that receives the messages.

Example:

//Thread 1
sendMessage("Initialize");

//Thread 2 
while(1)
{
    checkForMessages();
}

How can I write a function that is initialized for a specific message and handles the message recieved.

Example:

CommHandle(Command,MsgReceived)
{
  if(command)
  {
    if(MsgReceived == ok)
    ...
    if(MsgReceived == error)
    ...
  }

}

  • 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-13T09:20:55+00:00Added an answer on June 13, 2026 at 9:20 am

    I was told that I have to use callbacks as this is an asynchronous operation.

    Not necessarily. There is something in Windows called “asynchronous I/O”, this is to be regarded as an internal Windows term, which is synonymous with “overlapped I/O” (explanation here). When you are using overlapped I/O, you will get a callback when the transmission is finished. This is nice, since it reduces CPU load, but it is not really necessary if your program has nothing better to do while waiting. So it depends on the nature of your application.

    But no matter the nature of your application, you should indeed handle all serial communication through threads, so that you won’t cause the main GUI thread to freeze up in embarrassing ways.

    Having one rx and one tx thread gives you a dilemma though: they are using the same port handle and they cannot freely access it, because that wouldn’t be thread-safe. The solution is to either make one single super-thread handling all transmissions, or to protect the port handle through a mutex.

    I’m not sure which method that is best, I have no recommendation. I have only used the “super-thread” one myself: one obvious advantage was that I could centralize WaitFor instructions like “kill thread”, “port is open”, “port is closed” at one place. But at the same time the code turned out rather complex.

    How can I write a function that is initialized for a specific message and handles the message recieved.

    Let your thread(s) shovel their received data into some buffers. A tx buffer and a rx buffer. Depending on your serial protocol and performance, you might have to use double buffers: one that is used for the current transmission and one that contains the most recently received data.

    Then from main, pick up the data from the buffers. They need to be thread-safe. Once you have gotten that far, simply write a parser like you would with any form of data and take actions from there

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

Sidebar

Related Questions

So I have this test code to send HELLO over a USB serial port:
I have a hardware here, wich communicates over serial port. I use MS Visual
I have a class the communicates with a device over TCP. I send commands,
I have a D-Link DWM-156 3G USB modem that I want to send AT-commands
I have a need to send a message to a number of people to
I have a java app that uses ProcessBuilder to prepare an operating system command
I have a server than is a command handler process. It receives messages over
I have the following code on my Arduino that constantly checks for a serial
I have a small program, that sends sms messages over SerialPort. It works nice
Currently our application connects to an Arduino over a serial port. We send some

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.