プリンタに印刷をした際にWSD001A-Jを再生させる


VBプログラムからプリンタへの印刷とWSD001A-Jの再生を同時に行うサンプルプログラム



このプログラムでは、キッチンプリンタの制御(本サンプルではシリアル通信で作成)および、
WSD001A-Jの制御(接続先IPアドレス、音設定、LED設定)を行います。
※ サンプルプログラム内の192.168.1.199はWSD001A-JのIPアドレスです。このIPアドレスはお客様のネットワーク環境に合わせてIPアドレスを設定してください。 本プログラムは参考用であり、すべてのお客様の環境で動作を保証するものではありません。 実用のプログラムに組み込む場合、エラー処理等を組み込んでください。
サンプルソース

Imports System.Net
Imports System.IO
Imports System.Text
-------------------------------------------------------------------------------------------------------------------------------
Public Class DemoForm

Dim msgdata As String = ""
-------------------------------------------------------------------------------------------------------------------------------
' フォームロード時、初期値を設定
Private Sub DemoForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

CmbBaud.Items.Add("9600")
CmbBaud.Items.Add("19200")
CmbBaud.Items.Add("38400")
CmbBaud.SelectedIndex = 2

Dim cnt As Integer = 0
Try
 For Each sp As String In My.Computer.Ports.SerialPortNames
  CmbCOM.Items.Add(sp)
  cnt = cnt + 1
 Next
Catch ex As Exception
 MessageBox.Show(ex.Message)
End Try
If cnt > 0 Then
 CmbCOM.SelectedIndex = 0
End If

TbxIP.Text = Global.WSD001A_J_Printer_Demo.My.Settings.IPAddress

CmbSound.Items.Add("SDカード")
CmbSound.Items.Add("内蔵メモリ")
CmbSound.SelectedIndex = 1

CmbRepeat.Items.Add("なし")
CmbRepeat.Items.Add("1曲")
CmbRepeat.Items.Add("フォルダ")
CmbRepeat.SelectedIndex = 0

NumVol.Value = 5

CmbColor.Items.Add("なし")
CmbColor.Items.Add("赤")
CmbColor.Items.Add("オレンジ")
CmbColor.Items.Add("黄色")
CmbColor.Items.Add("緑")
CmbColor.Items.Add("水色")
CmbColor.Items.Add("青")
CmbColor.Items.Add("紫")
CmbColor.SelectedIndex = 1

For i As Integer = 5 To 30 Step 5
 CmbTimes.Items.Add(i.ToString)
Next
CmbTimes.SelectedIndex = 0

End Sub
-------------------------------------------------------------------------------------------------------------------------------
' フォームを閉じる
Private Sub DemoForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
 ' My.SettingsにIPアドレスを保存
 Global.WSD001A_J_Printer_Demo.My.Settings.IPAddress = TbxIP.Text
 My.Settings.Save()
End Sub
-------------------------------------------------------------------------------------------------------------------------------
' プリンタ印字 + WSD001A-J再生ボタン押下時
Private Sub BtnPrint1_Click(sender As System.Object, e As System.EventArgs) Handles BtnPrintPlay.Click

 ' WSD001A-J再生
 Dim res As Integer = Play()
 If res = 0 Then
  ' 印字内容作成
  MakeMsg()
  ' 印字
  Print(msgdata)
 End If

End Sub
-------------------------------------------------------------------------------------------------------------------------------
' プリンタ印字ボタン押下時
Private Sub BtnPrint_Click(sender As System.Object, e As System.EventArgs) Handles BtnPrint.Click
 ' 印字内容作成
 MakeMsg()
 ' 印字
 Print(msgdata)
End Sub
-------------------------------------------------------------------------------------------------------------------------------
' 再生ボタン押下時
Private Sub BtnPlay_Click(sender As System.Object, e As System.EventArgs) Handles BtnPlay.Click
 ' WSD001A-J再生
 Dim res As Integer = Play()
End Sub
-------------------------------------------------------------------------------------------------------------------------------
' 停止ボタン押下時
Private Sub BtnStop_Click(sender As System.Object, e As System.EventArgs) Handles BtnStop.Click

 Dim status As String = ""

 Try
  ' WSD001A-Jの状態確認
  status = SendHttpRequest("http://" + TbxIP.Text.Trim + "/status")
 Catch ex As Exception
  MessageBox.Show(ex.Message)
  Return
 End Try

 ' WSD001A-Jが再生中の場合
 If status = "3" Then
  ' HTTPリクエストにて停止コマンド送信
  SendHttpRequest("http://" + TbxIP.Text.Trim + "/stop")
 End If

End Sub
-------------------------------------------------------------------------------------------------------------------------------
' WSD001A-J再生
Private Function Play() As Integer

 Dim status As String = ""
 Dim ret As Integer = 0

 Try
  ' WSD001A-Jの状態確認
  status = SendHttpRequest("http://" + TbxIP.Text.Trim + "/status")
 Catch ex As Exception
  MessageBox.Show(ex.Message)
  Return 1
 End Try

' WSD001A-Jが停止中の場合
If status = "2" Then

Dim ip As String = TbxIP.Text.Trim
Dim device As String = CmbSound.SelectedIndex.ToString
Dim volume As String = NumVol.Text
Dim repeat As String = CmbRepeat.SelectedIndex.ToString
Dim folder As String = NumFolder.Text
Dim file As String = NumFile.Text
Dim ledcolor As String = CmbColor.SelectedIndex.ToString
Dim ledtimes As String = CmbTimes.SelectedIndex.ToString

' フォームで選択した各値をパラメタに再生コマンド作成
Dim playUri As String = "http://" + ip + "/play?" + _
"dev=" + device + _
"&vol=" + volume + _
"&rep=" + repeat + _
"&fol=" + folder + _
"&tck=" + file + _
"&led=" + ledcolor + _
"&num=" + ledtimes

Try
' HTTPリクエストにて再生コマンド送信
Dim response As String = SendHttpRequest(playUri)
' レスポンスにエラーが返ってきた場合
If response.Substring(0, 1) = "E" Then
MessageBox.Show(response)
ret = 1
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
ret = 1
End Try

Else
MessageBox.Show("再生中です")
ret = 1
End If

Return ret

End Function
-------------------------------------------------------------------------------------------------------------------------------
' HTTPリクエスト送信
Private Function SendHttpRequest(ByVal uri As String) As String

Dim rt As String = ""

Try
Dim request As WebRequest = WebRequest.Create(uri)
request.Timeout = 1000
Dim response As WebResponse = request.GetResponse()
Dim st As Stream = response.GetResponseStream()
Dim sr As StreamReader = New StreamReader(st, Encoding.GetEncoding("UTF-8"))
rt = sr.ReadToEnd()
sr.Close()
st.Close()

Catch ex As Exception
Throw ex
End Try

Return rt

End Function
-------------------------------------------------------------------------------------------------------------------------------
' 印字
Private Sub Print(ByVal msg As String)

Try
SerialPort1.BaudRate = CmbBaud.Text
SerialPort1.PortName = CmbCOM.Text
SerialPort1.Parity = Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = Ports.StopBits.One
SerialPort1.Encoding = System.Text.Encoding.GetEncoding(932)
SerialPort1.WriteTimeout = 2000

SerialPort1.Open()
SerialPort1.Write(msg)
While SerialPort1.BytesToWrite > 0
End While
SerialPort1.Close()

Catch ex As Exception

SerialPort1.Close()
MessageBox.Show(ex.Message)

End Try

End Sub
-------------------------------------------------------------------------------------------------------------------------------
' 印字内容を作成
Private Sub MakeMsg()

msgdata = Chr(&H1B) + Chr(&H40) + _
Chr(&H1B) + Chr(&H74) + Chr(&H1) + _
Chr(&H1C) + Chr(&H43) + Chr(&H1) + _
Chr(&H1B) + Chr(&H52) + Chr(&H8) + _
Chr(&H1B) + Chr(&H61) + Chr(&H1) + _
Chr(&H1D) + Chr(&H62) + Chr(&H1) + _
"テクノベインズ湯島店" + _
Chr(&HA) + _
"注文番号 No.0001" + _
Chr(&HA) + _
Date.Now + _
Chr(&H1B) + Chr(&H64) + Chr(&H2) + _
Chr(&H1B) + Chr(&H61) + Chr(&H0) + _
String.Format("{0, 18}", "スパゲッティミートソース") + _
Chr(&HA) + _
String.Format("{0, 9}", "600") + _
String.Format("{0, -6}", " X 1個") + _
String.Format("{0, 13}", "600円") + _
Chr(&HA) + _
String.Format("{0, 10}", "コーヒー") + _
Chr(&HA) + _
String.Format("{0, 9}", "120") + _
String.Format("{0, -6}", " X 1個") + _
String.Format("{0, 13}", "120円") + _
Chr(&H1B) + Chr(&H64) + Chr(&H5) + _
Chr(&H1D) + Chr(&H56) + Chr(&H1)

End Sub
-------------------------------------------------------------------------------------------------------------------------------
' 音源選択時
Private Sub CmbSound_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CmbSound.SelectedIndexChanged

If CmbSound.SelectedIndex = 1 Then
' 音源が内蔵メモリの場合 フォルダ選択不可・ファイル名は1から12まで選択可
NumFolder.ReadOnly = True
NumFile.Maximum = 12
Else
' 音源がSDカードの場合 フォルダ選択可・ファイル名は1から254まで選択可
NumFolder.ReadOnly = False
NumFile.Maximum = 254
End If
End Sub


End Class
-------------------------------------------------------------------------------------------------------------------------------