Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open serial port and write data to file (MSComm)

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
Hi all,

I'm not a VB person, but I am trying to write a small standalone EXE that opens a serial port and continusously writes the incoming information to a file. I think I can probably do this with MS Comm Control, but as i am not all that familiar with VB, I would appreciate some help.

Thanks,

CzarJ

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
I understand how to set up and open the com port as listed below, but how do I recieve the data and put it into a file?

Thanks

Code:
MSComm1.CommPort = 2
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
You need to use the Comm control's OnComm event. This event will be fired whenever anything interesting happens to the COM port, including receiving data. Just make sure that the .RThreshold property is set to something greater than zero (I usually use 1), so that OnComm will be generated when data is recieved.

In your OnComm event procedure, you just use the .Input property to move the data recieved by the COM port into a string and then use your method of choice to write that string to a file. I prefer to use TextStream object for textfile I/O, but that's just me.
 
I've managed to get the serial port to open and write to a textbox on the screen, but I can't get it to write to my file. can anyone see anything wrong with my code below. Note, there are no error messages

Code:
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 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")
        
    End Select
  End With 'MSComm1
End Sub


Public Sub WriteFile(FileName As String)
    Dim i As Integer
    i = FreeFile
    Open FileName For Output As #i
    Print #i, strInput
    Close #i
End Sub


Thanks

~CzarJ

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Look at your WriteFile routine. It opens the file for output and you're calling it on every OnComm event. I believe opening a file in output mode causes the current contents to be overwritten. Try opening the file in Append mode instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top