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 TugboatEng on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Data from Serial Port

Status
Not open for further replies.

chath259

Aerospace
Joined
Oct 11, 2006
Messages
9
Location
US
Hey,

I'm pretty new at VB and programming in general, but I don't think my question is too hard of one.

I want to be able to receive data from a serial port and write that data to a text file until I either tell it to stop or the connection is gone. Now I've never programmed anything to read data from an outside source so this is new ground for me.

So, any help as far as structure and code goes would be greatly appreciated.

Thanks!


 
Here is the code I have so far....unfortunately I don't have anything to test this code with (nothing in serial port). I've put the MSComm object into the form...so I guess my question now is...to the best of your knowledge, will this work for the above described situation?




Private Sub cmdClose_Click()

MSComm1.PortOpen = False
lblStatus.Caption = "Closed"
lblStatus.ForeColor = &HFF&

End Sub

Private Sub cmdOpen_Click()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
lblStatus.Caption = "Open"
lblStatus.ForeColor = &HC000&

End Sub


Private Sub cmdReceive_Click()
Dim Data As Variant
Do
DoEvents
Loop Until MSComm1.InBufferCount > 0
Data = MSComm1.Input
Open "C:\Data.txt" For Append As #1
Write #1, Data
Close #1
End Sub
 
Ooops the Receive button code is wrong it should read the following (I think?):

Private Sub cmdReceive_Click()
Dim Data As Variant

Do

Data = MSComm1.Input
Open "C:\Data.txt" For Append As #1
Write #1, Data
Close #1

Loop Until MSComm1.InBufferCount = 0 'As long as there is data in the buffer OR Returns 0 if user clicks "Close Port"

End Sub

 
It seems to be correct.

Can you test it with a null modem and a laptop?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top