i am connecting mirochip to serial com232

i am sending
from the pc to micro controler
then the micro controller
send back that he recieve
so the pc send the next word

how to wait the incoming serial data
and how to define the timeout(for example 10 second)

thanks at all

Dani AI

Generated

A few practical options to wait for a reply and implement a 10‑second timeout, building on and comments but focused on C/C++ and cross‑platform reliability.

On POSIX (Linux, macOS) use either a select/poll wait or the termios VMIN/VTIME behavior. select lets you wait up to N seconds for any bytes to arrive and then call read to pull whatever is available. VTIME is expressed in deciseconds and can make a blocking read return after the configured interval if no bytes arrive. Example patterns:

/* select-based wait (10 s) */
fd_set rfds;
struct timeval tv = {10, 0};
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
if (select(fd+1, &rfds, NULL, NULL, &tv) > 0) {
    ssize_t n = read(fd, buf, sizeof(buf));
    /* process n bytes */
} else {
    /* timeout or error */
}
/* termios: wait up to 10s for first byte (VTIME = 100 deciseconds) */
tio.c_cc[VMIN]  = 0;
tio.c_cc[VTIME] = 100;
tcsetattr(fd, TCSANOW, &tio);
ssize_t n = read(fd, buf, sizeof(buf)); /* returns 0 on timeout */

On Windows use the COM port timeouts or overlapped I/O. Setting the COMMTIMEOUTS structure allows ReadFile to return after a fixed number of milliseconds if no data arrives. For GUI apps, do the serial reads on a worker thread so the UI stays responsive; overlapped reads plus WaitForSingleObject let you apply your own timeout logic.

Practical notes and debugging tips:

  • Match baud, parity, data bits and flow control exactly on both ends.
  • Use a clear end‑of‑message marker or fixed length so the PC knows when the MCU reply is complete.
  • Account for partial reads: loop and accumulate until the terminator or expected length is reached, but stop when the overall timeout elapses.
  • Verify wiring/levels (RS232 vs TTL) with a terminal program or logic analyzer before blaming code.

These approaches avoid busy loops, give deterministic 10s behavior, and handle partial data arrivals cleanly.

Recommended Answers

All 3 Replies

Post the code you have so far. Its more or less like using a socket with buffered read/writes. See SerialPort.ReadTimeout and SerialPort.WriteTimeout for setting the timeout values.

for timeout u can start a timer with the interval u want
if it enters @ timer1_tick then stop the timer and print a time out msg

u can use also DataRecieved Event to recieve data by calling
the ReadExisting() function
then u can send the next word

i hope it is the answer u r searching for
good luck

commented: Take the extra time to spell out words completely -1

thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.