You are here:

C++/Serial communication and Break field


QUESTION: I have a problem whit the Break Field while sending messages. I am writing a LIN application and should send a Break Field in the beginning of every Header Field but the variation of the length of the Break Field causes problems.

Are there any way to get the duration of the Break Signal to 9 to 13 Bits (2ms) in Windows and C++ ?

I have used Sleep() function as shown below but the Sleep() isn't precise enough.



ANSWER: Generally you can't Sleep very accurately since Windows will go off running other tasks.  You can use QueryPerformanceCounter and spin in a loop waiting for the 1mS to elapse.  Although doing so is frowned upon, 1mS isn't going to cause a lot of problems.  The other thing to try is to raise the current thread's priority and see if the Sleep gets more precise.


---------- FOLLOW-UP ----------

QUESTION: Thanks for the reply

I have come to the conclusion that it is impossible to get the timing right whit User-space application in windows. I have been told that writing a kernel space serial port device driver could solve the problem? I don't know a lot about this but it seems like a lot of work?

I've never done this, so can't argue either way.

But I'll keep giving you ideas.

Set the CommTimeouts to call the event callback when receiving times out after 1mS.  I would think the OS would do the callback quickly since it represents an error, and in the callback handler you can turn the break off.



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 All rights reserved.