Results 1 to 6 of 6

Thread: Sending a file via COM PORT

  1. #1

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Sending a file via COM PORT

    I am working on COM PORT( SERIAL PORT)
    Someone have example about sending file via COM please show me, THanks.
    I have only finished about sending string via COM.

    appreciate
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Sending a file via COM PORT

    You just read the file into a byte array. If the file is relatively small, use io.file.readallbytes(path) to read it. If the file is large, use a filestream and read it in small blocks to a buffer (byte array) and keep looping until the whole file is read. Once you have the byte array, you just call serialport.write(bytearray, 0, bytearray.length) to send it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Sending a file via COM PORT

    Stan's answer is exactly correct.

    However, sometimes, binary file transfers are done using an error-checking file transfer protocol. The need for this is very small if you are using a direct connection (though some devices may employ one anyway). For transfer over a distance, error-detection and correction may be needed. You can download a .NET dll (XMCommCRC) from my homepage that implements XMODEM CRC/checksum error detection/correction. It is free. See the Software Downloads link from the page listed under my profile.
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  4. #4

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Sending a file via COM PORT

    Thanks your help and i used way converting image to base64string format.


    Code:
        Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
            Using ms As New MemoryStream()
                ' Convert Image to byte[]
                image.Save(ms, format)
                Dim imageBytes As Byte() = ms.ToArray()
    
                ' Convert byte[] to Base64 String
                Dim base64String As String = Convert.ToBase64String(imageBytes)
                Return base64String
            End Using
    
        End Function
    
        Public Function Base64ToImage(ByVal base64String As String) As Image
            ' Convert Base64 String to byte[]
            Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
            Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
    
            ' Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length)
            Dim image__1 As Image = Image.FromStream(ms, True)
            Return image__1
        End Function
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  5. #5
    New Member
    Join Date
    May 2018
    Posts
    1

    Re: Sending a file via COM PORT

    Does anyone have an example .sln for Visual Studio 2008 or newer? I am brand new to Visual Studio and would benefit very much from an example solution and all other source code files. Can someone please share an example with an exe form with a button I can press that will start a XMODEM-1k send with a progress bar? I program mobile PLCs with structured text CoDeSys and Android Apps etc and have no experience with VB.Net.

    If all the example is a basic form with one button that uses the above mentioned library and works, that's great! I just do not know how to put that together currently. With a working example I am confident I can figure out how to customize it to my needs with trial and error.

    Yes, I know XMODEM-1k is ancient, but I have a brand new mobile PLC using it for firmware file downloading. Any help with this would be greatly appreciated. Thank you

  6. #6
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Sending a file via COM PORT

    Yes, I have code that you can use as a starting point in my book, Visual Basic Programmer's Guide to Serial Communications 5. You can find a purchase link (click Books) on my web site, www.hardandsoftware.net. The XMODEM code examples that I show, in a number of variations, are standard 128 byte packet structures, with both checksum and CRC error correction. Modifying to 1K (1024) byte packets "should" be as simple as changing a couple of lines of code. However, XMODEM-1K was never very popular or "standard." So, some fiddling may be needed. If you purchase directly from me, you receive the book (eBook about 600 pages) and what I call a CDROM via download with hundreds of source code examples, a number of these involve error-corrected file transfer protocols, with XMODEM receiving the most attention; it was the most popular -- though, not qualitatively, the best such serial protocol.
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width