Link to home
Start Free TrialLog in
Avatar of Antrach
Antrach

asked on

copy a file to COM1

How do I copy a file from the hard drive to COM1: in VB6?
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

You need to use the MSCOMM controls.


MSComm control works quite well for this:

Add it to your components and place an instance on the form:

Private Sub Form_Load()
  With MSComm1
    .Settings = "9600,n,8,1"
    .CommPort = 1
    .PortOpen = True
  End With
End Sub

Private Sub Form_Unload()
  MSComm1.PortOpen = False
End Sub

Private Sub cmdSend_Click()
  MSComm1.Output = "Hello this is text out"
End Sub

Private Sub cmdReceive_Click()
  Dim strInput As String
  strInput = MSComm1.Input
  MsgBox strInput
End Sub

This is the simplest method of using the control as you are only sending and receiving on demand. The control has a single event _OnComm() which will allow you to run in an event-driven mode.
So as you can see... to send a file it would be easily accomplished with.


the following code will open a MSComm control and send a text file line by line over comm 1.


With MSComm1
    .Settings = "9600,n,8,1"
    .CommPort = 1
    .PortOpen = True
End With


dim data as string
Open "File.txt" for input as #1
do while eof(1) = false
line input #1,data
MSComm1.Output = data
loop
close #1


MSComm1.PortOpen = False
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or just

shell "copy HardDriveFile com1:"
Avatar of Antrach
Antrach

ASKER

MSComm component worked well sending a text string out the comm port, but did not work attempting to mimic a filecopy command.  The Do-While loop had no effect on the serial printer.