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. Binary data transfer through serail port

Binary data transfer through serail port

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
20 Posts 7 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.
  • S Offline
    S Offline
    shiv nand
    wrote on last edited by
    #1

    Hi i have to read a binary file from disk and i have to transfer file through serial port. i am using mscomm1 in vc++ 6.0. Please help how to transfer a binary file through serial port.

    C L K 4 Replies Last reply
    0
    • S shiv nand

      Hi i have to read a binary file from disk and i have to transfer file through serial port. i am using mscomm1 in vc++ 6.0. Please help how to transfer a binary file through serial port.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      What is your doubt about? Read the file in binary mode and then send the data on the serial line (set the Output property). BTW Why are you using MsComm (instead of Win32 API)? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S 1 Reply Last reply
      0
      • C CPallini

        What is your doubt about? Read the file in binary mode and then send the data on the serial line (set the Output property). BTW Why are you using MsComm (instead of Win32 API)? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        shiv nand
        wrote on last edited by
        #3

        i am working on dialog based application , i need to transfer binary file. any examples or sample code which helps me.

        C D 2 Replies Last reply
        0
        • S shiv nand

          Hi i have to read a binary file from disk and i have to transfer file through serial port. i am using mscomm1 in vc++ 6.0. Please help how to transfer a binary file through serial port.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          for binary data, you have to make sure the driver doesn't interfere with the data at all; so you must check each and every port setting, including: - pass NULL; - don't change CR/LF; - don't enable software handshaking. FWIW: I tend to avoid binary communication over serial ports; when portability is more important than performance, I'd rather do some encoding, typically stuffing 2 bytes in 3 printable characters (i.e. the range 0x20-0x7E), so port settings wouldn't interfere with it at all. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          S 1 Reply Last reply
          0
          • S shiv nand

            i am working on dialog based application , i need to transfer binary file. any examples or sample code which helps me.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Still you're not giving info about your doubts. Are you able to read a binary file? Do you know how to use MsComm or Win32 API for serial communication? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S 1 Reply Last reply
            0
            • L Luc Pattyn

              for binary data, you have to make sure the driver doesn't interfere with the data at all; so you must check each and every port setting, including: - pass NULL; - don't change CR/LF; - don't enable software handshaking. FWIW: I tend to avoid binary communication over serial ports; when portability is more important than performance, I'd rather do some encoding, typically stuffing 2 bytes in 3 printable characters (i.e. the range 0x20-0x7E), so port settings wouldn't interfere with it at all. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              S Offline
              S Offline
              shiv nand
              wrote on last edited by
              #6

              hi Luc Pattyn do you have a sample code , so it will help to understood, still i am confuse

              L 1 Reply Last reply
              0
              • S shiv nand

                i am working on dialog based application , i need to transfer binary file. any examples or sample code which helps me.

                D Offline
                D Offline
                Demian Panello
                wrote on last edited by
                #7

                Please refer to: http://msdn.microsoft.com/en-us/library/aa363196(v=VS.85).aspx[^] Regards.

                Demian. "I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone." -Bjarne Stroustrup, computer science professor, designer of C++ programming language (1950- )

                1 Reply Last reply
                0
                • C CPallini

                  Still you're not giving info about your doubts. Are you able to read a binary file? Do you know how to use MsComm or Win32 API for serial communication? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  S Offline
                  S Offline
                  shiv nand
                  wrote on last edited by
                  #8

                  here i will give the example what i am doing. CFile file; CString str; unsigned char ch; if(file.Open("source.bin",CFile::modeRead|CFile::typeBinary)) int nLength = file.GetLength(); for(i=0;i<=nLength;i++) //Transfering file data { file.Read(&ch,1); str.Format ("%c",ch); m_mscom.SetOutput (COleVariant(str)); } is it the right way to transfer a binary file ?. i have to transfer a binary file up to 15MB.

                  C 1 Reply Last reply
                  0
                  • S shiv nand

                    hi Luc Pattyn do you have a sample code , so it will help to understood, still i am confuse

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    No, as I said I avoid doing such things as it is more trouble than its worth most of the time. I haven't done any binary serial comm on Windows using C/C++ in many years; and if I really had to do it today, I'd use C# anyway. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    1 Reply Last reply
                    0
                    • S shiv nand

                      here i will give the example what i am doing. CFile file; CString str; unsigned char ch; if(file.Open("source.bin",CFile::modeRead|CFile::typeBinary)) int nLength = file.GetLength(); for(i=0;i<=nLength;i++) //Transfering file data { file.Read(&ch,1); str.Format ("%c",ch); m_mscom.SetOutput (COleVariant(str)); } is it the right way to transfer a binary file ?. i have to transfer a binary file up to 15MB.

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Well, you don't need to read the file (and write it to the serial port) a character at time (incidentally your loop-exit condition it's wrong, you should use i<nLength). For sending binary data, you need to fill a SafeArray and pack it into a Variant, see, for instance: "Mscomm and mode binary" at codeguru (using MFC OLE wrapper classes really helps here). Again, you may also choose to avoid COM and go with Win32's CreateFile, WriteFile, .., see the link Paniello provided. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      S 1 Reply Last reply
                      0
                      • C CPallini

                        Well, you don't need to read the file (and write it to the serial port) a character at time (incidentally your loop-exit condition it's wrong, you should use i<nLength). For sending binary data, you need to fill a SafeArray and pack it into a Variant, see, for instance: "Mscomm and mode binary" at codeguru (using MFC OLE wrapper classes really helps here). Again, you may also choose to avoid COM and go with Win32's CreateFile, WriteFile, .., see the link Paniello provided. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        S Offline
                        S Offline
                        shiv nand
                        wrote on last edited by
                        #11

                        BYTE *pBuffer = new BYTE[nLength]; COleVariant var; CByteArray barr; file.Read(pBuffer,nLength); for(UINT i = 0; i

                        C D 2 Replies Last reply
                        0
                        • S shiv nand

                          BYTE *pBuffer = new BYTE[nLength]; COleVariant var; CByteArray barr; file.Read(pBuffer,nLength); for(UINT i = 0; i

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #12

                          Why don't you try (start with small amount of data)? You know, sending 15MB of data will take more than 20 minutes even at 115200 baud.

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          S 1 Reply Last reply
                          0
                          • S shiv nand

                            Hi i have to read a binary file from disk and i have to transfer file through serial port. i am using mscomm1 in vc++ 6.0. Please help how to transfer a binary file through serial port.

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

                            Just create a file pointer to the serial port com1/com2. And do operations read file and write file with small chunks of data. And about the UI(User interface ) that u can create using MFC dialog based application. Silly matter only... Thanking you Krish

                            1 Reply Last reply
                            0
                            • C CPallini

                              Why don't you try (start with small amount of data)? You know, sending 15MB of data will take more than 20 minutes even at 115200 baud.

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              S Offline
                              S Offline
                              shiv nand
                              wrote on last edited by
                              #14

                              ok i will try with small file.

                              C 1 Reply Last reply
                              0
                              • S shiv nand

                                BYTE *pBuffer = new BYTE[nLength]; COleVariant var; CByteArray barr; file.Read(pBuffer,nLength); for(UINT i = 0; i

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

                                jiya-123 wrote:

                                for(UINT i = 0; i < sizeof((pBuffer)/sizeof(BYTE)) ; i++)

                                Really? You do realize that loop equates to:

                                for(UINT i = 0; i < 4; i++)

                                "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

                                "Man who follows car will be exhausted." - Confucius

                                1 Reply Last reply
                                0
                                • S shiv nand

                                  Hi i have to read a binary file from disk and i have to transfer file through serial port. i am using mscomm1 in vc++ 6.0. Please help how to transfer a binary file through serial port.

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Chances are the eunuch guarding the serail port won't like binary data. :-D

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                  C 1 Reply Last reply
                                  0
                                  • S shiv nand

                                    ok i will try with small file.

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #17

                                    First of all, see what David correctly pointed out http://www.codeproject.com/Messages/3628586/Re-Binary-data-transfer-through-serail-port.aspx[^]. :)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      Chances are the eunuch guarding the serail port won't like binary data. :-D

                                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                      C Offline
                                      C Offline
                                      Chuck OToole
                                      wrote on last edited by
                                      #18

                                      Generally, there's no problem sending binary data through a serial port if you are sure both ends are using full transparancy of the data, allow NULLs, CR/LF expansion (as mentioned by someone else above). XOFF and XON and EOF also need to be transparent. The real issue is what do you do about "transmission errors"? There's no guarantee that serial data gets to the other side without problems, dropped bits, character framing errors. So, typically you have to wrap the data with some higher level checking, CRC, etc to ensure proper transmission of the data. Of course, since corruption is detected on the receiving side, you then need to tell the transmitter to re-send the data stream. Wait, this is beginning to sound a lot like a complete protocol.....

                                      L 1 Reply Last reply
                                      0
                                      • C Chuck OToole

                                        Generally, there's no problem sending binary data through a serial port if you are sure both ends are using full transparancy of the data, allow NULLs, CR/LF expansion (as mentioned by someone else above). XOFF and XON and EOF also need to be transparent. The real issue is what do you do about "transmission errors"? There's no guarantee that serial data gets to the other side without problems, dropped bits, character framing errors. So, typically you have to wrap the data with some higher level checking, CRC, etc to ensure proper transmission of the data. Of course, since corruption is detected on the receiving side, you then need to tell the transmitter to re-send the data stream. Wait, this is beginning to sound a lot like a complete protocol.....

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #19

                                        I've been there. And it might be easier/better to install SLIP and use FTP then. Unfortunately the OP never mentioned what the other side was, in fact he didn't give any context. BTW: The transparency reply was mine also, this one was a reaction on the typo in the subject line. :)

                                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                        C 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          I've been there. And it might be easier/better to install SLIP and use FTP then. Unfortunately the OP never mentioned what the other side was, in fact he didn't give any context. BTW: The transparency reply was mine also, this one was a reaction on the typo in the subject line. :)

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                          C Offline
                                          C Offline
                                          Chuck OToole
                                          wrote on last edited by
                                          #20

                                          Oh, a "serail port". Isn't that where they take containers directly off the ship and put them on trains (or visa-versa). ;)

                                          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