S p e z i a l e l e k t r o n i k G m b H
iseg Spezialelektronik GmbH
Email: sales@iseg-hv.de
Tel ++ 49 (0) 351 / 26 996 - 0
Bautzner Landstr. 23
http://www.iseg-hv.de
Fax ++ 49 (0) 351 / 26 996 - 21
D - 01454 Radeberg/ Rossendorf
23
Programming
Simple programming example (without error handling) for communication with the HV device over Ethernet. This program was
compiled and tested with Microsoft Visual C++ 6.0 on Windows XP.
#include <stdio.h>
#include <winsock.h>
int
main(
int
argc,
char
*argv[])
{
WSADATA
wsadata;
SOCKET
sock;
SOCKADDR_IN
sockaddr_in;
int
retcode;
char
cmd[
255
]
=
"*IDN?\r\n"
;
char
ans[
255
];
// init sockets (Berkeley style, UNIX compatible)
WSAStartup(
2
,
&wsadata);
// create TCP socket
sock
=
socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
// bind socket to dynamic port
memset(&sockaddr_in,
0
,
sizeof
(sockaddr_in));
sockaddr_in.sin_family
=
AF_INET;
// UDP, TCP
sockaddr_in.sin_port
=
htons(
10001
);
// Remote Port
sockaddr_in.sin_addr.S_un.S_un_b.s_b1
=
192
;
// IP address
sockaddr_in.sin_addr.S_un.S_un_b.s_b2
=
168
;
sockaddr_in.sin_addr.S_un.S_un_b.s_b3
=
16
;
sockaddr_in.sin_addr.S_un.S_un_b.s_b4
=
13
;
// connect to server (three way handshake)
connect(sock,
(SOCKADDR
*)&sockaddr_in,
sizeof
(SOCKADDR_IN));
// send command to server
send(sock,
cmd,
strlen(cmd),
0
);
// read answer from server
retcode
=
recv(sock,
ans,
sizeof
(ans),
0
);
// close socket (three way handshake) and clean up
closesocket(sock);
WSACleanup();
// print answer to screen
printf(
"%s\n"
,
ans);
return
0
;
}