Thread: Weired serial port problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    Weired serial port problem

    Hi guys:

    Here are the sample codes about read and write of the serial port.

    serial.zip only uses nonoverlapped and serial3.zip uses overlapped.

    Both write the data to COM1 and read the data from COM2.

    The werid thing is:

    if I first time run serial.zip, it worked fine, it read one character "1" from COM2.

    After that, I run serial3.zip, it worked fine too, it read one character"T" from COM2.

    Then I was back to run serial.zip, now the problem existed, seems it was always waiting for character in input buffer of COM2.

    It might be something left in the serial port after running serial3.zip. I don't know that. I used PurgeCom() functions, but it didn't work at all.

    Both used quite basic lauses about serial port, but their comments are a little bit mess. Sorry for any inconvenience.

    I would apprecate that very much if someone could take a look at them. Maybe I did something stupid, but I really want to know that.

    Thanks

    Serial3.zip used some functions created by Codeplug.
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I don't know much about serial port operations but why do you wait twice in the non-overlapped version?
    Code:
    	   if (WaitCommEvent(hCom2, &dwEvtMask, NULL)) 
    		{
    			printf("test\n");
    			WaitCommEvent(hCom2, &dwEvtMask, NULL); 
    			
    			if (dwEvtMask == EV_RXCHAR) 
    			{
    		
    			 // To do.
    				bRead = ReadFile(hCom2, &valRead, dwToRead, &dwRead, NULL);
    				bytesRead++;
    			}
    
    		}
    Also:
    Code:
    	char input[] = "1234567890";
    	...
    	WriteACharBuffer((char *)&input,2 );
    input is already a char *, by passing its address you are getting a char ** which is why you have to cast it back to a char *.

    Hope that is of some help.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    Hi, thanx for the help, that's my mistake,

    just simple comment it the second

    WaitCommEvent(hCom2, &dwEvtMask, NULL);

    and


    WriteACharBuffer((char *)&input,2 );
    into
    WriteACharBuffer(input,2 );

    but these might not be the problem when I run them again.
    Don't laugh at me,I am just a SuperNewbie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending data over UDP from serial port
    By forumguy in forum Linux Programming
    Replies: 0
    Last Post: 04-25-2009, 02:10 PM
  2. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 09:32 AM
  3. Serial Port - Problem with binary frames
    By estratos in forum Linux Programming
    Replies: 2
    Last Post: 01-18-2006, 06:22 AM
  4. serial port communication from two Application
    By lsme in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-12-2005, 11:32 PM
  5. serial port data transmission
    By djarian in forum C Programming
    Replies: 2
    Last Post: 10-04-2003, 02:22 PM