' form level variables
Private filelines As List(Of String) = New List(Of String)
Private com As String = "COM3"
Private Sub btnTransmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransmit.Click
' declare stream and streamreader
Dim fs As IO.FileStream = Nothing
Dim sr As IO.StreamReader = Nothing
' clear the List of strings
filelines.Clear()
' reset the Progress bar
ProgressBar1.Value = 0
Try
' displays the OpenFileDialog1 to the user
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
' defines sr as a FileStream and opens the filename from the OpenFileDialog1
fs = New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
' reads the sr filestream ino the stream reader (sr)
sr = New System.IO.StreamReader(fs)
' read each line of the file into the list of strings
Dim line As String
Do
line = sr.ReadLine
If line IsNot Nothing Then
filelines.Add(line)
End If
Loop Until line Is Nothing
' configure progressbar parameters
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = filelines.Count
' start background worker to perform COM port write operation
BackgroundWorker1.RunWorkerAsync()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
' close the stream and reader
sr.Close()
fs.Close()
End Try
End Sub
Private Sub btnCancelTransmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelTransmit.Click
' cancel transmit operation
If BackgroundWorker1.IsBusy Then
BackgroundWorker1.CancelAsync()
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
' sets up comPort as SerialPort and opens the port
Using comport As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(com)
' setup the com ports
comport.DtrEnable = True
comport.RtsEnable = True
comport.BaudRate = 9600
comport.DataBits = 7
comport.Parity = System.IO.Ports.Parity.Even
comport.StopBits = System.IO.Ports.StopBits.One
comport.Handshake = System.IO.Ports.Handshake.None
' explicitly set Carriage return and Line feed as the end of line terminator
comport.NewLine = Convert.ToChar(&HD) & Convert.ToChar(&HA)
' set up counter variable
Dim i As Integer = 0
' writes each line of data to the comPort with pause after each block
For Each str As String In filelines
' check if operation has been cancelled
If worker.CancellationPending Then
e.Cancel = True
Exit For
Else
comport.WriteLine(str)
System.Threading.Thread.Sleep(20) ' 20 = 20 milliseconds
' increment counter for each line transmitted
i += 1
' report counter value to the ProgressChanged event
worker.ReportProgress(0, i)
End If
Next str
End Using
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
' update Progress bar with current reported value
Me.ProgressBar1.Value = DirectCast(e.UserState, Integer)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
' reset the Progress bar
ProgressBar1.Value = 0
If (Not e.Error Is Nothing) Then
MessageBox.Show(e.Error.Message, "CNC transmission")
ElseIf e.Cancelled Then
MessageBox.Show("Cancelled!", "CNC transmission")
Else
MessageBox.Show("Completed!", "CNC transmission")
End If
End Sub