serial port rs232 unable to connect

DEzs Desmond

New member
Joined
Oct 12, 2011
Messages
1
Programming Experience
Beginner
Hi everyone, I;m a beginner to wititing vb codes

pls help me or gives some guidance ,I;m unable to open the port. I;m using com1 and the error message shown is " port don't exist"

Module Module1


Sub Main()
Dim Data As String
Data = "Test"


Console.WriteLine("Writing the following data to COM1: " & Data)
SendSerialData(Data)


Console.WriteLine("Read the following data from COM1: " & ReceiveSerialData())


Console.WriteLine("Press ENTER to quit")
Console.ReadLine()


End Sub
Private Sub Form_Load()


MSComm1.RThreshold = 2 ' Fire Rx Event Every Two Bytes
MSComm1.Settings = "9600,N,8,1" ' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.CommPort = 1 ' Open COM1
MSComm1.PortOpen = True


End Sub
Private Sub Form_Unload(ByVal Cancel As Integer)


If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub




Sub SendSerialData(ByVal data As String)
' Send strings to a serial port.
Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1")
com1.WriteLine(data)
com1.Close()
End Using
End Sub


Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""


Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1")
Do
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
com1.Close()
End Using


Return returnStr
End Function


End Module
 
Back
Top