カテゴリー別アーカイブ: 未分類

このページはお客様専用ページです。
ページの閲覧には専用パスワードが必要となります。
パスワードはご連絡しておりますパスワードです。
パスワードをお忘れになられた場合、担当者までご連絡ください。
 

 
This page is USER ONLY.
Browse the page will require private password.
If you have forgotten your password, please contact.

Password:

   


Pythonのサンプルコード

提供しているソフトウェアのコードは以下のコードとなっております。

#
#    Copyright (C)2020 TESBIT co.,ltd. All rights reserved.
#    

import tkinter as tk
import serial
import threading
import time
from tkinter import messagebox


#グローバル変数========================================================
comf_conn = 0 # 接続管理フラグ
comf_disconn = 0 # 切断管理フラグ
tf = 1 # スレッドフラグ(Thread Flag)

comf_init = 0 # 初期化管理フラグ
comf_read_a = 0 # read_a管理フラグ

comf_move_z = 0 # ズーム移動管理フラグ
comf_move_f = 0 # フォーカス移動管理フラグ
comf_move_i = 0 # アイリス移動管理フラグ
comf_read_z = 0 # ズーム値読込管理フラグ
comf_read_f = 0 # フォーカス値読込管理フラグ
comf_read_i = 0 # アイリス値読込管理フラグ


# 画面設定
root = tk.Tk()
root.geometry('250x110') # 画面サイズ
root.title('シリアル通信') # 画面タイトル

var = tk.StringVar() # 初期化ステータス表示ラベルの変数


# ボタンのクリックイベント
def btn_ZoomSet_click(): # ズームsetボタン
    print('move_z')
    global comf_move_z # 使うところでグローバル宣言
    comf_move_z = 1 # 実際に使う

def btn_ZoomRead_click(): # ズームreadボタン
    print('read_z')
    global comf_read_z
    comf_read_z = 1

def btn_FocusSet_click(): # フォーカスsetボタン
    print('move_f')
    global comf_move_f
    comf_move_f = 1
    
def btn_FocusRead_click(): # フォーカスreadボタン
    print('read_f')
    global comf_read_f
    comf_read_f = 1
    
def btn_IrisSet_click(): # アイリスsetボタン
    print('move_i')
    global comf_move_i
    comf_move_i = 1

def btn_IrisRead_click(): # アイリスreadボタン
    print('read_i')
    global comf_read_i
    comf_read_i = 1

def btn_conn_click(): # 接続ボタン
    print('接続')
    global comf_conn
    comf_conn = 1

def btn_disconn_click(): # 切断ボタン
    print('切断')
    global comf_disconn
    comf_disconn = 1

def btn_init_click(): # 初期化ボタン
    print('初期化')
    global comf_init
    comf_init = 1


# ラベルの配置
lb_Zoom = tk.Label(text = 'Zoom')
lb_Zoom.grid(row=0,column=0)
lb_Focus = tk.Label(text = 'Focus')
lb_Focus.grid(row=1,column=0)
lb_Iris = tk.Label(text = 'Iris')
lb_Iris.grid(row=2,column=0)

# テキストボックスの配置
tb_Zoom = tk.Entry(width=20)
tb_Zoom.grid(row=0,column=1)
tb_Focus = tk.Entry(width=20)
tb_Focus.grid(row=1,column=1)
tb_Iris = tk.Entry(width=20)
tb_Iris.grid(row=2,column=1)


# ボタンの配置
btn_ZoomSet = tk.Button(text = 'set', command = btn_ZoomSet_click)
btn_ZoomRead = tk.Button(text = 'read', command = btn_ZoomRead_click)
btn_ZoomSet.grid(row=0,column=2)
btn_ZoomRead.grid(row=0,column=3)
btn_FocusSet = tk.Button(text = 'set', command = btn_FocusSet_click)
btn_FocusRead = tk.Button(text = 'read', command = btn_FocusRead_click)
btn_FocusSet.grid(row=1,column=2)
btn_FocusRead.grid(row=1,column=3)
btn_IrisSet = tk.Button(text = 'set', command = btn_IrisSet_click)
btn_IrisRead = tk.Button(text = 'read', command = btn_IrisRead_click)
btn_IrisSet.grid(row=2,column=2)
btn_IrisRead.grid(row=2,column=3)

# init関係
btn_init = tk.Button(text = '初期化', command = btn_init_click)
btn_init.grid(row=3,column=0)
var.set("") # 初期化ステータスを空欄にする
lb_init = tk.Label(textvariable=var)
lb_init.grid(row=3,column=1)

btn_conn = tk.Button(text = '接続', command = btn_conn_click)
btn_disconn = tk.Button(text = '切断', command=btn_disconn_click)
btn_conn.grid(row=3,column=2)
btn_disconn.grid(row=3,column=3)



# シリアル通信系のコード(スレッド)====================================
comf_receive = 0 # 結果を受信しに行っていいか判断するフラグ

def commCTR():
    # シリアル通信の設定
    comm = serial.Serial()
    comm.baudrate = 38400 # ボーレート
    comm.parity = serial.PARITY_NONE
    comm.bytesize = serial.EIGHTBITS
    comm.stopbits = serial.STOPBITS_ONE
    comm.timeout = 5 #sec
    comm.port = 'COM7' # TSZC-06を接続しているCOMポート

    #グローバル変数の呼び出し
    global tf
    tf = 1 # tf=1のあいだスレッド内のループが実行される
    global comf_conn
    global comf_disconn
    global comf_init
    global comf_move_z
    global comf_move_f
    global comf_move_i
    global comf_read_z
    global comf_read_f
    global comf_read_i

    global comf_receive
    global comf_read_a
    global var

    # シリアル通信ループ
    while tf:
        if comf_conn == 1: #フラグONのときの動作
            comf_conn = 0 #フラグOFFに戻す
            print("接続命令受信")

            if(comm.isOpen() == False):
                print('connect')
                comm.open() # COMポート開く
                comf_receive = 1 #受信ON
            else:
                print('opened')
        
        if comf_disconn == 1:
            comf_disconn = 0
            print("切断命令受信")
            comm.close() # COMポート閉じる
            comf_receive = 0 #受信OFF

        # init
        if comf_init == 1:
            comf_init = 0
            print("初期化命令受信")
            comm.reset_input_buffer() # ノイズデータがある場合があるのでバッファをクリアする
            # zoomの値をシリアル通信
            send_string = 'init_a'
            print(send_string)
            var.set("初期化中")
            comm.write(send_string.encode())
            #ボタン無効化
            btn_ZoomSet.config(state="disable")
            btn_ZoomRead.config(state="disable")
            btn_FocusSet.config(state="disable")
            btn_FocusRead.config(state="disable")
            btn_IrisSet.config(state="disable")
            btn_IrisRead.config(state="disable")
            btn_init.config(state="disable")
            btn_conn.config(state="disable")
            btn_disconn.config(state="disable")

        # read_a
        if comf_read_a ==1:
            comf_read_a = 0
            print("read_a実行")
            comm.reset_input_buffer()
            send_string = 'read_a'
            print(send_string)
            comm.write(send_string.encode())

        #zoomのsetボタンが押された後
        if comf_move_z == 1:
            comf_move_z = 0
            print("comf_move_z")
            # テキスト取得
            comm.reset_input_buffer()
            num = int(tb_Zoom.get())
            # zoomの値をシリアル通信
            send_string = 'move_z,' + str(num)
            print(send_string)
            comm.write(send_string.encode())

        #zoomのreadボタンが押された後
        if comf_read_z == 1:
            comf_read_z = 0
            print("comf_read_z")
            tb_Zoom.delete(0,tk.END) # テキストボックスの初期化
            comm.reset_input_buffer()
            # read_zoom命令をシリアル通信
            send_string = 'read_z'
            comm.write(send_string.encode())

        #focusのsetボタンが押された後
        if comf_move_f == 1:
            comf_move_f = 0
            print("comf_move_f")
            # テキスト取得
            comm.reset_input_buffer()
            num = int(tb_Focus.get())
            # focusの値をシリアル通信
            send_string = 'move_f,' + str(num)
            print(send_string)
            comm.write(send_string.encode())

        #focusのreadボタンが押された後
        if comf_read_f == 1:
            comf_read_f = 0
            print("comf_read_f")
            tb_Focus.delete(0,tk.END) # テキストボックスの初期化
            comm.reset_input_buffer()
            # read_iris命令をシリアル通信
            send_string = 'read_f'
            comm.write(send_string.encode())

        #irisのsetボタンが押された後
        if comf_move_i == 1:
            comf_move_i = 0
            print("comf_move_i")
            # テキスト取得
            comm.reset_input_buffer()
            num = int(tb_Iris.get())
            # irisの値をシリアル通信
            send_string = 'move_i,' + str(num)
            print(send_string)
            comm.write(send_string.encode())

        #irisのreadボタンが押された後
        if comf_read_i == 1:
            comf_read_i = 0
            print("comf_read_i")
            tb_Iris.delete(0,tk.END) # テキストボックスの初期化
            comm.reset_input_buffer()
            # read_iris命令をシリアル通信
            send_string = 'read_i'
            comm.write(send_string.encode())

        # 毎回受信する
        # readlineした結果に応じて動作を切り替える
        # read_z,read_f,read_iを含む文字列があるか調べる
        # init_aを送ったら終了時にinit_a_doneが返ってくる

        #comf_receiveが1になっているとき(毎回受信ON)
        if comf_receive == 1:
            # コマンドを送った結果を受信
            recv_string = comm.readline()
            recv_string = recv_string.decode()
            recv_list = []
            recv_list = recv_string.split(',')
            # リストの中にread_z,read_f,read_iが含まれるか
            if 'read_z' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                tb_Zoom.delete(0,tk.END)
                tb_Zoom.insert(0, recv_list[1])
            elif 'read_f' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                tb_Focus.delete(0,tk.END)
                tb_Focus.insert(0, recv_list[1])
            elif 'read_i' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                tb_Iris.delete(0,tk.END)
                tb_Iris.insert(0, recv_list[1])
            # 'read_a'のとき
            elif 'read_a' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                tb_Zoom.delete(0,tk.END)
                tb_Zoom.insert(0, recv_list[1])
                tb_Focus.delete(0,tk.END)
                tb_Focus.insert(0, recv_list[2])
                tb_Iris.delete(0,tk.END)
                tb_Iris.insert(0, recv_list[3])
            # 'init_a'のとき
            elif 'init_a_done' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                var.set("")
                messagebox.showinfo("メッセージ", "初期化が完了しました")
                #ボタン有効化
                btn_ZoomSet.config(state="normal")
                btn_ZoomRead.config(state="normal")
                btn_FocusSet.config(state="normal")
                btn_FocusRead.config(state="normal")
                btn_IrisSet.config(state="normal")
                btn_IrisRead.config(state="normal")
                btn_init.config(state="normal")
                btn_conn.config(state="normal")
                btn_disconn.config(state="normal")
                comf_read_a = 1
            # move後のread
            elif 'move_z;' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                comf_read_z = 1
            elif 'move_f;' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                comf_read_f = 1
            elif 'move_i;' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                comf_read_i = 1
            # 接続直後のread_a
            elif 'ZLC TESBIT TSZC-06' in recv_list[0]:
                print("コマンド結果受信",recv_string)
                comf_read_a = 1

            else :
                time.sleep(0.2)

        time.sleep(0.2)


# スレッド開始==========================================================
threadComm = threading.Thread(target=commCTR)
threadComm.start()
threadComm.join
print("スレッド起動完了")

# 閉じるボタン
def on_closing():
    global tf
    tf = 0
    root.destroy()

root.protocol("WM_DELETE_WINDOW", on_closing)

# 画面表示
root.mainloop()

VB.NETのサンプルコード

提供しているソフトウェアのコードは以下のコードとなっております。

Imports System.IO.Ports


'===========================================================
'
'   ズームレンズコントローラー
'   サンプルプログラム
'   2017 TESBIT co.,ltd.
'   http://tesbit.co.jp
'
'===========================================================

Public Class f_zlc

    Private COMSelected = False
    Private COMPort As String = "COM7"
    Private DEMO_MODE As Boolean = False
    Private strStatus As String = ""
    Private booting As Boolean = True
    Private PosZoom As Integer = 0
    Private PosFocus As Integer = 0
    Private PosIris As Integer = 0

    Const BAUD_RATE As Integer = 38400
    Private sendf As Boolean = False '送信済みフラグ
    Private move_af As Boolean = False 'move_aフラグ(move_p追加)
    Private init_af As Boolean = False 'init_aフラグ

    Delegate Sub UpdateDelegate()
    Delegate Sub enableDelegate()

    Dim tt_val As ToolTip

    Dim init_a_f As Boolean = False

    'フォーム構築・初期化
    Private Sub f_zlc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.tt_val = New ToolTip(Me.components)

        ''ポートリスト取得
        Dim portList() As String
        portList = SerialPort.GetPortNames

        Dim i As Integer

        '初期化ファイルの読み込み
        If System.IO.File.Exists("ZLCinit.ini") = False Then
NoSettings:

            'コンボボックス初期化
            Me.cb_com.Items.Add("未設定")
            For i = 0 To portList.Length - 1
                Me.cb_com.Items.Add(portList(i))
            Next
            Me.cb_com.EndUpdate()
            Me.cb_com.SelectedIndex = 0

            '確認メッセージ
            MsgBox("設定ファイルが存在しません。" + vbCrLf + "COMポートを選択してください。", MsgBoxStyle.OkOnly, "COMエラー")

            booting = False
            Return
        Else

            'ファイルの読み込み
            If Me.readInitFile() = False Then
                GoTo NoSettings
            End If

            For i = 0 To portList.Length - 1
                Me.cb_com.Items.Add(portList(i))
            Next
            Me.cb_com.EndUpdate()
            Me.cb_com.Text = Me.COMPort

            'COMの初期化
            Try
                With COM
                    .PortName = Me.COMPort
                    .Parity = Parity.None
                    '.BaudRate = 38400
                    .BaudRate = BAUD_RATE
                    .DataBits = 8
                    .StopBits = 1
                    .Open()
                End With
            Catch ex As Exception

                MsgBox("COMの初期化でエラーが発生しました。" + vbCrLf + ex.Message, MsgBoxStyle.OkOnly, "COMエラー")

                booting = False
                Return
            End Try

            'TSZCをリセット(DtrEnable = True)する 
            Me.COM_SEND("reset")


            'インチング速度設定
            Me.COM_SEND("s_incz,100")
            Me.COM_SEND("s_incf,100")
            Me.COM_SEND("s_inci,100")

            '現在値の取得
            Me.COM_SEND("read_a")

            booting = False
        End If


    End Sub

    '終了処理
    Private Sub f_zlc_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed

        If Me.COM.IsOpen Then
            Me.COM.Close()
        End If

        Me.COM.Dispose()

        '設定の保存
        Me.saveInitFile()


    End Sub

    'COMコンボボックス
    Private Sub cb_com_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cb_com.SelectedIndexChanged

        If Me.booting = True Then
            Return
        End If


        If Me.cb_com.SelectedText.Equals("未設定") Then
            Return
        Else
            If Me.COM.IsOpen = True Then
                Me.COM.Close()
            End If
            Me.COMPort = Me.cb_com.SelectedItem.ToString
            'COMの初期化
            Try
                With COM
                    .PortName = Me.COMPort
                    .Parity = Parity.None
                    '.BaudRate = 9600
                    .BaudRate = BAUD_RATE
                    .DataBits = 8
                    .StopBits = 1
                    .Open()
                End With
            Catch ex As Exception

                MsgBox("COMの初期化でエラーが発生しました。" + vbCrLf + ex.Message, MsgBoxStyle.OkOnly, "COMエラー")

            End Try

            'TSZCをリセット(DtrEnable = True)する 
            Me.COM_SEND("reset")
            'インチング速度設定 
            Me.COM_SEND("s_incz,100")
            Me.COM_SEND("s_incf,100")
            Me.COM_SEND("s_inci,100")

            '現在値の取得
            Me.COM_SEND("read_a")
        End If

    End Sub

    '初期化ファイル読み込み
    Private Function readInitFile() As Boolean

        Dim i As Integer
        Dim strRDT() As String
        Dim strDT() As String
        Try
            strRDT = System.IO.File.ReadAllLines("ZLCinit.ini")

            For i = 0 To strRDT.Length - 1

                strDT = strRDT(i).Split(";")
                Select Case strDT(0)
                    Case "COM"
                        Me.COMPort = strDT(1)
                        Exit Select
                End Select

            Next

        Catch ex As Exception

            MsgBox("初期化ファイルの読み込みに失敗しました1。" + vbCrLf + ex.Message, MsgBoxStyle.OkOnly, "ファイルエラー")
            Return False
        End Try

        Return True

    End Function

    '初期化ファイル保存
    Private Function saveInitFile() As Boolean

        Try

            Dim str As String = System.String.Empty
            str += "COM;" + Me.COMPort + vbCrLf

            Dim sw As System.IO.StreamWriter

            sw = New System.IO.StreamWriter("ZLCinit.ini", False, System.Text.Encoding.GetEncoding("shift_jis"))
            sw.Write(str)
            sw.Close()

        Catch ex As Exception

            MsgBox("初期化ファイルの保存に失敗しました。" + vbCrLf + ex.Message, MsgBoxStyle.OkOnly, "ファイルエラー")

            Return False
        End Try
        Return True

    End Function

    '===========================================================
    '
    '   通信関連
    '   
    '
    '===========================================================
    'コマンド定義
    Private Enum ZoomRetCMD
        read_a = 1
        read_z = 2
        read_f = 3
        read_i = 4
        move_a = 11
        move_z = 12
        move_f = 13
        move_i = 14
        inc_zf = 20
        inc_zr = 21
        inc_ff = 22
        inc_fr = 23
        inc_if = 24
        inc_ir = 25
        instop = 30

        init_a = 100
        init_a_z = 101
        init_a_f = 102
        init_a_i = 103

        err = 999

        standby = 0
    End Enum

    'コマンド判別
    Private Function getZoommCmd(ByRef strDT As String) As ZoomRetCMD

        Dim CMD As Integer = ZoomRetCMD.standby


        If strDT.IndexOf("read_a") <> -1 Then
            CMD = ZoomRetCMD.read_a
        ElseIf strDT.IndexOf("read_z") <> -1 Then
            CMD = ZoomRetCMD.read_z
        ElseIf strDT.IndexOf("read_f") <> -1 Then
            CMD = ZoomRetCMD.read_f
        ElseIf strDT.IndexOf("read_i") <> -1 Then
            CMD = ZoomRetCMD.read_i

        ElseIf strDT.IndexOf("move_a") <> -1 Then
            CMD = ZoomRetCMD.move_a
        ElseIf strDT.IndexOf("move_z") <> -1 Then
            CMD = ZoomRetCMD.move_z
        ElseIf strDT.IndexOf("move_f") <> -1 Then
            CMD = ZoomRetCMD.move_f
        ElseIf strDT.IndexOf("move_i") <> -1 Then
            CMD = ZoomRetCMD.move_i

        ElseIf strDT.IndexOf("inc_ff") <> -1 Then
            CMD = ZoomRetCMD.inc_ff
        ElseIf strDT.IndexOf("inc_fr") <> -1 Then
            CMD = ZoomRetCMD.inc_fr
        ElseIf strDT.IndexOf("inc_zf") <> -1 Then
            CMD = ZoomRetCMD.inc_zf
        ElseIf strDT.IndexOf("inc_zr") <> -1 Then
            CMD = ZoomRetCMD.inc_zr
        ElseIf strDT.IndexOf("inc_if") <> -1 Then
            CMD = ZoomRetCMD.inc_if
        ElseIf strDT.IndexOf("inc_ir") <> -1 Then
            CMD = ZoomRetCMD.inc_ir

        ElseIf strDT.IndexOf("instop") <> -1 Then
            CMD = ZoomRetCMD.instop

        ElseIf strDT.IndexOf("init_a_done") <> -1 Then
            CMD = ZoomRetCMD.init_a
        ElseIf strDT.IndexOf("init_a_z") <> -1 Then
            CMD = ZoomRetCMD.init_a_z
        ElseIf strDT.IndexOf("init_a_f") <> -1 Then
            CMD = ZoomRetCMD.init_a_f
        ElseIf strDT.IndexOf("init_a_i") <> -1 Then
            CMD = ZoomRetCMD.init_a_i
        ElseIf strDT.IndexOf("Err") <> -1 Then
            CMD = ZoomRetCMD.err
        Else
            CMD = ZoomRetCMD.standby
        End If


        Return CMD
    End Function

    'コマンド送信
    '同期通信対応 返信がある処理については本関数を通すこと。
    Private Function COM_SEND(ByVal strCMD As String) As Boolean

        '送信フラグON 
        sendf = True

        If Me.COM.IsOpen Then

            '発行されたコマンドが"reset"だった場合
            If (strCMD = "reset") Then
                'リセット(DtrEnable = True)すると「ZLC TESBIT・・」が返信されるため
                '本関数内処理実行() 
                Me.COM.DtrEnable = True
            Else

                '複数の返信があるコマンド
                If strCMD.IndexOf("move_a") <> -1 Then move_af = True
                If strCMD.IndexOf("move_p") <> -1 Then move_af = True
                If strCMD.IndexOf("init_a") <> -1 Then init_af = True

                'If Me.cb_delm.Checked Then 
                Me.COM.Write(strCMD + vbCrLf)
                'Else
                'Me.COM.Write(strCMD)
                'End If

                Me.strStatus = ">" + strCMD + vbCrLf

                '表示の更新
                Invoke(New UpdateDelegate(AddressOf UpdateData))

            End If

            Me.COM.DiscardOutBuffer()
        End If

        'データを受信するまで待機 
        Dim loopCnt As Integer = 1
        While (sendf)
            'メッセージキューに現在あるWindowsメッセージをすべて処理する
            System.Windows.Forms.Application.DoEvents()
            System.Threading.Thread.Sleep(10)

            If loopCnt = 20000 Then
                Me.strStatus = "VBError:timeout" + vbCrLf
                '表示の更新
                Invoke(New UpdateDelegate(AddressOf UpdateData))
                sendf = False
                Return False
            End If

            loopCnt = loopCnt + 1
        End While

        Return True

    End Function

    'コマンド受信
    '同期通信対応 それに伴い本関数内からのCOM_SENDの実行を禁止。
    Private Sub COM_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COM.DataReceived

        If Me.COM.IsOpen = False Then
            Return
        End If

        Try

            Dim strDT As String = Me.COM.ReadLine

            Me.strStatus = "<" + strDT + vbCrLf
            Invoke(New UpdateDelegate(AddressOf UpdateData))

            '送信フラグOFF 
            If move_af Then
                If System.Text.RegularExpressions.Regex.IsMatch(strDT, "move_a.*?_done") Then
                    'move_a(p)を実行した場合move_a(p)_doneが返却されたらOFF
                    If sendf = True Then sendf = False
                    move_af = False
                End If
            ElseIf init_af Then
                If strDT.IndexOf("init_a_done") <> -1 Then
                    'init_aを実行した場合init_a doneが返却されたらOFF
                    If sendf = True Then sendf = False
                    init_af = False
                End If
            Else
                '上記以外の場合OFF
                If sendf = True Then sendf = False
            End If


            If strDT.IndexOf("ZLC") <> -1 Then
                'Me.COM_SEND("read_a") <ZLC TESBIT TSZC-06b
            End If


            Select Case getZoommCmd(strDT)
                Case ZoomRetCMD.standby
                    Exit Select

                Case ZoomRetCMD.read_a
                    Dim strDTT() As String = strDT.Split(",")
                    If strDTT.Length < 3 Then
                        Exit Select
                    End If

                    Me.PosZoom = Integer.Parse(strDTT(1))
                    Me.PosFocus = Integer.Parse(strDTT(2))
                    Me.PosIris = Integer.Parse(strDTT(3))
                    Me.strStatus = ""
                    Invoke(New UpdateDelegate(AddressOf UpdateData))
                    Exit Select

                Case ZoomRetCMD.move_a, _
                     ZoomRetCMD.move_z, _
                     ZoomRetCMD.move_f, _
                     ZoomRetCMD.move_i

                    'Me.COM_SEND("read_a") 
                    'Invoke(New enableDelegate(AddressOf enableControl))


                    Exit Select
                Case ZoomRetCMD.inc_zf, _
                     ZoomRetCMD.inc_zr, _
                     ZoomRetCMD.inc_ff, _
                     ZoomRetCMD.inc_fr, _
                     ZoomRetCMD.inc_if, _
                     ZoomRetCMD.inc_ir


                    Exit Select
                Case ZoomRetCMD.instop

                    'Me.COM_SEND("read_a") 
                    Exit Select

                Case ZoomRetCMD.init_a
                    'Me.COM_SEND("move_a,400,600,800")
                    Exit Select

                Case ZoomRetCMD.init_a_z

                    Exit Select
                Case ZoomRetCMD.init_a_f
                    Exit Select
                Case ZoomRetCMD.init_a_i

                    Exit Select
                Case ZoomRetCMD.err
                    Invoke(New enableDelegate(AddressOf enableControl))
                    Exit Select
            End Select


        Catch ex As Exception

            Me.strStatus = ex.ToString
            Invoke(New UpdateDelegate(AddressOf UpdateData))

        End Try


        Me.COM.DiscardInBuffer()


    End Sub

    'エラー受信
    Private Sub COM_ErrorReceived(sender As Object, e As System.IO.Ports.SerialErrorReceivedEventArgs) Handles COM.ErrorReceived




    End Sub





    '===========================================================
    '
    '   ボタン操作
    '   
    '
    '===========================================================
    '送信ボタン
    Private Sub btn_send_Click(sender As System.Object, e As System.EventArgs) Handles btn_send.Click

        If Me.tx_comSend.Text = "" Then
            Return
        End If

        Dim str = Me.tx_comSend.Text
        Dim str_six = str.Substring(0, 6)
        'Me.COM_SEND(Me.tx_comSend.Text)
        Me.COM_SEND(str)


        Select Case str_six

            Case "move_a", _
                 "move_z", _
                 "move_f", _
                 "move_i", _
                 "move_p", _
                 "init_a", _
                 "init_z", _
                 "init_f", _
                 "init_i", _
                 "instop"

                Me.COM_SEND("read_a")
                Exit Select
            Case "inchsp"
                Dim arr = str.Split(",")
                If arr(0) = 255 Then
                    rb_z255.Checked = True
                End If
                Exit Select
            Case "s_incz"
                Exit Select
            Case "s_incf"
                Exit Select
            Case "s_inci"
                Exit Select
        End Select

    End Sub

    'zoom
    Private Sub btn_zoomUP_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_zoomUP.MouseDown

        Me.COM_SEND("inc_zr")


    End Sub
    Private Sub btn_zoomDW_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_zoomDW.MouseDown

        Me.COM_SEND("inc_zf")

    End Sub
    'focus
    Private Sub btn_focusUP_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_focusUP.MouseDown

        Me.COM_SEND("inc_fr")

    End Sub
    Private Sub btn_focusDW_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_focusDW.MouseDown

        Me.COM_SEND("inc_ff")

    End Sub
    'Iris
    Private Sub btn_irisUP_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_irisUP.MouseDown

        Me.COM_SEND("inc_ir")

    End Sub
    Private Sub btn_irisDW_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_irisDW.MouseDown

        Me.COM_SEND("inc_if")

    End Sub

    'インチング停止
    Private Sub btn_ZLC_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btn_zoomUP.MouseUp, _
                                                                                                    btn_zoomDW.MouseUp, _
                                                                                                    btn_focusUP.MouseUp, _
                                                                                                    btn_focusDW.MouseUp, _
                                                                                                    btn_irisUP.MouseUp, _
                                                                                                    btn_irisDW.MouseUp
        Me.COM_SEND("instop")

        Me.COM_SEND("read_a")

    End Sub


    '送信コマンドテキストボックス制御
    Private Sub tx_comSend_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tx_comSend.KeyPress
        If e.KeyChar = Chr(13) Then
            Me.btn_send.PerformClick()
            Me.tx_comSend.Text = ""
        End If
    End Sub

    '===========================================================
    '
    '   トラックバー関連
    '   
    '
    '===========================================================
    Dim tb_zoomDown As Boolean = False
    Dim tb_focusDown As Boolean = False
    Dim tb_irisDown As Boolean = False

    Private Sub tb_iris_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_iris.MouseDown
        tb_irisDown = True
    End Sub

    Private Sub tb_iris_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_iris.MouseMove
        If Me.tb_irisDown = True Then
            Me.tt_val.SetToolTip(Me.tb_iris, Me.tb_iris.Value.ToString)
        End If
    End Sub

    Private Sub tb_iris_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_iris.MouseUp
        Dim val As Integer = Me.tb_iris.Value
        Me.disableControl() '移動完了まで停止
        Me.COM_SEND("move_i," + val.ToString)
        Me.PosIris = val
        'Me.UpdateData() 
        Me.tb_irisDown = False
        Me.enableControl()

        Me.COM_SEND("read_a")
    End Sub

    Private Sub tb_focus_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_focus.MouseDown
        Me.tb_focusDown = True
    End Sub

    Private Sub tb_focus_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_focus.MouseMove
        If Me.tb_focusDown = True Then
            Me.tt_val.SetToolTip(Me.tb_focus, Me.tb_focus.Value.ToString)
        End If
    End Sub

    Private Sub tb_focus_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_focus.MouseUp
        Dim val As Integer = Me.tb_focus.Value
        Me.disableControl() '移動完了まで停止
        Me.COM_SEND("move_f," + val.ToString)
        Me.PosFocus = val
        'Me.UpdateData() 
        Me.tb_focusDown = False
        Me.enableControl()

        Me.COM_SEND("read_a")
    End Sub

    Private Sub tb_zoom_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_zoom.MouseDown
        Me.tb_zoomDown = True
    End Sub

    Private Sub tb_zoom_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_zoom.MouseMove
        If Me.tb_zoomDown = True Then
            Me.tt_val.SetToolTip(Me.tb_zoom, Me.tb_zoom.Value.ToString)
        End If
    End Sub

    Private Sub tb_zoom_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles tb_zoom.MouseUp
        Dim val As Integer = Me.tb_zoom.Value
        Me.disableControl() '移動完了まで停止
        Me.COM_SEND("move_z," + val.ToString)
        Me.PosZoom = val
        'Me.UpdateData() 
        Me.tb_zoomDown = False
        Me.enableControl()

        Me.COM_SEND("read_a")
    End Sub

    '===========================================================
    '
    '   テキストボックス関連
    '   
    '
    '===========================================================
    Private Sub tx_zoom_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tx_zoom.KeyPress
        If e.KeyChar = Chr(13) Then

            Me.PosZoom = Integer.Parse(Me.tx_zoom.Text)
            Me.COM_SEND("move_z," + Me.tx_zoom.Text)
            Me.COM_SEND("read_a")

        End If
    End Sub

    Private Sub tx_focus_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tx_focus.KeyPress
        If e.KeyChar = Chr(13) Then

            Me.PosFocus = Integer.Parse(Me.tx_focus.Text)
            Me.COM_SEND("move_f," + Me.tx_focus.Text)
            Me.COM_SEND("read_a")

        End If
    End Sub

    Private Sub tx_iris_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tx_iris.KeyPress
        If e.KeyChar = Chr(13) Then

            Me.PosIris = Integer.Parse(Me.tx_iris.Text)
            Me.COM_SEND("move_i," + Me.tx_iris.Text)
            Me.COM_SEND("read_a")

        End If
    End Sub


    '===========================================================
    '
    '   COM送受信部などの表示更新
    '   Invokeで使用
    '
    '===========================================================
    Private Sub UpdateData()

        Me.tx_comRes.AppendText(Me.strStatus)
        If Me.tx_comRes.Lines.Length > 100 Then

            Dim str As String = Me.tx_comRes.Text
            Dim Pos As Integer = Me.tx_comRes.Text.IndexOf(vbCrLf)
            Me.tx_comRes.Text = Me.tx_comRes.Text.Substring(Me.tx_comRes.Text.IndexOf(vbCrLf) _
                                   + 2, Me.tx_comRes.Text.Length - ((Me.tx_comRes.Text.IndexOf(vbCrLf) + 2)))
        End If

        Me.tx_comRes.SelectionStart = Me.tx_comRes.Text.Length
        Me.tx_comRes.ScrollToCaret()

        Me.tx_zoom.Text = Me.PosZoom.ToString
        Me.tx_zoom.Refresh()
        Me.tx_focus.Text = Me.PosFocus.ToString
        Me.tx_focus.Refresh()
        Me.tx_iris.Text = Me.PosIris.ToString
        Me.tx_iris.Refresh()

        Me.tb_zoom.Value = Me.PosZoom
        Me.tb_focus.Value = Me.PosFocus
        Me.tb_iris.Value = Me.PosIris

    End Sub

    '===========================================================
    '
    '   移動後のコントロールの動作回復
    '   Invokeで使用
    '
    '===========================================================
    Private Sub enableControl()
        Me.tb_zoom.Enabled = True
        Me.tb_focus.Enabled = True
        Me.tb_iris.Enabled = True
        Me.btn_zoomUP.Enabled = True
        Me.btn_zoomDW.Enabled = True
        Me.btn_focusUP.Enabled = True
        Me.btn_focusDW.Enabled = True
        Me.btn_irisUP.Enabled = True
        Me.btn_irisDW.Enabled = True
    End Sub
    Private Sub disableControl()
        Me.tb_zoom.Enabled = False
        Me.tb_focus.Enabled = False
        Me.tb_iris.Enabled = False
        Me.btn_zoomUP.Enabled = False
        Me.btn_zoomDW.Enabled = False
        Me.btn_focusUP.Enabled = False
        Me.btn_focusDW.Enabled = False
        Me.btn_irisUP.Enabled = False
        Me.btn_irisDW.Enabled = False
    End Sub

    '===========================================================
    '
    '   システム初期化
    '   初回通電時に必ず初期化を行ってください
    '   本機内部にポテンショメータの値を記録します。
    '
    '===========================================================
    Private Sub btn_init_Click(sender As System.Object, e As System.EventArgs) Handles btn_init.Click

        If MsgBox("初回起動時のポテンショメータの値登録を行いますか?" + vbCrLf + _
               "※3分程度掛かります。レンズを変更しない限り必要有りません。" + vbCrLf + _
               "※レンズを交換したら実行してください。", _
                MsgBoxStyle.YesNo, _
                "初期化の確認") = MsgBoxResult.Yes Then

            init_a_f = True

            Me.COM_SEND("init_a")

            rb_z100.Checked = True
            rb_f100.Checked = True
            rb_i100.Checked = True

            Me.COM_SEND("read_a")

            init_a_f = False
        End If


    End Sub

    '===========================================================
    '
    '   インチング速度設定
    '   ハードウエアの仕様上focusのみ移動速度が若干遅くなります。
    '   そのため、初期値でfocusのみ速度を速くしています。
    '
    '===========================================================
    Private Sub rb_z_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_z100.CheckedChanged, _
                                                                                            rb_z200.CheckedChanged, _
                                                                                            rb_z255.CheckedChanged
        If Me.booting = True Then
            Return
        End If

        If CType(sender, RadioButton).Checked = True And init_a_f = False Then
            Select Case CType(sender, RadioButton).Name
                Case "rb_z100"
                    Me.COM_SEND("s_incz,100")
                    Exit Select
                Case "rb_z200"
                    Me.COM_SEND("s_incz,200")
                    Exit Select
                Case "rb_z255"
                    Me.COM_SEND("s_incz,255")
                    Exit Select
            End Select
        End If

    End Sub
    Private Sub rb_f_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_f100.CheckedChanged, _
                                                                                            rb_f200.CheckedChanged, _
                                                                                            rb_f255.CheckedChanged

        If Me.booting = True Then
            Return
        End If

        If CType(sender, RadioButton).Checked = True And init_a_f = False Then
            Select Case CType(sender, RadioButton).Name
                Case "rb_f100"
                    Me.COM_SEND("s_incf,100")
                    Exit Select
                Case "rb_f200"
                    Me.COM_SEND("s_incf,200")
                    Exit Select
                Case "rb_f255"
                    Me.COM_SEND("s_incf,255")
                    Exit Select
            End Select
        End If
    End Sub
    Private Sub rb_i_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rb_i100.CheckedChanged, _
                                                                                            rb_i200.CheckedChanged, _
                                                                                            rb_i255.CheckedChanged

        If Me.booting = True Then
            Return
        End If

        If CType(sender, RadioButton).Checked = True And init_a_f = False Then
            Select Case CType(sender, RadioButton).Name
                Case "rb_i100"
                    Me.COM_SEND("s_inci,100")
                    Exit Select
                Case "rb_i200"
                    Me.COM_SEND("s_inci,200")
                    Exit Select
                Case "rb_i255"
                    Me.COM_SEND("s_inci,255")
                    Exit Select
            End Select
        End If
    End Sub

    '===========================================================
    '
    '   プリセット設定
    '   
    '   
    '
    '===========================================================
    'move
    Private Sub btn_mvP1_Click(sender As System.Object, e As System.EventArgs) Handles btn_mvP1.Click
        Me.COM_SEND("move_p,1")
        Me.COM_SEND("read_a")
    End Sub

    Private Sub btn_mvP2_Click(sender As System.Object, e As System.EventArgs) Handles btn_mvP2.Click
        Me.COM_SEND("move_p,2")
        Me.COM_SEND("read_a")
    End Sub

    Private Sub btn_mvP3_Click(sender As System.Object, e As System.EventArgs) Handles btn_mvP3.Click
        Me.COM_SEND("move_p,3")
        Me.COM_SEND("read_a")
    End Sub

    Private Sub btn_mvP4_Click(sender As System.Object, e As System.EventArgs) Handles btn_mvP4.Click
        Me.COM_SEND("move_p,4")
        Me.COM_SEND("read_a")
    End Sub

    'set

    Private Sub btn_stP1_Click(sender As System.Object, e As System.EventArgs) Handles btn_stP1.Click
        Me.COM_SEND("s_prst,1," + tx_zoom.Text + "," + tx_focus.Text + "," + tx_iris.Text)
    End Sub

    Private Sub btn_stP2_Click(sender As System.Object, e As System.EventArgs) Handles btn_stP2.Click
        Me.COM_SEND("s_prst,2," + tx_zoom.Text + "," + tx_focus.Text + "," + tx_iris.Text)
    End Sub

    Private Sub btn_stP3_Click(sender As System.Object, e As System.EventArgs) Handles btn_stP3.Click
        Me.COM_SEND("s_prst,3," + tx_zoom.Text + "," + tx_focus.Text + "," + tx_iris.Text)
    End Sub

    Private Sub btn_stP4_Click(sender As System.Object, e As System.EventArgs) Handles btn_stP4.Click
        Me.COM_SEND("s_prst,4," + tx_zoom.Text + "," + tx_focus.Text + "," + tx_iris.Text)
    End Sub
End Class