IDK MSD-54 Series Command Guide User Manual

Page 8

Advertising
background image

MSD-54 Series Users Guide (Command Guide)

8

Example of programing with Microsoft Visual Basic.NET 2008

1:

Imports System

2: Imports System.Net.Sockets

3: Public Class Form1

4: Private stClient As TcpClient

'Client

5: Private stns As System.Net.Sockets.NetworkStream 'Stream

6: Public Function mOpen(ByVal pHostName As String, ByVal pPortNum As Integer) As

Boolean

7: '********************************************************

8: ' Open

9: ' Return value Succed: True Fail: False

10: '********************************************************

11: mOpen = False

'Initial Value

12: Try

13: 'Opening Client

14: stClient = New System.Net.Sockets.TcpClient(pHostName, pPortNum)

15: stClient.NoDelay = True 'Invalid transmit / receive delay

16: stns = stClient.GetStream() 'Open stream

17: If stns.CanTimeout Then

18: stns.ReadTimeout = 1000 'Time out (1000ms)

19: End If

20: mOpen = True 'Succeed

21: Catch ex As Exception

22: Console.WriteLine(ex.Message) 'Displaying unsual process

23: End Try

24:

25: End Function

26: Private Function mSendMessege(ByVal pMsg As String) As String

27: '********************************************************

28: ' Sending message

29: ' pMsg sending message

30: ' Return value Returned Charactor

31: '********************************************************

32: Dim dtBirth As DateTime 'Time out

33: Dim wNow As DateTime 'Current time

34: Dim pRecvMsg As String 'Return message

35: Dim bytes2(1024) As Byte

'Return message temporally store area (Byte type)

36: Dim bytesRead1 As Integer ‘Return message temporally store area (Integer type)

37: Dim word As Byte() 'Store area for temporary written data at the time of system output

38:

39: mSendMessege = "" 'Return value Clear

40: pRecvMsg = "" 'Work Area Clear

41: :

42: Try

43: '----Transmit Checking----

44: If stns.CanWrite Then

'Be able to write?

45: 'character encode

46: word = System.Text.Encoding.Default.GetBytes (pMsg + vbCrLf)

47: 'Output to socket

48: stns.Write(word, 0, word.Length)

49: Else

50: Exit Function

51: End If

52:

53: '----Receive----

54: dtBirth = DateTime.Now

55: dtBirth = dtBirth.AddSeconds(3)

'Time out after 3 sec.

56: Do

57: wNow = DateTime.Now 'Compare to current time

58: If (wNow > dtBirth) Then

59: Exit Do

'Stop process when it exceeds the set time (time out)

60: End If

61:

62: If stns.CanRead Then

'When readable

63: 'Read data

64: bytesRead2 = stns.Read(bytes2, 0, bytes2.Length)

65: 'Encode

66: pRecvMsg = pRecvMsg & _

67: System.Text.Encoding.Default.GetString(bytes2, 0, bytesRead2)

68: If pRecvMsg <> "" Then

Advertising