Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ReadFile () reads Junk characters when tried to read from COM1 Port

ReadFile () reads Junk characters when tried to read from COM1 Port

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structureshelp
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pandit84
    wrote on last edited by
    #1

    Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.

    hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );

    Then I set the COM state

     GetCommState( hCom, &lpCC.dcb); 
     lpCC.dcb.BaudRate = CBR\_9600;     //  baud rate
     lpCC.dcb.StopBits = ONESTOPBIT;    //  stop bit	
     lpCC.dcb.ByteSize = 8;             //  data size, xmit and rcv		 
     lpCC.dcb.Parity   = NOPARITY;      //  parity bit
     lpCC.dcb.fBinary = TRUE ;
     lpCC.dcb.fParity = TRUE ;
          SetCommState( hCom, &lpCC.dcb ); 
    

    I am trying to write the data using

    WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);

    and trying to read the data using

    ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);

    ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks

    D K 2 Replies Last reply
    0
    • P pandit84

      Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.

      hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );

      Then I set the COM state

       GetCommState( hCom, &lpCC.dcb); 
       lpCC.dcb.BaudRate = CBR\_9600;     //  baud rate
       lpCC.dcb.StopBits = ONESTOPBIT;    //  stop bit	
       lpCC.dcb.ByteSize = 8;             //  data size, xmit and rcv		 
       lpCC.dcb.Parity   = NOPARITY;      //  parity bit
       lpCC.dcb.fBinary = TRUE ;
       lpCC.dcb.fParity = TRUE ;
            SetCommState( hCom, &lpCC.dcb ); 
      

      I am trying to write the data using

      WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);

      and trying to read the data using

      ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);

      ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      pandit84 wrote:

      ...and Reads Junk characters.

      How are you verifying this? Do you know that data is available to read before calling ReadFile()?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

      P 1 Reply Last reply
      0
      • D David Crow

        pandit84 wrote:

        ...and Reads Junk characters.

        How are you verifying this? Do you know that data is available to read before calling ReadFile()?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

        P Offline
        P Offline
        pandit84
        wrote on last edited by
        #3

        I am printing the ptr using printf statement.

        B D 2 Replies Last reply
        0
        • P pandit84

          I am printing the ptr using printf statement.

          B Offline
          B Offline
          barneyman
          wrote on last edited by
          #4

          if you're doing this between two computers, you are using a null-modem cable?

          P 1 Reply Last reply
          0
          • P pandit84

            I am printing the ptr using printf statement.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            And that looks like?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

            P 1 Reply Last reply
            0
            • B barneyman

              if you're doing this between two computers, you are using a null-modem cable?

              P Offline
              P Offline
              pandit84
              wrote on last edited by
              #6

              I have not yet tried this on two computers. I am using only one computer to test.

              B 1 Reply Last reply
              0
              • D David Crow

                And that looks like?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                P Offline
                P Offline
                pandit84
                wrote on last edited by
                #7

                "═══════════════════════════════════════²²²²"

                D 1 Reply Last reply
                0
                • P pandit84

                  I have not yet tried this on two computers. I am using only one computer to test.

                  B Offline
                  B Offline
                  barneyman
                  wrote on last edited by
                  #8

                  I doubt very much the COM redirector understands how to loopback i.e. You can't read and write on the same COM port on the same computer, and if you have a machine with two COM ports, you still need a null modem cable between them

                  modified on Wednesday, May 18, 2011 4:31 AM

                  1 Reply Last reply
                  0
                  • P pandit84

                    "═══════════════════════════════════════²²²²"

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    That's what your printf() statement looks like?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                    P 1 Reply Last reply
                    0
                    • D David Crow

                      That's what your printf() statement looks like?

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                      P Offline
                      P Offline
                      pandit84
                      wrote on last edited by
                      #10

                      oops I am sorry

                      printf("The date %s", ptr) ;

                      D 1 Reply Last reply
                      0
                      • P pandit84

                        oops I am sorry

                        printf("The date %s", ptr) ;

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Ok, so are you using communications events?

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                        P 1 Reply Last reply
                        0
                        • D David Crow

                          Ok, so are you using communications events?

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                          P Offline
                          P Offline
                          pandit84
                          wrote on last edited by
                          #12

                          No I am not using communication event. I have tried using

                          ::WaitCommEvent

                          . But it never get any event and it goes into continuous wait state. So I have removed that and tried to read the Port using ::ReadFile () without using any event.

                          D 1 Reply Last reply
                          0
                          • P pandit84

                            Hi , I have created sample application which opens the serial COM Port and tried to write and Read the data. I have used CreateFile () method to open the COM1.

                            hCom = CreateFile (pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0,NULL );

                            Then I set the COM state

                             GetCommState( hCom, &lpCC.dcb); 
                             lpCC.dcb.BaudRate = CBR\_9600;     //  baud rate
                             lpCC.dcb.StopBits = ONESTOPBIT;    //  stop bit	
                             lpCC.dcb.ByteSize = 8;             //  data size, xmit and rcv		 
                             lpCC.dcb.Parity   = NOPARITY;      //  parity bit
                             lpCC.dcb.fBinary = TRUE ;
                             lpCC.dcb.fParity = TRUE ;
                                  SetCommState( hCom, &lpCC.dcb ); 
                            

                            I am trying to write the data using

                            WriteFile (hCom,(LPCVOID)ptr,20,&byteswritten,NULL);

                            and trying to read the data using

                            ReadFile (hCom, (LPVOID)ptr, 20, &dwBytesTransferred, 0);

                            ptr is pointer to the BYTE array. The above code of "ReadFiles() returns true and Reads Junk characters. I have written exact 20 characters through WriteFile() and it returned TRUE . I am not able to figure out the exact problem . Please provide any input in this regards. Thanks

                            K Offline
                            K Offline
                            kakan
                            wrote on last edited by
                            #13

                            You just got to use overlapped I/O when you are using COM ports. There are some classes/samples here. Just search for a suitable article.

                            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                            P 1 Reply Last reply
                            0
                            • P pandit84

                              No I am not using communication event. I have tried using

                              ::WaitCommEvent

                              . But it never get any event and it goes into continuous wait state. So I have removed that and tried to read the Port using ::ReadFile () without using any event.

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              pandit84 wrote:

                              . But it never get any event and it goes into continuous wait state.

                              Which means nothing is available to read.

                              pandit84 wrote:

                              So I have removed that and tried to read the Port using ::ReadFile () without using any event.

                              So reading what's not available (to read) seemed like a better idea?

                              "One man's wage rise is another man's price increase." - Harold Wilson

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                              1 Reply Last reply
                              0
                              • K kakan

                                You just got to use overlapped I/O when you are using COM ports. There are some classes/samples here. Just search for a suitable article.

                                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                                P Offline
                                P Offline
                                pandit84
                                wrote on last edited by
                                #15

                                Finally this solution works for me. What I did is 1. In ReadFile() - Read from different COM port. E.G Writing is done on COM1 and Reading is Done on COM2. As I was having only 1 COM Port . I installed Virtual Port Software on my machine and Paired COM1 and COM2 2. ReadFile() provided a buffer of exact size which I used in WriteFile () method.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups