1 Attachment(s)
[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 ?
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
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:
Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
Dim fs As New IO.FileStream("c:\test.txt", IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim fileBytes() As Byte = br.ReadBytes(CInt(br.BaseStream.Length))
br.Close()
fs.Close()
comPort.Write(fileBytes, 0, fileBytes.Length)
End Using
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")
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.