Open a serial port and continuously write incoming data to a file.

I am trying to write a program that will open a serial port and continuously write all the incomming data to a text file. For what I have below, after I run it I don't see any data in the text file (just blank spaces). Any help would be appreciated.

_______________________________________
Private Sub Form_Load()

         Form1.Caption = "Port Monitoring"
         With MSComm1
            .CommPort = 2
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
         End With
         
End Sub

_______________________________________
Private Sub Form_Unload(Cancel As Integer)
   
    MSComm1.PortOpen = False
   
End Sub

_______________________________________
Private Sub MSComm1_OnComm()

  Dim strInput As Integer
  With MSComm1
    'test for incoming event
    Select Case .CommEvent
      Case comEvReceive
        'get data
        strInput = .Input
        Call WriteFile("c:\test.txt")
       
    End Select
  End With 'MSComm1

End Sub

_______________________________________
Public Sub WriteFile(FileName As String)

    Dim i As Byte
    i = FreeFile
    Open FileName For Append As #i
    Print #i, strInput
    Close #i

End Sub


doctorspearsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
Option Explicit 'Place this line at the top of your form code

Option Explicit will force you to declare all your variables before you can use them.

I believe the problem is here - you declared strInput in your OnComm event so it is not seen in the WriteFile function.
You may also want to declare it as String, not Integer.

Public Sub WriteFile(FileName As String)

    Dim i As Byte
    i = FreeFile
    Open FileName For Append As #i
'+++++++++++++++
    Print #i, strInput '<< strInput is not a local or public variable!
'+++++++++++++++
    Close #i

End Sub

To solve the problem, either make the variable Private to the entire module, or pass the value as a parameter to your WriteFile procedure.

Hope it helps!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EDDYKTCommented:
Your strInput variable declare as local variable

better to pass in as arg into writefile

ie

Public Sub WriteFile(FileName As String, strInput as integer)

   Dim i As Byte
   i = FreeFile
   Open FileName For Append As #i
   Print #i, strInput
   Close #i

End Sub


to call

Call WriteFile("c:\test.txt", strInput)
0
EDDYKTCommented:
Ooops
0
doctorspearsAuthor Commented:
Using your suggestions i've gotten this to work pretty well, both writing to the screen and to a text file. The only problem is that the text file has somewhat truncated lines. For example, if the line "equipment failure" were sent, it would write fine on the screen, but in the text file it would look like this:

|equipm
ent fa
ilure

Any suggestions?

------------------------------------------------
Option Explicit

Private Sub Form_Load()

         Form1.Caption = "Physiological Data Logger"
         With MSComm1
            .CommPort = 2
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
         End With
         
End Sub

------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)
   
    MSComm1.PortOpen = False
    MsgBox "Please Note: Your data is located at C:\test.txt", vbNormal, "Get Your Data!"

   
End Sub

------------------------------------------------
Private Sub MSComm1_OnComm()

  Dim strInput As String
  With MSComm1
    'test for incoming event
    Select Case .CommEvent
      Case comEvReceive
        'display incoming event data to displaying textbox
        strInput = .Input
        Text1.SelText = strInput
        'copy incoming event data to file
        Call WriteFile("c:\test.txt", strInput)
       
    End Select
  End With 'MSComm1

End Sub

------------------------------------------------
Public Sub WriteFile(FileName As String, strInput As String)

   Dim i As Byte
   i = FreeFile
   Open FileName For Append As #i
   Print #i, strInput
   Close #i

End Sub
------------------------------------------------
0
EDDYKTCommented:
should use


Print #i, strInput;


instead
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.