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 1 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 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