Results 1 to 5 of 5

Thread: Problem with Event,serial port win32 API

  1. #1
    Join Date
    Apr 2009
    Posts
    89

    Problem with Event,serial port win32 API

    Hi all

    That I'm working on the serial port win32 api and I must, my liaison with asynchronous read character go once they are received on the serial port.

    so I wrote this code.

    C + + code:

    Code:
      # include <windows.h> 
      # include <assert.h> 
      # include <stdlib.h> 
      # include <conio.h> 
      # include <stdio.h> 
      # include <vcl.h> 
      # pragma hdrstop 
    
      //------------------------------------------------ --------------------------- 
    
      # pragma argsused 
      int main (int argc, char * argv []) 
      ( 
              Overlapped over; 
              HANDLE hComm; 
              DWORD EventMask; 
              hComm = CreateFile ( "COM1", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); 
              if (hComm == INVALID_HANDLE_VALUE) ( 
                      printf ( "Error opening port"); 
              Else () 
                      if (SetCommMask (hComm, EV_RXCHAR)) ( 
                              over. hEvent = CreateEvent ( 
                                      NULL, 
                                      TRUE, 
                                      FALSE, 
                                      NULL); 
                              over. Internal = 0; 
                              over. InternalHigh = 0; 
                              over. Offset = 0; 
                              over. OffsetHigh = 0; 
                              assert (over.a hEvent); 
                              while (true) ( 
                                      if (WaitCommEvent (hComm, & EventMask, & over)) ( 
                                              if (EventMask & EV_RXCHAR) ( 
                                                      printf ( "character received"); 
                                                      getch (); 
                                              ) 
                                      ) 
                                      else ( 
                                              printf ( "Error% d", GetLastError ()); 
                                              getch (); 
                                      ) 
                              ) 
                      Else () 
                              printf ( "error creation event% d", GetLastError ()); 
                      ) 
              ) Getch (); 
              return 0; 
      )
    Outside I have a big problem, waitcommevent me always returns 0, so the error getlast I watch what I can and if I take it in my loop I have something like

    Error 997 ERROR_IO_PENDING
    Error 87 ERROR_INVALID_PARAMETER

    So now it's been a good time am with the forums and but I can not find anything so I put my query here.

    If you have an genius idea please let me know.

    (in case I test the connection with hyperterminal no problem 2)

  2. #2
    Join Date
    Jan 2009
    Posts
    99

    Re: Problem with Event,serial port win32 API

    You should perhaps re-structure overlapped between each call. when i tried out different program with little bit of same logic, I do not repeat the event, by against) ...

  3. #3
    Join Date
    Apr 2009
    Posts
    89

    Re: Problem with Event,serial port win32 API

    I rebuilt my structure is no longer the event

  4. #4
    Join Date
    Jan 2009
    Posts
    99

    Re: Problem with Event,serial port win32 API

    Just remember its handle to another variable ...

    Code:
     # include <windows.h> 
      # include <assert.h> 
      # include <stdlib.h> 
      # include <conio.h> 
      # include <stdio.h> 
    
      //------------------------------------------------ --------------------------- 
    
      # pragma argsused 
      int main (int argc, char * argv []) 
      ( 
    	  HANDLE hComm; 
    	  hComm = CreateFile ( "COM1", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); 
    	  if (hComm == INVALID_HANDLE_VALUE) ( 
    		  printf ( "Error opening port"); 
    	  Else () 
    		  if (SetCommMask (hComm, EV_RXCHAR)) ( 
    			  HANDLE hEvent = CreateEvent ( 
    			   NULL, 
    			   TRUE, 
    			   FALSE, 
    			   NULL); 
    			  while (true) ( 
    				  DWORD EventMask; 
    				  Overlapped over; 
    				  over. hEvent = hEvent; 
    				  over. Internal = 0; 
    				  over. InternalHigh = 0; 
    				  over. Offset = 0; 
    				  over. OffsetHigh = 0; 
    				  assert (over.a hEvent); 
    				  if (WaitCommEvent (hComm, & EventMask, & over)) ( 
    					  if (EventMask & EV_RXCHAR) ( 
    						  printf ( "character received"); 
    						  getch (); 
    					  ) 
    				  Else () 
    					  printf ( "Error% d", GetLastError ()); 
    					  getch (); 
    				  ) 
    			  ) 
    		  Else () 
    			  printf ( "error creation event% d", GetLastError ()); 
    		  ) 
    	  ) 
    	  getch (); 
    	  return 0; 
      )
    But on the other hand, I think that this was not the source of the problem: The problem is apparently that WaitCommEvent () is supposed to return ERROR_IO_PENDING and that you must then do a WaitForSingleObject () on hEvent. ..

  5. #5
    Join Date
    Apr 2009
    Posts
    89

    Re: Problem with Event,serial port win32 API

    I try solution 2

    still nothing

    Edit: I replace the FILE_FLAG_OVERLAPPED with 0 and now it goes, but after I'll have to know why> <(I must submit my project for my project) so someone will have an idea?

Similar Threads

  1. USB Virtual Serial Port
    By Warren Hill in forum Hardware Peripherals
    Replies: 1
    Last Post: 26-02-2011, 12:19 AM
  2. Dongle Problem with windows XP SP2 using LPT1 serial port
    By Rafftar in forum Hardware Peripherals
    Replies: 3
    Last Post: 15-10-2010, 02:47 AM
  3. PCI Serial Port Driver
    By Ottawa in forum Hardware Peripherals
    Replies: 6
    Last Post: 12-05-2010, 09:31 AM
  4. Need to use a serial device in usb port
    By affleck in forum Hardware Peripherals
    Replies: 2
    Last Post: 16-09-2009, 11:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,005,913.75247 seconds with 17 queries