CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Apr 2017
    Posts
    18

    Re: Why doesn't this method of closing a worker thread work?

    Quote Originally Posted by 2kaud View Post
    The easiest way is probably to sleep the thread for 50ms.
    This only guarantees a lower limit. So there is a possibility that the process might sleep for longer and miss the next packet of data. Won't my idea work?

  2. #17
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] Why doesn't this method of closing a worker thread work?

    If the data is only being received every 50ms, then its probably easier to just wait for the data comm event to occur using WaitCommEvent(). See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    This will wait until a comm event is signalled. The lpEvtMask parameter indicates what comm event is signalled. For data being received then this
    would be EV_RXCHAR when all chars available could then be read. The loop then turns into something like

    Code:
    DWORD	dwCommEvent;
    
    while (true) {
        WaitCommEvent(hcomm, &dwCommEvent, NULL);
        if (dwCommEvent & EV_RXCHAR) {
            //Character(s) waiting to be read
            //Read all available chars from comm port and process
        }
    }
    Last edited by 2kaud; June 10th, 2017 at 07:22 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] Why doesn't this method of closing a worker thread work?

    A code example of reading from a comm port, assembling one line of read data and processing it is
    Code:
    //Thread master function
    unsigned __stdcall ProcessData(PVOID args)
    {
    HANDLE	hComm = *(HANDLE*)args;
    
    DWORD	dwCommEvent,
    		dwRead;
    
    char	chRead;
    
    string	line;
    
    	for ( ; ; ) {
    		if (WaitCommEvent(hComm, &dwCommEvent, NULL) && (dwCommEvent & EV_RXCHAR))
    			do {
    				if (ReadFile(hComm, &chRead, 1, &dwRead, NULL)) {
    					if (dwRead == 1) {
    						if (chRead < ' ') {
    							if (chRead == 13) {
    								Process(hComm, line);
    								line = "";
    							}
    						} else
    							line += chRead;
    					}
    				} else {
    					cout << "error in the ReadFile call\n";
    					return 8;
    				}
    			} while (dwRead);
    		else {
    			cout << "error in waitcommevent\n";
    			return 9;
    		}
    	}
    }
    Last edited by 2kaud; June 10th, 2017 at 07:26 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    Join Date
    Apr 2017
    Posts
    18

    Re: [RESOLVED] Why doesn't this method of closing a worker thread work?

    Thank you so much! This is way beyond anything you needed to do!
    Could you explain what you are doing with chRead in the two if statements? Also what does Process do? I just want to understand this, rather than just lift it.
    A link to any resource would do, I would not want to waste your time.

  5. #20
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [RESOLVED] Why doesn't this method of closing a worker thread work?

    Another good essay of Joe Newcomer about using serial ports: http://flounder.com/serial.htm
    Victor Nijegorodov

  6. #21
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] Why doesn't this method of closing a worker thread work?

    Could you explain what you are doing with chRead in the two if statements?
    hComm is the handle to the port obtained from CreateFile().

    When a comms event occurs then WaitCommEvent() will return. If the event is EV_RXCHAR then at least one char is ready to be read from the port. The do loop reads one char at a time from the port until no chars are read. For the purpose this code is used, the device being read sends data one line at a time terminated by a CR (ASCII code 13). If a char 13 is read then the end of line has been read and the accumulated string is passed to the process function. Chars with a value of less than a <space> in this application are ignored - hence the test for < ' '. If the read char is >= ' ' then it is appended to the string line and the loops repeat. Whether the processing of the read char in this example code in your case is appropriate or not will depend upon the format that the device from which you are reading sends its data.

    Also what does Process do
    Process() simply processes the completed assembled line of data read from the device in the string line. This is whatever you want to do with the read data.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Why doesn't this method of closing a worker thread work?

    Quote Originally Posted by VBCherry View Post
    This only guarantees a lower limit. So there is a possibility that the process might sleep for longer and miss the next packet of data.
    No Windows user timer can be relied upon to be 100% accurate as the OS is not real-time. I agree that having Sleep() in code is not usually a good idea but I thought that you wanted simply to sample the input every 50ms rather than read the data which is received every 50ms. IMO using any windows timing mechanism in this scenario is not recommended as exactly 50ms cannot be guaranteed - and if slightly longer could miss the next packet as you stated. Also the time taken to process the data packet needs to be taken into consideration (which needs to be less than 50ms). The wait time plus the process time needs to be 50ms which is going to be very difficult - if not impossible - to undertake. Therefore as suggested in my posts #17 and #18 and in the article referenced by Victor in post #20 IMO other ways should be investigated that don't rely upon timing.
    Last edited by 2kaud; June 10th, 2017 at 03:23 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured