2.12 Communications with a Windows PC (Visual Basic Application)
Using Automatic Reception with the MP3000 as a Slave
2-332
• Programming Example for Extended MEMOBUS Protocol Message Communications
The programming example that is given below demonstrates how to manage sockets and perform mes-
sage communications using the Extended MEMOBUS protocol.
The name of the template specified when creating this program example will be used as the name of
the target Windows-based application.
Module Memobus
Dim sock As Integer
Dim PC_addr, MP_addr As sockaddr_in
Dim from_addr As sockaddr_in
Dim sbuf(4095) As Byte
Dim rbuf(4095) As Byte
Dim DATAi, MDATAi As Short
Dim iSerial As Short
'------------------------------------------------------------------------------
' ’Open a TCP/UDP port
'------------------------------------------------------------------------------
Function MemobusMasterMain(ByVal TransPort As Short, _
ByVal MyIP As String, ByVal MyPort As Short, _
ByVal DstIP As String, ByVal DstPort As Short) As Short
Dim rc As Short
Dim ws_data As WSADATA
'---- Declaration to use Winsock.dll (must be declared before calling routines) ----
rc = WSAStartup(&H101S, ws_data)
If (rc <> 0) Then
MsgBox("Init error" & rc)
End If
'---- Initialize serial number setting ----
iSerial = 0
'---- Declare the PC’s IP address and port number ----
PC_addr.sin_family = AF_INET
PC_addr.sin_addr.s_addr = inet_addr(MyIP)
PC_addr.sin_port = htons(MyPort)
'---- Declare the MP3000’s IP address and port number ----
MP_addr.sin_family = AF_INET
MP_addr.sin_addr.s_addr = inet_addr(DstIP)
MP_addr.sin_port = htons(DstPort)
'---- Create the TCP or UDP socket ----
If (TransPort = 0) Then
sock = socket(AF_INET, SOCK_STREAM, 0)
Else
sock = socket(AF_INET, SOCK_DGRAM, 0)
End If