﻿Imports System.Text

Public Class frmSSmpMode
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents Label16 As System.Windows.Forms.Label
    Friend WithEvents txtNumberData As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents txtStopNumber As System.Windows.Forms.TextBox
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cboStopWith As System.Windows.Forms.ComboBox
    Friend WithEvents cboStop As System.Windows.Forms.ComboBox
    Friend WithEvents cboClock As System.Windows.Forms.ComboBox
    Friend WithEvents cboStartWith As System.Windows.Forms.ComboBox
    Friend WithEvents cboStart As System.Windows.Forms.ComboBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents rdoRing As System.Windows.Forms.RadioButton
    Friend WithEvents rdoOnce As System.Windows.Forms.RadioButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.Label16 = New System.Windows.Forms.Label()
        Me.txtNumberData = New System.Windows.Forms.TextBox()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.txtStopNumber = New System.Windows.Forms.TextBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.cboStopWith = New System.Windows.Forms.ComboBox()
        Me.cboStop = New System.Windows.Forms.ComboBox()
        Me.cboClock = New System.Windows.Forms.ComboBox()
        Me.cboStartWith = New System.Windows.Forms.ComboBox()
        Me.cboStart = New System.Windows.Forms.ComboBox()
        Me.GroupBox3 = New System.Windows.Forms.GroupBox()
        Me.rdoRing = New System.Windows.Forms.RadioButton()
        Me.rdoOnce = New System.Windows.Forms.RadioButton()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnCancel
        '
        Me.btnCancel.Location = New System.Drawing.Point(432, 311)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(112, 28)
        Me.btnCancel.TabIndex = 18
        Me.btnCancel.Text = "Cancel"
        '
        'btnOK
        '
        Me.btnOK.Location = New System.Drawing.Point(304, 311)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(112, 28)
        Me.btnOK.TabIndex = 17
        Me.btnOK.Text = "OK"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label16, Me.txtNumberData})
        Me.GroupBox2.Location = New System.Drawing.Point(8, 245)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(256, 56)
        Me.GroupBox2.TabIndex = 15
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Transfer Number Event Notification"
        '
        'Label16
        '
        Me.Label16.Location = New System.Drawing.Point(32, 23)
        Me.Label16.Name = "Label16"
        Me.Label16.Size = New System.Drawing.Size(80, 19)
        Me.Label16.TabIndex = 24
        Me.Label16.Text = "Number:"
        '
        'txtNumberData
        '
        Me.txtNumberData.Location = New System.Drawing.Point(112, 23)
        Me.txtNumberData.Name = "txtNumberData"
        Me.txtNumberData.Size = New System.Drawing.Size(104, 20)
        Me.txtNumberData.TabIndex = 23
        Me.txtNumberData.Text = "TextBox5"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label6, Me.txtStopNumber, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.cboStopWith, Me.cboStop, Me.cboClock, Me.cboStartWith, Me.cboStart})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(544, 224)
        Me.GroupBox1.TabIndex = 14
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Sampling Mode"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(344, 150)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(80, 19)
        Me.Label6.TabIndex = 26
        Me.Label6.Text = "Stop Number:"
        '
        'txtStopNumber
        '
        Me.txtStopNumber.Location = New System.Drawing.Point(424, 149)
        Me.txtStopNumber.Name = "txtStopNumber"
        Me.txtStopNumber.Size = New System.Drawing.Size(104, 20)
        Me.txtStopNumber.TabIndex = 25
        Me.txtStopNumber.Text = "TextBox5"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 188)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(112, 19)
        Me.Label5.TabIndex = 19
        Me.Label5.Text = "Counter Stop Mode:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 149)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(112, 19)
        Me.Label4.TabIndex = 18
        Me.Label4.Text = "Stop Condition:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 108)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(112, 19)
        Me.Label3.TabIndex = 17
        Me.Label3.Text = "Clock Condition:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 67)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(112, 19)
        Me.Label2.TabIndex = 16
        Me.Label2.Text = "Counter Start Mode:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 28)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(112, 19)
        Me.Label1.TabIndex = 15
        Me.Label1.Text = "Start Condition:"
        '
        'cboStopWith
        '
        Me.cboStopWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStopWith.Location = New System.Drawing.Point(128, 177)
        Me.cboStopWith.Name = "cboStopWith"
        Me.cboStopWith.Size = New System.Drawing.Size(208, 21)
        Me.cboStopWith.TabIndex = 14
        '
        'cboStop
        '
        Me.cboStop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStop.Location = New System.Drawing.Point(128, 140)
        Me.cboStop.Name = "cboStop"
        Me.cboStop.Size = New System.Drawing.Size(208, 21)
        Me.cboStop.TabIndex = 13
        '
        'cboClock
        '
        Me.cboClock.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboClock.Location = New System.Drawing.Point(128, 102)
        Me.cboClock.Name = "cboClock"
        Me.cboClock.Size = New System.Drawing.Size(208, 21)
        Me.cboClock.TabIndex = 12
        '
        'cboStartWith
        '
        Me.cboStartWith.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStartWith.Location = New System.Drawing.Point(128, 65)
        Me.cboStartWith.Name = "cboStartWith"
        Me.cboStartWith.Size = New System.Drawing.Size(208, 21)
        Me.cboStartWith.TabIndex = 11
        '
        'cboStart
        '
        Me.cboStart.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStart.Location = New System.Drawing.Point(128, 28)
        Me.cboStart.Name = "cboStart"
        Me.cboStart.Size = New System.Drawing.Size(208, 21)
        Me.cboStart.TabIndex = 10
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.rdoRing, Me.rdoOnce})
        Me.GroupBox3.Location = New System.Drawing.Point(272, 245)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(280, 56)
        Me.GroupBox3.TabIndex = 16
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Transfer Mode"
        '
        'rdoRing
        '
        Me.rdoRing.Location = New System.Drawing.Point(144, 23)
        Me.rdoRing.Name = "rdoRing"
        Me.rdoRing.Size = New System.Drawing.Size(120, 19)
        Me.rdoRing.TabIndex = 3
        Me.rdoRing.Text = "Ring"
        '
        'rdoOnce
        '
        Me.rdoOnce.Location = New System.Drawing.Point(24, 23)
        Me.rdoOnce.Name = "rdoOnce"
        Me.rdoOnce.Size = New System.Drawing.Size(96, 19)
        Me.rdoOnce.TabIndex = 2
        Me.rdoOnce.Text = "Once"
        '
        'frmSSmpMode
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(560, 351)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK, Me.GroupBox2, Me.GroupBox1, Me.GroupBox3})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmSSmpMode"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Slave Sampling Condition Setting"
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim StopNum As Integer
    Public Shared S_NotifyCount As Integer

    '===================================================
    '	Dialog initialization
    '===================================================
    Private Sub frmSSmpMode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------
        ' Initialize dialog display
        '--------------------------------
        ' 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, "Start counter regardless of sampling")
            .Items.Insert(1, "Start counter when sampling starts")
        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, "Stop Transfer by Specified Times")

        End With
        ' Stop with
        With cboStopWith
            .Items.Clear()
            .Items.Insert(0, "Stop counter regardless of sampling")
            .Items.Insert(1, "Stop counter when sampling stops")
        End With

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

        StopNum = 1000
        S_NotifyCount = 500
        Sync.S_IsRing = CNTS_WRITE_ONCE


        ' In order to display the stop number to unsigned, convert it once through hexadecimal characters
        txtStopNumber.Text = Convert.ToUInt32(Hex(StopNum), 16)
        ' In order to display the number of sampling cycles for notification to unsigned, convert it once through hexadecimal characters
        txtNumberData.Text = Convert.ToUInt32(Hex(S_NotifyCount), 16)

        If Sync.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 processing
    '===================================================
    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)

        '--------------------------------
        ' Get screen data
        '--------------------------------
        ' In order to set the stop number to signed, convert it once through hexadecimal characters
        StopNum = Convert.ToInt32(Hex(Val(txtStopNumber.Text)), 16)
        ' In order to set the number of sampling cycles for notification to signed, convert it once through hexadecimal characters
        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
            Sync.S_IsRing = CNTS_WRITE_ONCE
        Else
            Sync.S_IsRing = CNTS_WRITE_RING
        End If

        '--------------------------------
        ' Set sampling condition
        '--------------------------------
        ExtSig1 = 1
        ExtSig2 = 1
        ExtSig3 = 1
        ret = CntSetSlave(Sync.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(Sync.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(Sync.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(Sync.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(Sync.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

    '===================================================
    '	Cancel processing
    '===================================================
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Hide()
    End Sub

    '===================================================
    '	Change stop condition setting
    '===================================================
    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, "Stop Transfer by Specified Times") = 0 Then
            txtStopNumber.Enabled = True
        Else
            txtStopNumber.Enabled = False
        End If
    End Sub

    Private Sub frmSSmpMode_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
        Me.Hide()
    End Sub
End Class
