|
分類:[.NET 全般]
環境 os:windows7-pro vs2013
シリアルの受信のみを行い受信したデータをCSVファイルに書き込むソフトを作成しております 受信データは100BYTE程度のデータが1秒当たり6回送られてきます 受信データが欠落してしまい困っています ラインモニタで確認するとデータはきています
ResultReadはスレッドから呼び出されます スレッドは10msで動かしています
データを取りこぼさない方法をご指導お願いします
設定 SerialPort1.PortName = 1 SerialPort1.BaudRate = 19200 SerialPort1.DataBits = 8 SerialPort1.Parity = 0 SerialPort1.StopBits = 1 SerialPort1.Handshake = IO.Ports.Handshake.None SerialPort1.WriteBufferSize = 8192 'SerialPort1.ReadBufferSize = 4096 SerialPort1.ReadBufferSize = 4096 SerialPort1.ReadTimeout = 3000 SerialPort1.WriteTimeout = 3000 SerialPort1.RtsEnable = False SerialPort1.DtrEnable = False
Public Function ResultRead() As Boolean
Dim strReturnData As String = "" Dim intRet As Integer = 0 Dim intCsvSaveFlag As Integer = 0 Try
'実績確認 Try
intRet = ReadCommand(strReturnData)
If intRet = 1 Then 'ファイルに保存 MakeCsv(strReturnData) ElseIf intRet = -1 Then
End If
Catch ex As Exception Return -1 End Try
End Function
Public Function ReadCommand(ByRef strReturnData As String) As Integer
Try
strReceiveData = ""
AddHandler SerialPort1.DataReceived, AddressOf SerialPort1_DataReceived
ReceiveDone.Reset()
ReceivePoint:
'受信待ち ReceiveDone.WaitOne(1000) ReceiveDone.Reset() 'ReceiveDone.Dispose()
'受信なし If strReceiveData.Length = 0 Then Return 0 Else 'Debug.Print(strReceiveData) End If
If strReceiveData.Length >= 2 Then strReturnData = MidB(strReceiveData, 2, strReceiveData.Length - 1) End If
Catch ex As Exception Return -1 Finally RemoveHandler SerialPort1.DataReceived, AddressOf SerialPort1_DataReceived End Try
Return 1
End Function
Public Shared Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim dev As SerialPort Dim intRetry As Integer = 0
Try
dev = DirectCast(sender, SerialPort)
If e.EventType = SerialData.Chars Then
Try
'ETXまで受信 dev.Encoding = System.Text.Encoding.GetEncoding("SHIFT-JIS") strReceiveData = dev.ReadTo(Chr(3)) 'Debug.Print(strReceiveData) If strReceiveData <> String.Empty Then If ReceiveDone IsNot Nothing Then ReceiveDone.Set() End If End If
Catch ex As Exception strReceiveData = String.Empty End Try
End If
Catch ex As Exception
End Try
End Sub
|