﻿Imports System.Text

Public Class frmSSmpMode
    Dim StopNum As Integer
    Public Shared S_NotifyCount As Integer

    '===================================================
    ' ダイアログ初期化
    '===================================================
    Private Sub frmSSmpMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------
        ' 表示初期化
        '--------------------------------
        ' Start
        With cboStart
            .Items.Clear()
            .Items.Insert(0, "ExtSig1")
            .Items.Insert(1, "ExtSig2")
            .Items.Insert(2, "ExtSig3")
        End With
        ' Start with
        With cboStartWith
            .Items.Clear()
            .Items.Insert(0, "サンプリングとは無関係に開始")
            .Items.Insert(1, "サンプリングと同時に開始")
        End With
        ' Clock
        With cboClock
            .Items.Clear()
            .Items.Insert(0, "ExtSig1")
            .Items.Insert(1, "ExtSig2")
            .Items.Insert(2, "ExtSig3")
        End With
        ' Stop
        With cboStop
            .Items.Clear()
            .Items.Insert(0, "ExtSig1")
            .Items.Insert(1, "ExtSig2")
            .Items.Insert(2, "ExtSig3")
            .Items.Insert(3, "指定個数転送完了")

        End With
        ' Stop with
        With cboStopWith
            .Items.Clear()
            .Items.Insert(0, "サンプリングとは無関係に停止")
            .Items.Insert(1, "サンプリングと同時に停止")
        End With

        cboStart.SelectedIndex = 1
        cboStartWith.SelectedIndex = 0
        cboClock.SelectedIndex = 0
        cboStop.SelectedIndex = 2
        cboStopWith.SelectedIndex = 0

        StopNum = 1000
        S_NotifyCount = 500
        Form1.S_IsRing = CNTS_WRITE_ONCE


        ' 停止個数を符号無しで表示するため、一度16進文字例変換を経由
        txtStopNumber.Text = Convert.ToUInt32(Hex(StopNum), 16)
        ' 通知を行うサンプリング回数を符号無しで表示するため、一度16進文字例変換を経由
        txtNumberData.Text = Convert.ToUInt32(Hex(S_NotifyCount), 16)

        If Form1.S_IsRing = CNTS_WRITE_ONCE Then
            rdoOnce.Checked = True
            rdoRing.Checked = False
        Else
            rdoOnce.Checked = False
            rdoRing.Checked = True
        End If

        cboStop_SelectedIndexChanged(cboStop, New System.EventArgs())

    End Sub

    '===================================================
    ' OKボタン処理
    '===================================================
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim szBuf As New String("", 100)
        Dim Checked As Boolean
        Dim ExtSig1, ExtSig2, ExtSig3 As Short
        Dim dwStart As Short
        Dim wStartWith As Short
        Dim dwClock As Short
        Dim dwStop As Short
        Dim wStopWith As Short
        Dim ret As Integer
        Dim strMsg As String
        Dim error_string As New StringBuilder("", 256)

        '--------------------------------
        ' 表示データを取得
        '--------------------------------
        ' 停止個数を符号ありで設定するため、一度16進文字例変換を経由
        StopNum = Convert.ToInt32(Hex(Val(txtStopNumber.Text)), 16)
        ' 通知を行うサンプリング回数を符号ありで設定するため、一度16進文字例変換を経由
        S_NotifyCount = Convert.ToInt32(Hex(Val(txtNumberData.Text)), 16)
        Checked = rdoOnce.Checked

        Select Case cboStart.SelectedIndex
            Case 0
                dwStart = CNTS_START_EXTSIG_1
            Case 1
                dwStart = CNTS_START_EXTSIG_2
            Case 2
                dwStart = CNTS_START_EXTSIG_3
        End Select

        Select Case cboStartWith.SelectedIndex
            Case 0
                wStartWith = CNTS_START_CNT_NOT_WITH_SAMP
            Case 1
                wStartWith = CNTS_START_CNT_WITH_SAMP
        End Select

        Select Case cboClock.SelectedIndex
            Case 0
                dwClock = CNTS_CLOCK_EXTSIG_1
            Case 1
                dwClock = CNTS_CLOCK_EXTSIG_2
            Case 2
                dwClock = CNTS_CLOCK_EXTSIG_3
        End Select

        Select Case cboStop.SelectedIndex
            Case 0
                dwStop = CNTS_STOP_EXTSIG_1
            Case 1
                dwStop = CNTS_STOP_EXTSIG_2
            Case 2
                dwStop = CNTS_STOP_EXTSIG_3
            Case 3
                dwStop = CNTS_STOP_NUM
        End Select

        Select Case cboStopWith.SelectedIndex
            Case 0
                wStopWith = CNTS_STOP_CNT_NOT_WITH_SAMP
            Case 1
                wStopWith = CNTS_STOP_CNT_WITH_SAMP
        End Select

        If Checked = True Then
            Form1.S_IsRing = CNTS_WRITE_ONCE
        Else
            Form1.S_IsRing = CNTS_WRITE_RING
        End If

        '--------------------------------
        ' サンプリング条件を設定
        '--------------------------------
        ExtSig1 = 1
        ExtSig2 = 1
        ExtSig3 = 1
        ret = CntSetSlave(Form1.S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSlave ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            Exit Sub
        End If

        ret = CntSetSamplingStartTrigger(Form1.S_Id, dwStart, wStartWith, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            Exit Sub
        End If
        ret = CntSetSamplingClockTrigger(Form1.S_Id, dwClock)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            Exit Sub
        End If
        ret = CntSetSamplingStopTrigger(Form1.S_Id, dwStop, wStopWith, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
            Exit Sub
        End If
        ret = CntSetSamplingStopNumber(Form1.S_Id, StopNum)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            strMsg = String.Format("CntSetSamplingStopNumber ret = {0:d}: {1}", ret, error_string)
            MsgBox(strMsg, MsgBoxStyle.OkOnly, Nothing)
        End If

        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 cboStop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboStop.SelectedIndexChanged
        Dim szBuf As New String("", 100)

        szBuf = cboStop.Text
        If StrComp(szBuf, "指定個数転送完了") = 0 Then
            txtStopNumber.Enabled = True
        Else
            txtStopNumber.Enabled = False
        End If

    End Sub

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