Results 1 to 5 of 5

Thread: [2005]How to copy a file to a COM port in VB?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    [2005]How to copy a file to a COM port in VB?

    Hi Guys,

    I am a novice Developer, and have only just completed a pretty basic course in VB.net.

    I am trying to do a straightforward file copy.

    I want to take a file, eg c:\test.txt
    and copy it straight to a com port eg COM1

    This works well in DOS and I suppose I can do it using Shell if absolutely necessary, but I would like to do it in VB.

    I have managed to get the file to copy perfectly, using

    FileCopy ("c:\test.txt", "COM1")

    I am monitoring my COM port using Free Serial Port Monitor http://www.serial-port-monitor.com/index.html, and the data is going through the same as if you do a DOS copy;
    (eg: copy c:\test.txt com1:)

    But VB crashing out with an error when it has sent the file, as per the screen shot below.

    Does anyone have any ideas ?
    Attached Images Attached Images  
    Last edited by danweb; Jun 15th, 2007 at 07:14 AM.

  2. #2
    Addicted Member joe1985's Avatar
    Join Date
    Jun 2007
    Posts
    151

    Re: [2005]How to copy a file to a COM port in VB?

    What is the full path of comPort? If you go into my computer can you see it? If so what drive letter does it take up. Can you view files on comport from MS-DOS.

    This simply means you are not referencing the path correctly.

    - Joe

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005]How to copy a file to a COM port in VB?

    I dont think its as simple as just copying the file to the port, besides FileCopy (which shouldnt be used in .Net, there are better alternatives in the System.IO namespace) only accepts filesystem paths. Try this code, it reads the content of the file into a byte array and writes it to COM1

    VB.Net Code:
    1. Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    2.             Dim fs As New IO.FileStream("c:\test.txt", IO.FileMode.Open)
    3.             Dim br As New IO.BinaryReader(fs)
    4.             Dim fileBytes() As Byte = br.ReadBytes(CInt(br.BaseStream.Length))
    5.             br.Close()
    6.             fs.Close()
    7.             comPort.Write(fileBytes, 0, fileBytes.Length)
    8.         End Using
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    Re: [2005]How to copy a file to a COM port in VB?

    That seems to be EXACTLY what I want. I did actually manage to get my other code working too, by catching the system error. But it doesn't seem a particularly "graceful" way of doing it.
    Whereas your suggestion seems more graceful, and also appears to be quicker at sending the data.

    Sothank you ever so much for your help;

    Oh and just an aside, I had to change the first line of code slightly to get it to work, it now reads;
    Using comPort As Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")

  5. #5
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: [2005]How to copy a file to a COM port in VB?

    Ok i need to do that same as DanWeb here, however I really dont know the correct method for receiving thus file. I guess what i would like to do is send and receive files via COM1.

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