M2D-Laser-Scanner with Ethernet-Interface
Manual
HB-M2D-Ethernet-E
Page 36 of 39
www.MElsensor.com
E
E
t
t
h
h
e
e
r
r
n
n
e
e
t
t
W
W
i
i
n
n
S
S
o
o
c
c
k
k
I
I
m
m
p
p
l
l
e
e
m
m
e
e
n
n
t
t
a
a
t
t
i
i
o
o
n
n
To communicate with a M2D Ethernet–Scanner so called Windows Socket „WinSock“-functions are used. These functions are
part of all windows operating systems. Other operating systems provide similar functions.
„WinSock”-functions are encapsulated into a „ws2_32.dll“-file. These files belong to Windows.
The communication uses a TCP/IP-protocol with M2D Scanner working as a server. So the communication partner, in our
case the PC, has to be set up as a client. Before using the network functions the „WSAStartup“-function has to be called. So
the regular functions from „WinSock“ can be used.
Next a valid TCP-SOCKET (a kind of object) has to be received from the system. This is done by calling the function „socket“.
If there is a valid SOCKET, it is possible to establish a connection to the M2D Scanner. This will be made by the function
„connect“. After a successful call of this function, PC and scanner are connected and it is possible to exchange data with the
two commands „send“ and „recv“.
At the end of each communication stream all memories and sockets have to be released. This is done by using the two
functions „closesocket“ and „WSACleanup“ at the end.
E
E
x
x
a
a
m
m
p
p
l
l
e
e
:
:
(
(
“
“
C
C
”
”
)
)
u
u
s
s
i
i
n
n
g
g
W
W
i
i
n
n
S
S
o
o
c
c
k
k
a
a
n
n
d
d
S
S
c
c
a
a
n
n
n
n
e
e
r
r
f
f
u
u
n
n
c
c
t
t
i
i
o
o
n
n
s
s
:
:
// receiving block size should be modulo 2.048
// the Scanner sends blocks in size of 2.048 Bytes.
#define TCPBUFSIZE 2048
// structure for WinSocket
WSADATA wsaData;
// socket-Variable
SOCKET sTCP;
// receive buffer of the size mentioned above
char chBuffer[TCPBUFSIZE];
// number of received bytes from scanner
DWORD dwReceived = 0;
// permanently try to reach socket, when connection is broken
BOOL bRunSocket = TRUE;
// permanently try to establish connection, when receive function returns an error
BOOL bRunConnect = TRUE;
// permanently receive data from scanner
BOOL bRunRead = TRUE;
// define TimeOut in [ms] for receive function
// after this time, the “recv”-Function returns with dwReceived=0
// then the link will be locked and transmission established
DWORD dwRecvTimeOut = 10000;
// try to run WinSocket Version 2.1
if (WSAStartup (MAKEWORD(2, 1), &wsaData) != NULL)
{
AfxMessageBox("Fehler: WSAStartup", MB_OK | MB_ICONEXCLAMATION, NULL);
return;
}
//Structure for the “connect”-command
SOCKADDR_IN serv_addr;
// MUST have the value “AF_INET”
serv_addr.sin_family = AF_INET;
// transmit Port-Number of Scanner
serv_addr.sin_port = htons(atoi(“3000”));
// transmit IP-Adresse of Scanner
serv_addr.sin_addr.S_un.S_addr = inet_addr(“192.168.123.224”);
while(bRunSocket)
{
bRunConnect = TRUE;
// get Socket for TCP=SOCK_STREAM
sTCP = socket(AF_INET, SOCK_STREAM, 0);
// socket-Error?
if (sTCP == INVALID_SOCKET)
{
sTCP = 0;
bRunConnect = FALSE;
TRACE("SocketError\n");
}
// set TimeOut for “recv”-Function
setsockopt(sTCP, SOL_SOCKET, SO_RCVTIMEO, (const char*)&dwRecvTimeOut, sizeof(int));