Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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


0
doctorspears
Asked:
doctorspears
  • 3
2 Solutions
 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now