﻿Imports System.Text

Public Class Events

    Dim OutputEvent As Integer = CNT_OEVENT_GENOUT
    Dim RF0 As Integer = CNT_INP_NONE
    Dim RF1 As Integer = CNT_INP_NONE
    Dim PresetReg As Integer = &H1000S


    '===================================================
    ' ダイアログ初期化
    '===================================================
    Private Sub frmEvent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
        '===================================================
        ' コンボボックス初期化
        '===================================================
        '信号入力イベント
        With cboInputEvent
            .Items.Clear()
            .Items.Insert(0, "汎用入力")
            .Items.Insert(1, "カウンタ 開始/停止")
            .Items.Insert(2, "カウンタ プリセット")
            .Items.Insert(3, "カウンタ クリア")
        End With

        'カウンタイベント
        With cboCount
            .Items.Clear()
            .Items.Insert(0, "なし")
            .Items.Insert(1, "プリセット")
            .Items.Insert(2, "クリア")
        End With

        'RF0 立ち上がり/立下り
        With cboRF0
            .Items.Clear()
            .Items.Insert(0, "なし")
            .Items.Insert(1, "立ち上がり")
            .Items.Insert(2, "立ち下り")
        End With

        'RF1 立ち上がり/立下り
        With cboRF1
            .Items.Clear()
            .Items.Insert(0, "なし")
            .Items.Insert(1, "立ち上がり")
            .Items.Insert(2, "立ち下り")
        End With

        'パルス幅
        With cboPulseLength
            .Items.Clear()
            .Items.Insert(&H0S, "0")
            .Items.Insert(&H1S, "1")
            .Items.Insert(&H2S, "2")
            .Items.Insert(&H3S, "3")
            .Items.Insert(&H4S, "4")
            .Items.Insert(&H5S, "5")
            .Items.Insert(&H6S, "6")
            .Items.Insert(&H7S, "7")
            .Items.Insert(&H8S, "8")
            .Items.Insert(&H9S, "9")
            .Items.Insert(&HAS, "A")
            .Items.Insert(&HBS, "B")
            .Items.Insert(&HCS, "C")
            .Items.Insert(&HDS, "D")
            .Items.Insert(&HES, "E")
            .Items.Insert(&HFS, "F")
        End With

        cboInputEvent.SelectedIndex = 0
        cboCount.SelectedIndex = 0
        cboRF0.SelectedIndex = 2
        cboRF1.SelectedIndex = 2
        cboPulseLength.SelectedIndex = 0
        txtPresetReg.Text = "1000"

        '===================================================
        ' データ初期化
        '===================================================
        ' イベント信号入力
        cboInputEvent_SelectedIndexChanged(cboInputEvent, New System.EventArgs())

        'イベント信号出力()
        If OutputEvent = CNT_OEVENT_GENOUT Then
            rdoGenOutput.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = False
            txtChNo.Enabled = True
            txtData.Enabled = True
            btnOutPulse.Enabled = True
        End If
        If OutputEvent And CNT_OEVENT_MATCH0 Then
            rdoCountMatch0.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_MATCH1 Then
            rdoCountMatch1.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_AIERR Then
            rdoAiError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        If OutputEvent And CNT_OEVENT_FLTERR Then
            rdoFilterError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If

        If OutputEvent And CNT_OEVENT_ALMERR Then
            rdoAlarmError.Checked = System.Windows.Forms.CheckState.Checked
            cboPulseLength.Enabled = True
            txtChNo.Enabled = False
            txtData.Enabled = False
            btnOutPulse.Enabled = False
        End If
        txtChNo.Text = "0"
        txtData.Text = "1"
    End Sub

    '---------------------------------------------
    ' 設定完了処理
    '---------------------------------------------
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim dwInputEvent As Integer
        Dim wPulseLength As Short
        Dim dwCountEvent As Integer
        Dim ChNo As Short
        Dim ret As Integer
        Dim str As String
        Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)

        '-------------------------------
        ' 設定情報取得
        '-------------------------------
        ' イベント信号入力
        ' Input Event
        dwInputEvent = cboInputEvent.SelectedIndex
        ' Preset Reg
        ' Preset Reg値を符号ありで設定するため、一度16進文字例変換を経由
        PresetReg = Convert.ToInt32(Hex(Val("&H" & txtPresetReg.Text)), 16)
        ' RF0
        RF0 = cboRF0.SelectedIndex
        ' RF1
        RF1 = cboRF1.SelectedIndex


        ' イベント信号出力
        OutputEvent = CNT_OEVENT_GENOUT

        If rdoCountMatch0.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_MATCH0

        ElseIf rdoCountMatch1.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_MATCH1

        ElseIf rdoAiError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_AIERR

        ElseIf rdoFilterError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_FLTERR

        ElseIf rdoAlarmError.Checked = True Then
            OutputEvent = OutputEvent Or CNT_OEVENT_ALMERR
        End If

        ' パルス幅
        Select Case cboPulseLength.SelectedIndex
            Case 0
                wPulseLength = &H0S
            Case 1
                wPulseLength = &H1S
            Case 2
                wPulseLength = &H2S
            Case 3
                wPulseLength = &H3S
            Case 4
                wPulseLength = &H4S
        End Select
        ' カウント一致イベント
        dwCountEvent = cboCount.SelectedIndex

        '-------------------------------
        ' 設定情報をデバイスへセット
        '-------------------------------
        For ChNo = 0 To Form1.ChannelNum - 1
            '---------------------------------------------
            ' 制御入力信号のハードウェアイベント設定
            '---------------------------------------------
            ret = CntSetInputHardwareEvent(Form1.Id, ChNo, dwInputEvent, RF0, RF1, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetInputHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
            '---------------------------------------------
            ' プリセットレジスタの設定
            '---------------------------------------------
            If dwInputEvent = CNT_IEVENT_PRESET Or dwCountEvent = CNT_MATCH_PRESET Then
                ret = CntSetPresetRegister(Form1.Id, ChNo, PresetReg, 0)
                If ret <> 0 Then
                    CntGetErrorString(ret, error_string)
                    str = String.Format("CntSetPresetRegister : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                    MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                    GoTo ERR_END
                End If
            End If
            '---------------------------------------------
            ' 制御出力信号のハードウェアイベント設定
            '---------------------------------------------
            ret = CntSetOutputHardwareEvent(Form1.Id, ChNo, 0, OutputEvent, wPulseLength)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetOutputHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
            '---------------------------------------------
            ' カウント一致のハードウェアイベント設定
            '---------------------------------------------
            ret = CntSetCountMatchHardwareEvent(Form1.Id, ChNo, 0, dwCountEvent, 0)
            If ret <> 0 Then
                CntGetErrorString(ret, error_string)
                str = String.Format("CntSetCountMatchHardwareEvent : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
                MsgBox(str, MsgBoxStyle.OkOnly, "Error")
                GoTo ERR_END
            End If
        Next ChNo
ERR_END:
        Me.Hide()
    End Sub

    '---------------------------------------------
    ' キャンセル処理
    '---------------------------------------------
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    '---------------------------------------------
    ' イベント信号入力 設定
    '---------------------------------------------
    Private Sub cboInputEvent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboInputEvent.SelectedIndexChanged

        If cboInputEvent.Text = "汎用入力" Then
            cboRF0.SelectedIndex = 0
            cboRF1.SelectedIndex = 0
            cboRF0.Enabled = False
            cboRF1.Enabled = False
            txtPresetReg.Enabled = False
        End If
        If cboInputEvent.Text = "カウンタ 開始/停止" Then
            cboRF0.Enabled = True
            cboRF1.Enabled = True
            txtPresetReg.Enabled = False
        End If
        If cboInputEvent.Text = "カウンタ プリセット" Then
            cboRF0.Enabled = True
            cboRF1.SelectedIndex = 0
            cboRF1.Enabled = False
            txtPresetReg.Enabled = True
        End If
        If cboInputEvent.Text = "カウンタ クリア" Then
            cboRF0.Enabled = True
            cboRF1.SelectedIndex = 0
            cboRF1.Enabled = False
            txtPresetReg.Enabled = False
        End If
        If cboCount.Text = "プリセット" Then
            txtPresetReg.Enabled = True
        End If
    End Sub

    Private Sub cboCount_Selendok(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoGenOutput.Click
        cboInputEvent_SelectedIndexChanged(cboInputEvent, New System.EventArgs())
    End Sub

    '---------------------------------------------
    ' イベント信号入力 設定
    '---------------------------------------------
    Private Sub rdoGenOutput_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoGenOutput.Click

        cboPulseLength.Enabled = False
        txtChNo.Enabled = True
        txtData.Enabled = True
        btnOutPulse.Enabled = True

    End Sub

    Private Sub rdoCountMatch0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoCountMatch0.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoCountMatch1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoCountMatch1.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoAiError_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoAiError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoFilterError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoFilterError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    Private Sub rdoAlarmError_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoAlarmError.Click

        cboPulseLength.Enabled = True
        txtChNo.Enabled = False
        txtData.Enabled = False
        btnOutPulse.Enabled = False

    End Sub

    '---------------------------------------------
    ' 信号出力 処理
    '---------------------------------------------
    Private Sub btnOutPulse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutPulse.Click

        Dim ChNo As Short
        Dim Data As Byte
        Dim ret As Integer
        Dim str As String
        Dim buf As New String("", 100)
        Dim error_string As New StringBuilder("", 256)

        ChNo = Val(txtChNo.Text)
        Data = CByte(txtData.Text)
        '---------------------------------------------
        ' 汎用出力処理
        '---------------------------------------------
        ret = CntOutputDOBit(Form1.Id, ChNo, 0, Data)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntOutPulse : ret = {0:D}" & vbCrLf & error_string.ToString(), ret)
            MsgBox(str, MsgBoxStyle.OkOnly, "Error")
        End If
    End Sub

    Private Sub frmEvent_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class