Erster Test von Jörn
This commit is contained in:
parent
9041f0b0d2
commit
83103d679e
6 changed files with 12977 additions and 0 deletions
74
erster test/IPCommon.can
Normal file
74
erster test/IPCommon.can
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*@@var:*/
|
||||
variables
|
||||
{
|
||||
const long INVALID_SOCKET = ~0;
|
||||
const long WSA_IO_PENDING = 997;
|
||||
const long WSAEWOULDBLOCK = 10035;
|
||||
const dword INVALID_IP = 0xffffffff;
|
||||
|
||||
dword gIpAddress = INVALID_IP;
|
||||
char gIpLastErrStr[1024] = "";
|
||||
char gIpAddressStr[32] = "";
|
||||
int gIpLastErr = 0;
|
||||
|
||||
dword gUdpPort = 0;
|
||||
long gUdpSocket = INVALID_SOCKET;
|
||||
char gUdpRxBuffer[4096];
|
||||
|
||||
dword gTcpPort = 0;
|
||||
long gTcpSocket = INVALID_SOCKET;
|
||||
long gTcpDataSocket = INVALID_SOCKET;
|
||||
char gTcpRxBuffer[8192];
|
||||
|
||||
// status
|
||||
int gStatus = 0;
|
||||
const int gkSTATUS_UNINITIALISED = 0;
|
||||
const int gkSTATUS_INITIALISED = 1;
|
||||
}
|
||||
/*@@end*/
|
||||
|
||||
/*@@caplFunc:UdpReceive(dword):*///function
|
||||
long UdpRecv( dword socket)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
result = UdpReceiveFrom( socket, gUdpRxBuffer, elcount( gUdpRxBuffer));
|
||||
|
||||
if ( 0 != result)
|
||||
{
|
||||
gIpLastErr = IpGetLastSocketError( socket);
|
||||
|
||||
if ( WSA_IO_PENDING != gIpLastErr)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "UdpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/*@@end*/
|
||||
|
||||
/*@@caplFunc:<newFunction>:*///function
|
||||
long TcpRecv( dword socket)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
result = TcpReceive( socket, gTcpRxBuffer, elcount( gTcpRxBuffer));
|
||||
|
||||
if ( 0 != result)
|
||||
{
|
||||
gIpLastErr = IpGetLastSocketError( socket);
|
||||
|
||||
if ( WSA_IO_PENDING != gIpLastErr)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/*@@end*/
|
276
erster test/IPServer.can
Normal file
276
erster test/IPServer.can
Normal file
|
@ -0,0 +1,276 @@
|
|||
includes
|
||||
{
|
||||
#include "IPCommon.can"
|
||||
}
|
||||
|
||||
variables
|
||||
{
|
||||
int adapterIndex = 1;
|
||||
char Server_IP[16]="";
|
||||
}
|
||||
|
||||
void SetupIp()
|
||||
{
|
||||
|
||||
char text[512] = "";
|
||||
char info[512] = "";
|
||||
int size = 512;
|
||||
long result = 0;
|
||||
dword addresses[1];
|
||||
|
||||
writeClear(0);
|
||||
|
||||
if (1 > IpGetAdapterCount())
|
||||
{
|
||||
writelineex(0, 3, "Error: There is no network interface available!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
//
|
||||
// while (adapterIndex<=IpGetAdapterCount())
|
||||
// {
|
||||
// IpGetAdapterDescription(adapterIndex, text, size);
|
||||
// write("Info: Interface %d von %d: %s", adapterIndex, IpGetAdapterCount(), text);
|
||||
// adapterIndex++;
|
||||
// };
|
||||
//
|
||||
|
||||
if (0 != IpGetAdapterAddress(adapterIndex, addresses, 1))
|
||||
{
|
||||
writelineex(0, 3, "Error: Could not retrieve Ip address!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
gIpAddress = addresses[0]; // the interface used
|
||||
|
||||
if (INVALID_IP == gIpAddress)
|
||||
{
|
||||
writelineex(0, 3, "Error: Ip address to be used is invalid!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
IpGetAdapterDescription(adapterIndex, text, size);
|
||||
snprintf(info, size, "Interface: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
IpGetAdapterAddressAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Ip address: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
strncpy(Server_IP, text,16);
|
||||
sysSetVariableString(sysvar::TCPIP::TcpServerIp, text);
|
||||
|
||||
IpGetAdapterMaskAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Subnet mask: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
IpGetAdapterGatewayAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Gateway address: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
gStatus = gkSTATUS_INITIALISED;
|
||||
}
|
||||
|
||||
on start
|
||||
{
|
||||
SetupIp();
|
||||
StartListenTcp();
|
||||
}
|
||||
|
||||
on stopMeasurement
|
||||
{
|
||||
ResetIp();
|
||||
}
|
||||
|
||||
void OnTcpListen( dword socket, long result)
|
||||
{
|
||||
if (gTcpSocket != socket)
|
||||
{
|
||||
writelineex( 0, 2, "OnTcpListen called for unexpected socket (%d).", socket);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 0 != result)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "OnTcpListen error (%d, %s).",
|
||||
IpGetLastSocketError( socket), gIpLastErrStr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (gTcpDataSocket != INVALID_SOCKET)
|
||||
{
|
||||
TcpClose(gTcpDataSocket);
|
||||
}
|
||||
|
||||
gTcpDataSocket = TcpAccept( socket);
|
||||
|
||||
if ( INVALID_SOCKET == gTcpDataSocket)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "Error: TcpAccept (%d): %s",
|
||||
IpGetLastSocketError( socket), gIpLastErrStr);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
TcpRecv( gTcpDataSocket);
|
||||
|
||||
writelineex( 0, 1, "Status: Tcp connection established.");
|
||||
}
|
||||
|
||||
long OnTcpReceive( dword socket, long result, dword address, dword port, char buffer[], dword size)
|
||||
{
|
||||
char addressString[64] = "";
|
||||
|
||||
if ( gTcpDataSocket != socket)
|
||||
{
|
||||
writelineex(0, 2, "OnTcpReceive called for unknown socket 0x%X", socket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (0 != result)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "OnTcpReceive error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
IpGetAddressAsString(address, addressString, elcount(addressString));
|
||||
|
||||
strncat (buffer, " Received", elcount(buffer)+10);
|
||||
SysSetVariableString(sysvar::TCPIP::TcpData, buffer);
|
||||
SysSetVariableString(sysvar::TCPIP::Connect_IP, addressString);
|
||||
|
||||
@sysvar::TCPIP::Connect_Port = port;
|
||||
SendTcpData();
|
||||
TcpRecv( socket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void OnTcpSend( dword socket, long result, char buffer[], dword size)
|
||||
{
|
||||
if ( gTcpDataSocket != socket)
|
||||
{
|
||||
writelineex(0, 2, "OnTcpSend called for unknown socket 0x%X", socket);
|
||||
}
|
||||
|
||||
if (0 != result)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "OnTcpSend error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
writelineex( 0, 1, "Tcp data sent successfully!");
|
||||
}
|
||||
}
|
||||
|
||||
void StartListenTcp()
|
||||
{
|
||||
gTcpPort = @sysvar::TCPIP::TcpServerPort;
|
||||
|
||||
gTcpSocket = TcpOpen(gIpAddress, gTcpPort);
|
||||
|
||||
if ( INVALID_SOCKET == gTcpSocket)
|
||||
{
|
||||
writelineex(0, 1, "Error: could not create Tcp socket!");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
writelineex(0, 1, "Successfully created Tcp socket.");
|
||||
|
||||
TcpListen(gTcpSocket);
|
||||
|
||||
writelineex( 0, 1, "Listening for incoming Tcp connections on port %d", gTcpPort);
|
||||
}
|
||||
|
||||
void StopListenTcp()
|
||||
{
|
||||
if (INVALID_SOCKET != gTcpDataSocket)
|
||||
{
|
||||
TcpClose(gTcpDataSocket);
|
||||
|
||||
gTcpDataSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (INVALID_SOCKET != gTcpSocket)
|
||||
{
|
||||
TcpClose(gTcpSocket);
|
||||
|
||||
gTcpSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
writelineex(0, 1, "Tcp socket is closed.");
|
||||
}
|
||||
|
||||
void SendTcpData()
|
||||
{
|
||||
char buffer[560];
|
||||
|
||||
SysGetVariableString(sysvar::TCPIP::TcpData, buffer, elcount(buffer));
|
||||
|
||||
if (INVALID_SOCKET != gTcpDataSocket)
|
||||
|
||||
if (0 != TcpSend( gTcpDataSocket, buffer, strlen(buffer)))
|
||||
{
|
||||
gIpLastErr = IpGetLastSocketError( gTcpDataSocket);
|
||||
|
||||
if ( WSA_IO_PENDING != gIpLastErr)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "TcpSend error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
writelineex( 0, 1, "Tcp data sent successfully!");
|
||||
}
|
||||
}
|
||||
|
||||
void ResetIp()
|
||||
{
|
||||
if (INVALID_SOCKET != gTcpDataSocket)
|
||||
{
|
||||
TcpClose(gTcpDataSocket);
|
||||
|
||||
gTcpDataSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (INVALID_SOCKET != gTcpSocket)
|
||||
{
|
||||
TcpClose(gTcpSocket);
|
||||
|
||||
gTcpSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
on key 'a'
|
||||
{
|
||||
SendTcpData();
|
||||
}
|
||||
|
||||
on key 's'
|
||||
{
|
||||
StartListenTcp();
|
||||
}
|
||||
|
||||
|
||||
on key 'S'
|
||||
{
|
||||
StopListenTcp();
|
||||
}
|
6412
erster test/modbus.cfg
Normal file
6412
erster test/modbus.cfg
Normal file
File diff suppressed because it is too large
Load diff
18
erster test/sender.can
Normal file
18
erster test/sender.can
Normal file
|
@ -0,0 +1,18 @@
|
|||
includes
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
variables
|
||||
{
|
||||
int gedrueckt=0;
|
||||
}
|
||||
|
||||
on sysvar senden
|
||||
{
|
||||
if (gedrueckt==1) {return;};
|
||||
if (@senden==0) {gedrueckt=0;}; // Rücksetzen wenn Button losgelassen
|
||||
|
||||
write ("Gedrueckt");
|
||||
|
||||
}
|
191
erster test/socket.can
Normal file
191
erster test/socket.can
Normal file
|
@ -0,0 +1,191 @@
|
|||
includes
|
||||
{
|
||||
#include "IPCommon.can"
|
||||
}
|
||||
|
||||
variables
|
||||
{
|
||||
char Local_IP[15];
|
||||
// char Server_IP[15]="192.168.178.49";
|
||||
}
|
||||
|
||||
void SetupIp()
|
||||
{
|
||||
int adapterIndex = 1;
|
||||
char text[512] = "";
|
||||
char info[512] = "";
|
||||
int size = 512;
|
||||
long result = 0;
|
||||
dword addresses[1];
|
||||
|
||||
writeClear(0);
|
||||
|
||||
if (1 > IpGetAdapterCount())
|
||||
{
|
||||
writelineex(0, 3, "Error: There is no network interface available!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
if (0 != IpGetAdapterAddress(adapterIndex, addresses, 1))
|
||||
{
|
||||
writelineex(0, 3, "Error: Could not retrieve ip address!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
gIpAddress = addresses[0]; // the interface used
|
||||
|
||||
if (INVALID_IP == gIpAddress)
|
||||
{
|
||||
writelineex(0, 3, "Error: ip address to be used is invalid!");
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
IpGetAdapterDescription(adapterIndex, text, size);
|
||||
snprintf(info, size, "Interface: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
IpGetAdapterAddressAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Ip address: %s", text);
|
||||
strncpy(Local_IP, text,16);
|
||||
sysSetVariableString(sysvar::TCPIP::TcpClientIp, text);
|
||||
|
||||
writelineex(0, 1, info);
|
||||
|
||||
// SysSetVariableString(sysvar::TCPIP::TcpClientIp, text);
|
||||
|
||||
IpGetAdapterMaskAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Subnet mask: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
IpGetAdapterGatewayAsString(adapterIndex, text, size);
|
||||
snprintf(info, size, "Gateway address: %s", text);
|
||||
writelineex(0, 1, info);
|
||||
|
||||
gStatus = gkSTATUS_INITIALISED;
|
||||
}
|
||||
|
||||
on preStart
|
||||
{
|
||||
setStartdelay(1000);
|
||||
}
|
||||
|
||||
on start
|
||||
{
|
||||
SetupIp();
|
||||
ConnectTcp();
|
||||
}
|
||||
|
||||
on key 'd'
|
||||
{
|
||||
SendTcpData();
|
||||
}
|
||||
|
||||
void ConnectTcp()
|
||||
{
|
||||
char buffer[64];
|
||||
dword serverIp;
|
||||
long fehler;
|
||||
|
||||
SysGetVariableString(sysvar::TCPIP::TcpServerIp, buffer, elcount(buffer));
|
||||
// strncpy(buffer, Server_IP,16);
|
||||
|
||||
serverIp = IpGetAddressAsNumber(buffer);
|
||||
|
||||
if (INVALID_IP == serverIp)
|
||||
{
|
||||
writelineex(0, 1, "Error: invalid server Ip address!");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// gTcpPort = @sysvar::TCPIP::TcpClientPort;
|
||||
gTcpPort = 12345;
|
||||
|
||||
gTcpDataSocket = TcpOpen(gIpAddress, gTcpPort);
|
||||
|
||||
if ( INVALID_SOCKET == gTcpDataSocket)
|
||||
{
|
||||
writelineex(0, 1, "Error: could not open Tcp socket!");
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
writelineex(0, 1, "Tcp socket opened.");
|
||||
}
|
||||
|
||||
if (0 == TcpConnect(gTcpDataSocket, serverIp, @sysvar::TCPIP::TcpServerPort))
|
||||
{
|
||||
writelineex(0, 1, "Successfully connected to server %s:%d", buffer, @sysvar::TCPIP::TcpServerPort);
|
||||
|
||||
TcpRecv( gTcpDataSocket);
|
||||
}
|
||||
else
|
||||
{
|
||||
fehler=IpGetLastSocketError(gTcpDataSocket);
|
||||
|
||||
write ("No Port-Connection %d", fehler);
|
||||
}
|
||||
}
|
||||
|
||||
void SendTcpData()
|
||||
{
|
||||
char buffer[512];
|
||||
|
||||
strncpy (buffer, "TestData2", 12);
|
||||
|
||||
// SysGetVariableString(sysvar::TCPIP::TcpClientData, buffer, elcount(buffer));
|
||||
|
||||
if (INVALID_SOCKET == gTcpDataSocket)
|
||||
{
|
||||
writelineex( 0, 2, "Tcp socket is invalid!");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
write ("Sende Länge %d", strlen(buffer));
|
||||
|
||||
if (0 != TcpSend( gTcpDataSocket, buffer, strlen(buffer)))
|
||||
{
|
||||
gIpLastErr = IpGetLastSocketError( gTcpDataSocket);
|
||||
|
||||
if ( WSA_IO_PENDING != gIpLastErr)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "Tcp send error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
writelineex( 0, 1, "Tcp data sent successfully!");
|
||||
}
|
||||
}
|
||||
|
||||
void OnTcpConnect( dword socket, long result)
|
||||
{
|
||||
if ( gTcpDataSocket != socket)
|
||||
{
|
||||
writelineex(0, 2, "OnTcpConnect called for unknown socket 0x%X", socket);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 != result)
|
||||
{
|
||||
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||||
|
||||
writelineex( 0, 2, "OnTcpConnect error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
writelineex(0, 1, "Successfully connected to server via Tcp");
|
||||
|
||||
TcpRecv( socket);
|
||||
}
|
||||
}
|
6006
erster test/socket.cfg
Normal file
6006
erster test/socket.cfg
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue