My reasoning is that many USB-Serial adapters are autobaud, and will take any standard rate, auto-setting to the serial side. Note that in the readData routine, I did not set any serial parameters at all, yet it worked anyway.
Start a new project, make a form with 1 TextBox, 1 ComboBox, and three buttons. I've added code to the Snippet to stop the transfer, place the data into the TextBox, and to clear the TextBox.
Give it a try and see if you have any better luck. If this doesn't work, you might want to try setting serial port parameters.
Imports System.Text
Imports System.IO.Ports
Public Class Form1
Private keepgoing As Boolean = True
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each portName In My.Computer.Ports.SerialPortNames
comboPort.Items.Add(portName)
Next
comboPort.Text = comboPort.Items.Item(0)
End Sub
Private Sub readData()
Dim buffer As New StringBuilder
Using comPort = My.Computer.Ports.OpenSerialPort(comboPort.Text)
Do
Dim line = comPort.ReadLine()
If line Is Nothing Then
Exit Do
Else
buffer.AppendLine(line)
TextBox1.Text &= line
End If
Application.DoEvents()
If Not keepgoing Then
comPort.Close()
Exit Do
End If
Loop
End Using
End Sub
Private Sub sendData(txt As String)
Using comPort = My.Computer.Ports.OpenSerialPort("COM1", 2400)
' Using comPort = My.Computer.Ports.OpenSerialPort("COM1", 2400)
comPort.DtrEnable = True
comPort.Write("S" & vbCrLf)
' All data transfer code goes here.
End Using
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
keepgoing = True
readData()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
keepgoing = False
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
TextBox1.Clear()
End Sub
End Class

New Topic/Question
Reply





MultiQuote







|