Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Converting char to char* Pin
talkster54-May-05 5:33
talkster54-May-05 5:33 
GeneralRe: Converting char to char* Pin
David Crow4-May-05 6:07
David Crow4-May-05 6:07 
GeneralRe: Converting char to char* Pin
talkster54-May-05 6:17
talkster54-May-05 6:17 
GeneralRe: Converting char to char* Pin
talkster54-May-05 6:21
talkster54-May-05 6:21 
GeneralRe: Converting char to char* Pin
John R. Shaw4-May-05 9:55
John R. Shaw4-May-05 9:55 
GeneralSetTimer() in Serial Comm Program Question Pin
CNewbie3-May-05 7:59
CNewbie3-May-05 7:59 
GeneralRe: SetTimer() in Serial Comm Program Question Pin
Trollslayer3-May-05 12:28
mentorTrollslayer3-May-05 12:28 
GeneralRe: SetTimer() in Serial Comm Program Question Pin
John R. Shaw4-May-05 11:10
John R. Shaw4-May-05 11:10 
ONTIMER: (this all most application need)

Since the the data received (using Windows serial communication) is received/sent in the background, you can use the SetTimer() function and just check if any new data has arrived in your OnTimer() event function. How often you need to check depends on the size of the receive buffer you specified when you setup communication (See SetupComm()). To detemine if any data has been received just call ClearCommError().

MULTITHREADING:

Of course you could use a seperate thread that uses shared data buffers, protected by critical sections so your threads will not step on each others toes. What I mean by shared buffers is one or more buffers you and the Rx thread both share, when you're reading from a buffer, the RX thread cann't write to it and when the RX thread is writing to it, you cann't read from it. Then all you need to do is calculate approximatly how long it should take to receive X-number of characters and go to sleep for that period of time in your RX thread; when the thread wakes up it then reads the data into the buffer and goes back to sleep (that takes care of the CPU usage problem).

You could use two buffers so that the RX thread could be writing into one while you are reading the other one.

OR

you could use a ring buffer so that you can read from the tail end while the receiver is writing to the head end. (this can be a bit more tricky).

The RX thread can inform you that new data has arrived by posting (not sending) a message back to your main thread or by setting an event flag that you must check for every now and then, either in an OnTimer() event function or in the main message loop.

FINALY:

That should be enough to help solve your problems.

FYI:

There are some articles at CP (CSerial) and in the MSDN library that use multiple threading for serial communication.

GOOD LUCK!

INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
GeneralRe: SetTimer() in Serial Comm Program Question Pin
CNewbie4-May-05 11:24
CNewbie4-May-05 11:24 
GeneralRe: SetTimer() in Serial Comm Program Question Pin
John R. Shaw4-May-05 12:09
John R. Shaw4-May-05 12:09 
GeneralCreating a dll using a type library Pin
ChicoM3-May-05 7:51
ChicoM3-May-05 7:51 
GeneralMulti arrays Pin
bitsNbites3-May-05 6:29
bitsNbites3-May-05 6:29 
GeneralRe: Multi arrays Pin
David Crow3-May-05 6:47
David Crow3-May-05 6:47 
GeneralRe: Multi arrays Pin
bitsNbites3-May-05 7:52
bitsNbites3-May-05 7:52 
GeneralRe: Multi arrays Pin
David Crow3-May-05 8:01
David Crow3-May-05 8:01 
GeneralRe: Multi arrays Pin
Priyank Bolia3-May-05 19:52
Priyank Bolia3-May-05 19:52 
GeneralRe: Multi arrays Pin
bitsNbites4-May-05 15:42
bitsNbites4-May-05 15:42 
GeneralRe: Multi arrays Pin
Priyank Bolia4-May-05 18:19
Priyank Bolia4-May-05 18:19 
GeneralPlease help me. Pin
dSolariuM3-May-05 6:12
dSolariuM3-May-05 6:12 
GeneralRe: Please help me. Pin
David Crow3-May-05 6:50
David Crow3-May-05 6:50 
GeneralRe: Please help me. Pin
dSolariuM16-May-05 6:35
dSolariuM16-May-05 6:35 
GeneralFile Version Pin
picasso23-May-05 6:01
picasso23-May-05 6:01 
GeneralRe: File Version Pin
David Crow3-May-05 7:05
David Crow3-May-05 7:05 
GeneralRe: File Version Pin
Blake Miller3-May-05 7:26
Blake Miller3-May-05 7:26 
GeneralRich edit control problem Pin
bluehai3-May-05 5:38
bluehai3-May-05 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.