Serial Port Polling
Hi all: I am building a PC based system to talk to Microchip PICS via the RS232 ( converted to rs485) port. I have the software working nice in hyperterm and t. Describes interfacing the Serial (RS-232) Port in relation to both Hardware and Software. Polling the UART is a lot slower method. Firstly, let me preface by saying I feel silly for asking a question like this. The information should be online, but I can't seem to find it anywhere. I feel like I. I am just reviewing some code I wrote to communicate with the serial port in C# on CF2.0. I am not using DataReceived event since it's not reliable. MSDN states that.
I am writing a program that receives data from a serial port at random times. I don't know when the port is going to send me data until it does. I would like to know someway I can set the serial port to wait until data appears. The 2 things that are making this difficult for me are these, 1) I am using ce so I cannot use FILE_FLAG_OVERLAPPED, 2) I need to be able to shutdown while waiting, currently this leads to a hang since I am using WaitCommEvent and its still waiting for a signal change. I'm looking for some way do this and any help is appreciated. I will supply any code on request. Thanks Abe Oh I am looking for something a little more robust than something like a sleep(500) readfile loop.
>>1) I am using ce so I cannot use FILE_FLAG_OVERLAPPED, ack. >>2) I need to be able to shutdown while waiting, If a process attempts to change the device handle's event mask by using the SetCommMask function while an overlapped WaitCommEvent operation is in progress, WaitCommEvent returns immediately. Bios Agent 3.66. The variable pointed to by the lpEvtMask parameter is set to zero. Found this in the docs: it's contradictory, since if a WaitCommEvent operation in progress is an overlapped one, then what would return immediately should be a WaitFor.() function or possibly GetOverlappedResult(), and not WaitCommEvent() itself. What about trying to have a thread waiting for the shutdown event and using SetCommMask() to 'kill' the WaitCommEvent() in the other thread? Alternatively, why not try to kill the thread which is waiting for the Comm event? PS: any links added to the text are not endorsed by me, or any of my affiliates, and any opinions or facts expressed by these links are not representative of _nn_;).
Changed ReadMsg. My code was sloppy. >>Are you sure you want to add the data char by char in msgList? Drivers Para Multifuncional Hp Photosmart D110 Series.
Eventually I will run these characters through a function that will combine them to make messages and add timestamps but for now I just wanted to get the basics working. Is this what you were asking or something else? >>Can you please be more specific? The way my code works now characters can be read immediately as they come in, which is perfect. The problem is messages can only be sent every half second, due to the COMMTIMEOUT setting of 500, well faster if a character is read since that breaks the wait. So usually only 1 message can be sent every half second.
I could lower the ReadTotalTimeoutConstant to send messages faster but this leads to more and more CPU drain, up to 100% if I set it to 0. Murdered.exe 0xc0000142. What seems more logical to me is to do it with events. Something like.
Wait for incoming characters until character is received or SendEvent. Then either read the character or send the message and restart loop. It seems I somehow missed your update. Thanks for the points:) Out of curiosity, did you try to really read char per char? I mean: if (!ReadFile(comport, buffer, 1, &numRead, 0) numRead == 0) If you want to speedup things, you could possibly use ClearCommError().