connected

This commit is contained in:
Forsworns 2022-02-15 11:28:30 +08:00
parent 4f59e4ea24
commit f0b2886a03
9 changed files with 41 additions and 94 deletions

View File

@ -1,10 +1,11 @@
#include "snap7.h"
#include <transform.h>
#include "lwip/sys.h"
static S7Object Client;
static unsigned char Buffer[65536]; // 64 K buffer
static char Address[20]="192.168.250.252"; // PLC IP Address 192.168.250.8
static char* Address="192.168.250.252"; // plc 250.8, pc 250.252
static int Rack = 0, Slot = 1; // Default Rack and Slot
static int Area = S7AreaDB, DBNumber = 13, Start = 0, Amount = 4, WordLen = S7WLByte; // Default Rack and Slot
static int bRead = 1; // By default read
@ -18,9 +19,9 @@ static int CliConnect()
if (Check(Client, res, "UNIT Connection"))
{
Cli_GetPduLength(Client, Requested, Negotiated);
KPrintf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot);
KPrintf(" PDU Requested : %d bytes\n", Requested);
KPrintf(" PDU Negotiated : %d bytes\n", Negotiated);
printf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot);
printf(" PDU Requested : %d bytes\n", Requested);
printf(" PDU Negotiated : %d bytes\n", Negotiated);
};
return !res;
}
@ -56,11 +57,19 @@ void PerformTasks()
}
}
void s7_demo()
void s7_demo(int argc, char *argv[])
{
if(argc >= 2)
{
Address = argv[1];
}
printf(" Connected to : %s", Address);
// Client Creation
Client = Cli_Create();
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
// Connection
if (CliConnect())
{

View File

@ -43,28 +43,6 @@ void libdone(void)
// in future expansions here can be inserted some destruction code
}
#ifdef OS_WINDOWS
BOOL APIENTRY DllMain (HINSTANCE hInst,
DWORD reason,
LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
libinit();
break;
case DLL_PROCESS_DETACH:
libdone();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return libresult;
}
#endif
//***************************************************************************
// CLIENT
//***************************************************************************

View File

@ -57,21 +57,13 @@ longword TSnapBase::SwapDWord(longword Value)
//---------------------------------------------------------------------------
void Msg_CloseSocket(socket_t FSocket)
{
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket);
#endif
}
//---------------------------------------------------------------------------
longword Msg_GetSockAddr(socket_t FSocket)
{
sockaddr_in RemoteSin;
#ifdef OS_WINDOWS
int namelen = sizeof(RemoteSin);
#else
uint32_t namelen = sizeof(RemoteSin);
#endif
namelen=sizeof(sockaddr_in);
if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
return RemoteSin.sin_addr.s_addr;
@ -128,33 +120,21 @@ void TMsgSocket::GetSin(sockaddr_in sin, char *Address, u_short &Port)
//---------------------------------------------------------------------------
void TMsgSocket::GetLocal()
{
#ifdef OS_WINDOWS
int namelen = sizeof(LocalSin);
#else
uint32_t namelen = sizeof(LocalSin);
#endif
uint32_t namelen = sizeof(LocalSin);
if (getsockname(FSocket, (struct sockaddr*)&LocalSin, &namelen)==0)
GetSin(LocalSin, LocalAddress, LocalPort);
}
//---------------------------------------------------------------------------
void TMsgSocket::GetRemote()
{
#ifdef OS_WINDOWS
int namelen = sizeof(RemoteSin);
#else
uint32_t namelen = sizeof(RemoteSin);
#endif
uint32_t namelen = sizeof(RemoteSin);
if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
GetSin(RemoteSin, RemoteAddress, RemotePort);
}
//---------------------------------------------------------------------------
int TMsgSocket::GetLastSocketError()
{
#ifdef OS_WINDOWS
return WSAGetLastError();
#else
return errno;
#endif
}
//---------------------------------------------------------------------------
void TMsgSocket::Purge()
@ -178,8 +158,9 @@ void TMsgSocket::CreateSocket()
DestroySocket();
LastTcpError=0;
FSocket =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (FSocket!=INVALID_SOCKET)
SetSocketOptions();
if (FSocket!=INVALID_SOCKET){
SetSocketOptions();
}
else
LastTcpError =GetLastSocketError();
}
@ -209,11 +190,7 @@ void TMsgSocket::DestroySocket()
{
if (shutdown(FSocket, SD_SEND)==0)
Purge();
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket);
#endif
FSocket=INVALID_SOCKET;
}
LastTcpError=0;
@ -223,13 +200,8 @@ int TMsgSocket::WaitingData()
{
int result = 0;
u_long x = 0;
#ifdef OS_WINDOWS
if (ioctlsocket(FSocket, FIONREAD, &x) == 0)
result = x;
#else
if (ioctl(FSocket, FIONREAD, &x) == 0)
result = x;
#endif
if (result>MaxPacketSize)
result = MaxPacketSize;
return result;
@ -270,9 +242,9 @@ void TMsgSocket::SetSocketOptions()
int KeepAlive = 1;
LastTcpError=0;
SockCheck(setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY,(char*)&NoDelay, sizeof(NoDelay)));
if (LastTcpError==0)
if (LastTcpError==0){
SockCheck(setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&KeepAlive, sizeof(KeepAlive)));
}
}
//---------------------------------------------------------------------------
int TMsgSocket::SockCheck(int SockResult)
@ -321,7 +293,6 @@ bool TMsgSocket::CanRead(int Timeout)
FD_ZERO(&FDset);
FD_SET(FSocket, &FDset);
x = select(FSocket + 1, &FDset, NULL, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio
if (x==(int)SOCKET_ERROR)
{
@ -348,10 +319,13 @@ int TMsgSocket::SckConnect()
CreateSocket();
if (LastTcpError == 0) {
flags = fcntl(FSocket, F_GETFL, 0);
if (flags >= 0) {
if (fcntl(FSocket, F_SETFL, flags | O_NONBLOCK) != -1) {
if (flags >= 0) {
// lwip defines O_NONBLOCK as 1
#define LWIP_O_NONBLOCK 1
int setfl_res = fcntl(FSocket, F_SETFL, flags | LWIP_O_NONBLOCK);
if (setfl_res != -1) {
n = connect(FSocket, (struct sockaddr*)&RemoteSin, sizeof(RemoteSin));
if (n < 0) {
if (n < 0) {
if (errno != EINPROGRESS) {
LastTcpError = GetLastSocketError();
}
@ -429,11 +403,7 @@ void TMsgSocket::ForceClose()
if(FSocket != INVALID_SOCKET)
{
try {
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket);
#endif
} catch (...) {
}
FSocket=INVALID_SOCKET;
@ -617,12 +587,8 @@ TRawSocketPinger::~TRawSocketPinger()
{
if (FSocket!=INVALID_SOCKET)
{
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket);
#endif
FSocket=INVALID_SOCKET;
close(FSocket);
FSocket=INVALID_SOCKET;
};
}
//---------------------------------------------------------------------------
@ -759,11 +725,7 @@ bool RawSocketsCheck()
Result=RawSocket != INVALID_SOCKET;
if (Result)
#ifdef OS_WINDOWS
closesocket(RawSocket);
#else
close(RawSocket);
#endif
return Result;
}

View File

@ -29,14 +29,7 @@
#include "snap_platform.h"
#include "snap_sysutils.h"
#include "errno.h"
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Non blocking connection to avoid root priviledges under UNIX
// i.e. raw socket pinger is not more used.
// Thanks to Rolf Stalder that made it ;)
//----------------------------------------------------------------------------
#include <fcntl.h>
//----------------------------------------------------------------------------
typedef int socket_t;
@ -183,9 +176,6 @@ class TPinger
private:
PRawSocketPinger RawPinger;
bool RawAvail;
#ifdef OS_WINDOWS
bool WinPing(longword ip_addr, int Timeout);
#endif
bool RawPing(longword ip_addr, int Timeout);
public:
TPinger();

View File

@ -30,6 +30,7 @@
#include <lwip/tcp.h>
#include <lwip/errno.h>
#include <lwip/sockets.h>
#include <lwip/sockets.h> // fcntl related status definition is different from linux
#include <lwip/inet.h>
#include <unistd.h>

View File

@ -95,6 +95,12 @@ int PrivTaskDelay(int32_t ms)
return usleep(ms);
}
uint32_t PrivGetTickTime(){
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
/*********************fs**************************/
/************************Driver Posix Transform***********************/

View File

@ -190,6 +190,7 @@ int PrivTaskStartup(pthread_t *thread);
int PrivTaskDelete(pthread_t thread, int sig);
void PrivTaskQuit(void *value_ptr);
int PrivTaskDelay(int32_t ms);
uint32_t PrivGetTickTime();
/*********************driver*************************/

View File

@ -7,8 +7,8 @@ MAKEFLAGS += --no-print-directory
support :=kd233 stm32f407-st-discovery maix-go stm32f407zgt6 aiit-riscv64-board aiit-arm32-board hifive1-rev-B hifive1-emulator k210-emulator cortex-m3-emulator cortex-m4-emulator ok1052-c gapuino stm32f103-nano gd32vf103_rvstar cortex-m0-emulator
SRC_DIR:=
export BOARD ?=kd233
# kd233
export BOARD ?=ok1052-c
ifeq ($(filter $(BOARD),$(support)),)
$(warning "You should choose board like this:make BOARD=kd233")

View File

@ -2009,7 +2009,7 @@
* LWIP_SO_RCVBUF==1: Enable SO_RCVBUF processing.
*/
#if !defined LWIP_SO_RCVBUF || defined __DOXYGEN__
#define LWIP_SO_RCVBUF 0
#define LWIP_SO_RCVBUF 1
#endif
/**
@ -3292,7 +3292,7 @@
* SOCKETS_DEBUG: Enable debugging in sockets.c.
*/
#if !defined SOCKETS_DEBUG || defined __DOXYGEN__
#define SOCKETS_DEBUG LWIP_DBG_OFF
#define SOCKETS_DEBUG LWIP_DBG_ON
#endif
/**