?
Solved

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

Posted on 2005-03-14
5
Medium Priority
?
223 Views
Last Modified: 2010-05-02
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
Comment
Question by:doctorspears
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 300 total points
ID: 13535559
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
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 300 total points
ID: 13535568
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13535571
Ooops
0
 

Author Comment

by:doctorspears
ID: 13544791
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13545134
should use


Print #i, strInput;


instead
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question