Option Strict On Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class Form1 Private P1arry(10) As Integer Private P2arry(10) As Integer Private P3arry(10) As Integer Private P1cn, P2cn, P3cn, P1s, P2s, P3s As Integer Private P1sts, P2sts, P3sts, C1 As Byte Private P1flg, P2plag, P3flg As Boolean Private Ch, Cm As Integer Private Ntdt As DateTime Private IPAddress As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer4.Interval = 1000 ' フォームの表示制御を行うタイマ4のインターバルを1sとするため1000をセット Call Timer4.Start() ' 1秒刻みのtaimer4をスタートします IPAddress = ComboBox1.Text ' コンボボックスからXPortのIPアドレスをセットします '  コンボボックスのデフォルトの値として一番利用する可能性のあるIPアドレスとする End Sub Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick Label4.Text = DateTime.Now.ToString Call TimeCheckP1() Call TimeCheckP2() Call TimeCheckP3() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick p1cn = p1cn + 1 ' 経過時間をカウントするカウンタ If P1arry(P1s) <= P1cn Then ' 各配列の値と比較 時間経過をチェック P1sts = CByte(P1sts Xor &H1) Call Sendcmd(IPAddress, &H1, P1sts) P1s = P1s + 1 If P1s > 10 Then P1s = 1 P1cn = 0 Else If P1arry(P1s) = P1cn Then P1s = 1 P1cn = 0 End If End If End If End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick p2cn = p2cn + 1 ' 経過時間をカウントするカウンタ If P2arry(P2s) <= P2cn Then ' 各配列の値と比較 時間経過をチェック P2sts = CByte(P2sts Xor &H2) Call Sendcmd(IPAddress, &H2, P2sts) P2s = P2s + 1 If P2s > 10 Then P2s = 1 P2cn = 0 Else If P2arry(P2s) = P2cn Then P2s = 1 P2cn = 0 End If End If End If End Sub Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick p3cn = p3cn + 1 ' 経過時間をカウントするカウンタ If P3arry(P3s) <= P3cn Then ' 各配列の値と比較 時間経過をチェック P3sts = CByte(P3sts Xor &H4) Call Sendcmd(IPAddress, &H4, P3sts) P3s = P3s + 1 If P3s > 10 Then P3s = 1 P3cn = 0 Else If P3arry(P3s) = P3cn Then P3s = 1 P3cn = 0 End If End If End If End Sub Private Sub P1stb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1stb.Click 'MessageBox.Show(IPAddress) 'IPアドレスの確認のメッセージボックス 必要に応じてコメントを外しIPアドレスを確認する If P1stb.Text = "Start" Then ' ボタンの表示は "start" かチェック Timer1.Interval = CInt(P1TMT.Text) ' start なので インターバル値をセット P1cn = 0 '計時のためのカウンタをゼロクリア Call subpgmp1() 'テキストボックスの値を配列にセットするプロシージャ If P1RBon.Checked = True Then ' ラジオボタンのチェック ONのチェックで p1sts=1 P1sts = 1 Else P1sts = 0 End If Call Timer1.Start() ' タイマ1のスタート P1stb.Text = "Stop" ' ボタンをタイマストップボタンに設定 P1flg = True P1s = 1 Else Call Timer1.Stop() P1stb.Text = "Start" End If End Sub Private Sub P2stb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P2stb.Click If P2stb.Text = "Start" Then Timer2.Interval = CInt(P2TMT.Text) P2cn = 0 Call subpgmp2() If P2RBon.Checked = True Then P2sts = 2 Else P2sts = 0 End If Call Timer2.Start() ' タイマ2のスタート P2stb.Text = "Stop" ' ボタンをタイマストップボタンに設定 P2s = 1 Else Call Timer2.Stop() P2stb.Text = "Start" End If End Sub Private Sub P3stb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P3stb.Click If P3stb.Text = "Start" Then Timer3.Interval = CInt(P3TMT.Text) P3cn = 0 Call subpgmp3() If P3RBon.Checked = True Then P3sts = 4 Else P3sts = 0 End If Call Timer3.Start() ' タイマ3のスタート P3stb.Text = "Stop" ' ボタンをタイマストップボタンに設定 P3s = 1 Else Call Timer3.Stop() P3stb.Text = "Start" End If End Sub Private Sub Sendcmd(ByVal ipstring As String, ByVal dat1 As Byte, ByVal dat2 As Byte) Dim XPsocket As Socket Dim Rn As Integer Dim Sdat(8), Rdat(4) As Byte Dim XPstream As NetworkStream Dim Binwriter As BinaryWriter Dim Binreader As BinaryReader XPsocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Try XPsocket.Connect(ipstring, 30704) XPstream = New NetworkStream(XPsocket) Binwriter = New BinaryWriter(XPstream) Binreader = New BinaryReader(XPstream) For Rn = 0 To 8 Sdat(Rn) = 0 Next Sdat(0) = &H1B Sdat(1) = dat1 Sdat(5) = dat2 Call Binwriter.Write(Sdat) Call Binwriter.Flush() For Rn = 0 To 4 Rdat(Rn) = Binreader.ReadByte Next Rn XPsocket.Close() Catch Call Timer1.Stop() Call Timer2.Stop() Call Timer3.Stop() MessageBox.Show("アドレスを確認してください " + ipstring) Finally End Try End Sub Private Sub subpgmp1() If P1TB1.Text = "" Then P1TB1.Text = "0" End If p1arry(1) = CInt(P1TB1.Text) If P1TB2.Text = "" Then P1TB2.Text = "0" End If P1arry(2) = CInt(P1TB2.Text) + P1arry(1) If P1TB3.Text = "" Then P1TB3.Text = "0" End If P1arry(3) = CInt(P1TB3.Text) + P1arry(2) If P1TB4.Text = "" Then P1TB4.Text = "0" End If P1arry(4) = CInt(P1TB4.Text) + P1arry(3) If P1TB5.Text = "" Then P1TB5.Text = "0" End If P1arry(5) = CInt(P1TB5.Text) + P1arry(4) If P1TB6.Text = "" Then P1TB6.Text = "0" End If P1arry(6) = CInt(P1TB6.Text) + P1arry(5) If P1TB7.Text = "" Then P1TB7.Text = "0" End If P1arry(7) = CInt(P1TB7.Text) + P1arry(6) If P1TB8.Text = "" Then P1TB8.Text = "0" End If P1arry(8) = CInt(P1TB8.Text) + P1arry(7) If P1TB9.Text = "" Then P1TB9.Text = "0" End If P1arry(9) = CInt(P1TB9.Text) + P1arry(8) If P1TB10.Text = "" Then P1TB10.Text = "0" End If P1arry(10) = CInt(P1TB10.Text) + P1arry(9) End Sub Private Sub subpgmp2() If P2TB1.Text = "" Then P2TB1.Text = "0" End If p2arry(1) = CInt(P2TB1.Text) If P2TB2.Text = "" Then P2TB2.Text = "0" End If P2arry(2) = CInt(P2TB2.Text) + P2arry(1) If P2TB3.Text = "" Then P2TB3.Text = "0" End If P2arry(3) = CInt(P2TB3.Text) + P2arry(2) If P2TB4.Text = "" Then P2TB4.Text = "0" End If P2arry(4) = CInt(P2TB4.Text) + P2arry(3) If P2TB5.Text = "" Then P2TB5.Text = "0" End If P2arry(5) = CInt(P2TB5.Text) + P2arry(4) If P2TB6.Text = "" Then P2TB6.Text = "0" End If P2arry(6) = CInt(P2TB6.Text) + P2arry(5) If P2TB7.Text = "" Then P2TB7.Text = "0" End If P2arry(7) = CInt(P2TB7.Text) + P2arry(6) If P2TB8.Text = "" Then P2TB8.Text = "0" End If P2arry(8) = CInt(P2TB8.Text) + P2arry(7) If P2TB9.Text = "" Then P2TB9.Text = "0" End If P2arry(9) = CInt(P2TB9.Text) + P2arry(8) If P2TB10.Text = "" Then P2TB10.Text = "0" End If P2arry(10) = CInt(P2TB10.Text) + P2arry(9) End Sub Private Sub subpgmp3() If P3TB1.Text = "" Then P3TB1.Text = "0" End If p3arry(1) = CInt(P3TB1.Text) If P3TB2.Text = "" Then P3TB2.Text = "0" End If p3arry(2) = CInt(P3TB2.Text) + p3arry(1) If P3TB3.Text = "" Then P3TB3.Text = "0" End If p3arry(3) = CInt(P3TB3.Text) + p3arry(2) If P3TB4.Text = "" Then P3TB4.Text = "0" End If p3arry(4) = CInt(P3TB4.Text) + p3arry(3) If P3TB5.Text = "" Then P3TB5.Text = "0" End If p3arry(5) = CInt(P3TB5.Text) + p3arry(4) If P3TB6.Text = "" Then P3TB6.Text = "0" End If p3arry(6) = CInt(P3TB6.Text) + p3arry(5) If P3TB7.Text = "" Then P3TB7.Text = "0" End If p3arry(7) = CInt(P3TB7.Text) + p3arry(6) If P3TB8.Text = "" Then P3TB8.Text = "0" End If p3arry(8) = CInt(P3TB8.Text) + p3arry(7) If P3TB9.Text = "" Then P3TB9.Text = "0" End If p3arry(9) = CInt(P3TB9.Text) + p3arry(8) If P3TB10.Text = "" Then P3TB10.Text = "0" End If p3arry(10) = CInt(P3TB10.Text) + p3arry(9) End Sub Private Sub SyncStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SyncStart.Click Call P3stb_Click(sender, e) Call P1stb_Click(sender, e) Call P2stb_Click(sender, e) End Sub Private Sub TimeCheckP1() Dim timp1i, timp1s As DateTime timp1i = DTimeP1I.Value timp1s = DTimeP1S.Value Ntdt = DateTime.Now If TimecheckBt1.Text = "ON" Then ' 時刻制御のボタンがONになっていることを確認 If timp1i.TimeOfDay > timp1s.TimeOfDay Then '開始時刻 > 終了時刻か チェック If (timp1i.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp1s.TimeOfDay) Then ' 開始時刻 > 現在時刻> 終了時刻 If Timer1.Enabled = True Then ' 現在は制御停止の期間 Call Timer1.Stop() TimecheckBt1.BackColor = Color.Beige End If Else ' 開始時間>終了時間>現在時間 TimecheckBt1.BackColor = Color.Red ' 現在は制御中 If Timer1.Enabled = False Then Call Timer1.Start() End If End If Else ' 終了時間 > 開始時間 終了時間 >現在時間 >開始時間 If (timp1s.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp1i.TimeOfDay) Then ''終了時間>現在時間 現在時間 > 開始時間 TimecheckBt1.BackColor = Color.Red ' 現在は制御中 If Timer1.Enabled = False Then Call Timer1.Start() End If Else If Timer1.Enabled = True Then ' 現在は制御停止の期間 Call Timer1.Stop() TimecheckBt1.BackColor = Color.Beige End If End If End If End If End Sub Private Sub TimeCheckP2() Dim timp2i, timp2s As DateTime timp2i = DTimeP2I.Value timp2s = DTimeP2S.Value ntdt = DateTime.Now If TimeCheckBt2.Text = "ON" Then ' 時刻制御のボタンがONになっていることを確認 If timp2i.TimeOfDay > timp2s.TimeOfDay Then '開始時刻 > 終了時刻か チェック If (timp2i.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp2s.TimeOfDay) Then ' 開始時刻 > 現在時刻> 終了時刻 If Timer2.Enabled = True Then ' 現在は制御停止の期間 Call Timer2.Stop() TimeCheckBt2.BackColor = Color.Beige End If Else ' 開始時間>終了時間>現在時間 TimeCheckBt2.BackColor = Color.Red ' 現在は制御中 If Timer2.Enabled = False Then Call Timer2.Start() End If End If Else ' 終了時間 > 開始時間 終了時間 >現在時間 >開始時間 If (timp2s.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp2i.TimeOfDay) Then ''終了時間>現在時間 現在時間 > 開始時間 TimeCheckBt2.BackColor = Color.Red ' 現在は制御中 If Timer2.Enabled = False Then Call Timer2.Start() End If Else If Timer2.Enabled = True Then ' 現在は制御停止の期間 Call Timer2.Stop() TimeCheckBt2.BackColor = Color.Beige End If End If End If End If End Sub Private Sub TimeCheckP3() Dim timp3i, timp3s As DateTime timp3i = DTimeP3I.Value timp3s = DTimeP3S.Value Ntdt = DateTime.Now If TimeCheckBt3.Text = "ON" Then ' 時刻制御のボタンがONになっていることを確認 If timp3i.TimeOfDay > timp3s.TimeOfDay Then '開始時刻 > 終了時刻か チェック If (timp3i.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp3s.TimeOfDay) Then ' 開始時刻 > 現在時刻> 終了時刻 If Timer3.Enabled = True Then ' 現在は制御停止の期間 Call Timer3.Stop() TimeCheckBt3.BackColor = Color.Beige End If Else ' 開始時間>終了時間>現在時間 TimeCheckBt3.BackColor = Color.Red ' 現在は制御中 If Timer3.Enabled = False Then Call Timer3.Start() End If End If Else ' 終了時間 > 開始時間 終了時間 >現在時間 >開始時間 If (timp3s.TimeOfDay > Ntdt.TimeOfDay) And (Ntdt.TimeOfDay > timp3i.TimeOfDay) Then ''終了時間>現在時間 現在時間 > 開始時間 TimeCheckBt3.BackColor = Color.Red ' 現在は制御中 If Timer3.Enabled = False Then Call Timer3.Start() End If Else If Timer3.Enabled = True Then ' 現在は制御停止の期間 Call Timer3.Stop() TimeCheckBt3.BackColor = Color.Beige End If End If End If End If End Sub Private Sub TimecheckBt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimecheckBt1.Click Dim ret As DialogResult If TimecheckBt1.Text = "ON" Then ret = MessageBox.Show("時刻制御稼動中です停止しますか", "停止確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimecheckBt1.Text = "OFF" TimecheckBt1.BackColor = Color.Beige End If Else ret = MessageBox.Show("時刻制御停止中です稼動しますか", "稼動確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimecheckBt1.Text = "ON" End If End If ' Call TimeCheckP1() End Sub Private Sub TimeCheckBt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimeCheckBt2.Click Dim ret As DialogResult If TimeCheckBt2.Text = "ON" Then ret = MessageBox.Show("時刻制御稼動中です停止しますか", "停止確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimeCheckBt2.Text = "OFF" TimeCheckBt2.BackColor = Color.Beige End If Else ret = MessageBox.Show("時刻制御停止中です稼動しますか", "稼動確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimeCheckBt2.Text = "ON" End If End If End Sub Private Sub TimeCheckBt3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimeCheckBt3.Click Dim ret As DialogResult If TimeCheckBt3.Text = "ON" Then ret = MessageBox.Show("時刻制御稼動中です停止しますか", "停止確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimeCheckBt3.Text = "OFF" TimeCheckBt3.BackColor = Color.Beige End If Else ret = MessageBox.Show("時刻制御停止中です稼動しますか", "稼動確認", MessageBoxButtons.OKCancel) If ret = Windows.Forms.DialogResult.OK Then TimeCheckBt3.Text = "ON" End If End If End Sub Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged IPAddress = ComboBox1.Text 'IPアドレス設定が変更されたときのこの処理が実行される End Sub End Class