シリアル通信の受信タイミングを自分で決めるには?

解決


乗換え中  2008-03-18 19:04:10  No: 144543  IP: 192.*.*.*

今回VB2005でシリアル通信を行おうとおもい、サイトなど見よう見まねで以下のようなプログラムを作成しました。
OSはXPです。通信相手はPLCで上位通信を行っています。

実際は送信コマンドを投げるたびに、受信レスポンスを確認し、正常なレスポンスを受信した際に次のコマンドを送信したいのですが…
このコードだと、1000回いっきに送信した後、1000回いっきに受信してしまいます。。


'FormがLoadされたとき
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '通信のデフォルト設定
        SerialPort1.PortName = "COM1"
        SerialPort1.BaudRate = 9600
        SerialPort1.Parity = IO.Ports.Parity.Even
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.NewLine = Chr(13)

        Call SerialPort1.Open()

        txt_Message.Text = "送信コマンドを入力してください!!"

    End Sub

'シリアル通信イベント処理を実行するために必要な定義文
    Delegate Sub AddDataDelegate(ByVal str As String)

    Private Sub AddData(ByVal str As String)
        txt_Message.Text = str
    End Sub

'コマンドを受信した場合の処理
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim strReceiveCommand As String = ""  '受信コマンドを格納する変数
        Dim add As New AddDataDelegate(AddressOf AddData)

        Try
            strReceiveCommand = SerialPort1.ReadLine

            If strReceiveCommand.Contains("E") = True Then

                MessageBox.Show("エラーレスポンスを取得しました。処理を中断します!!", "処理中断", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txt_Message.Invoke(add, strReceiveCommand)
                bError = True
                Exit Sub

            End If

        Catch ex As Exception
            strReceiveCommand = ex.Message
        End Try

        txt_Message.Invoke(add, strReceiveCommand)
    End Sub

'デモ実行ボタンが押された場合の処理
    Private Sub btn_Demo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Demo.Click

        Dim i As Integer = 0         'インクリメント変数
        Dim sCommand As String = ""  '送信コマンドを格納しておく変数
        Dim bSet As Boolean = True   '強制SET_RESET切り替え用変数
        Dim strReceiveCommand As String = ""  '受信コマンドを格納する変数
        Dim add As New AddDataDelegate(AddressOf AddData)

        bError = False

        'ON_OFF処理500回繰り返す
        For i = 0 To 999

            '送信コマンドの格納
            If bSet = True Then
                sCommand = "ST 00500"
            Else
                sCommand = "RS 00500"
            End If

            Try
                '送信コマンドの送信
                SerialPort1.WriteLine(sCommand)
            Catch ex As Exception
                txt_Message.Text = ex.Message
            End Try

            'エラーレスポンスを受信した場合、処理中断
            If bError = True Then
                Exit Sub
            End If

            '強制SET_RESET信号切り替え
            If bSet = True Then
                bSet = False
            Else
                bSet = True
            End If
        Next
        txt_Message.Text = "デモ処理終了!!"
    End Sub

編集 削除
(報告)  2008-03-18 19:09:27  No: 144544  IP: 192.*.*.*

[マルチポストについて]
http://www.ippo.ne.jp/g/53.html

http://hanatyan.sakura.ne.jp/vbnetbbs/wforum.cgi?mode=allread&no=7210&page=0

編集 削除
我龍院  2008-03-19 07:33:51  No: 144545  IP: 192.*.*.*

SerialPort1.NewLineが受信データーの終わりとなっているのなら、
単純に、送受信が交互になるようなフラグをForrm1レベルで設定すれば
良いだけでは。

編集 削除
乗換え中  2008-03-19 10:39:25  No: 144546  IP: 192.*.*.*

>我龍院さま

  どうもありがとうございました。
  フラグとLoop処理で簡単にできました。。
  こんなことに気づかなかった自分が恥ずかしい。
  ありがとうございました。

編集 削除