連續接收二進位檔案的寫法 (可避免程式沒收到值而當掉)
======================================================
Dim NN = 0
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
NN = NN + 1
TextBox2.Text = NN.ToString + "," + SerialPort1.BytesToRead.ToString
Dim sss_c1
Dim all As String = " "
Dim buffer As Byte()
Dim n_all = SerialPort1.BytesToRead
ReDim buffer(n_all - 1)
If n_all > 0 Then '可找目前接收緩衝區中有多少個BYTE
sss_c1 = SerialPort1.Read(buffer, 0, n_all)
all = all + sss_c1.ToString
sw_b1.Write(buffer)
sw_b1.Flush()
End If
'-------------------------
TextBox1.Text = all.ToString
End Sub