Hello, I am new here, so I apologize if this is not the right place to open this thread.
I wrote a program that uses the following class from that site: VB.NET Tutorials - Serial Port Communication In VB.Net | DreamInCode.net
The problem is that I cannot open a virtual serial port, and the second is that I want to make the communication asynchronous. Are there any ideas?
This is the subroutine which reads from the port:
and this is the subroutine that opens the port:Code:#Region "comPort_DataReceived" ''' <summary> ''' method that will be called when theres data waiting in the buffer ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) 'determine the mode the user selected (binary/string) Select Case CurrentTransmissionType Case TransmissionType.Text 'user chose string 'read data waiting in the buffer Dim msg As String = comPort.ReadExisting() 'display the data to the user _type = MessageType.Incoming _msg = msg DisplayData(MessageType.Incoming, msg + "" + Environment.NewLine + "") Exit Select Case TransmissionType.Hex 'user chose binary 'retrieve number of bytes in the buffer Dim bytes As Integer = comPort.BytesToRead 'create a byte array to hold the awaiting data Dim comBuffer As Byte() = New Byte(bytes - 1) {} 'read the data and store it comPort.Read(comBuffer, 0, bytes) 'display the data to the user _type = MessageType.Incoming _msg = ByteToHex(comBuffer) + "" + Environment.NewLine + "" DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "" + Environment.NewLine + "") Exit Select Case Else 'read data waiting in the buffer Dim str As String = comPort.ReadExisting() 'display the data to the user _type = MessageType.Incoming _msg = str + "" + Environment.NewLine + "" DisplayData(MessageType.Incoming, str + "" + Environment.NewLine + "") Exit Select End Select End Sub #End Region
Code:#Region "OpenPort" Public Function OpenPort() As Boolean Try 'first check if the port is already open 'if its open then close it If comPort.IsOpen = True Then comPort.Close() End If 'set the properties of our SerialPort Object comPort.BaudRate = Integer.Parse(_baudRate) 'BaudRate comPort.DataBits = Integer.Parse(_dataBits) 'DataBits comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits) 'StopBits comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity) 'Parity comPort.PortName = _portName 'PortName 'now open the port comPort.Open() 'display message _type = MessageType.Normal _msg = "Port opened at " + DateTime.Now + "" + Environment.NewLine + "" DisplayData(_type, _msg) 'return true Return True Catch ex As Exception DisplayData(MessageType.[Error], ex.Message) Return False End Try End Function #End Region


LinkBack URL
About LinkBacks




Reply With Quote



Bookmarks