hlp:桂林国际齿轮泵代码
This commit is contained in:
@@ -0,0 +1,397 @@
|
||||
/* common.c - Contains functions needed for a number of barcodes */
|
||||
|
||||
/*
|
||||
libzint - the open source barcode library
|
||||
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
|
||||
int ustrlen(unsigned char data[]) {
|
||||
/* Local replacement for strlen() with unsigned char strings */
|
||||
int i;
|
||||
for (i=0;data[i];i++);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void ustrcpy(unsigned char target[], unsigned char source[]) {
|
||||
/* Local replacement for strcpy() with unsigned char strings */
|
||||
int i, len;
|
||||
|
||||
len = ustrlen(source);
|
||||
for(i = 0; i < len; i++) {
|
||||
target[i] = source[i];
|
||||
}
|
||||
target[i] = '\0';
|
||||
}
|
||||
|
||||
void concat(char dest[], char source[])
|
||||
{ /* Concatinates dest[] with the contents of source[], copying /0 as well */
|
||||
unsigned int i, j, n;
|
||||
|
||||
j = strlen(dest);
|
||||
n = strlen(source);
|
||||
for(i = 0; i <= n; i++) {
|
||||
dest[i + j] = source[i]; }
|
||||
}
|
||||
|
||||
void uconcat(unsigned char dest[], unsigned char source[])
|
||||
{ /* Concatinates dest[] with the contents of source[], copying /0 as well */
|
||||
unsigned int i, j;
|
||||
|
||||
j = ustrlen(dest);
|
||||
for(i = 0; i <= ustrlen(source); i++) {
|
||||
dest[i + j] = source[i]; }
|
||||
}
|
||||
|
||||
|
||||
int ctoi(char source)
|
||||
{ /* Converts a character 0-9 to its equivalent integer value */
|
||||
if((source >= '0') && (source <= '9'))
|
||||
return (source - '0');
|
||||
return(source - 'A' + 10);
|
||||
}
|
||||
|
||||
char itoc(int source)
|
||||
{ /* Converts an integer value to its hexadecimal character */
|
||||
if ((source >= 0) && (source <= 9)) {
|
||||
return ('0' + source); }
|
||||
else {
|
||||
return ('A' + (source - 10)); }
|
||||
}
|
||||
|
||||
void to_upper(unsigned char source[])
|
||||
{ /* Converts lower case characters to upper case in a string source[] */
|
||||
unsigned int i, src_len = ustrlen(source);
|
||||
|
||||
for (i = 0; i < src_len; i++) {
|
||||
if ((source[i] >= 'a') && (source[i] <= 'z')) {
|
||||
source [i] = (source[i] - 'a') + 'A'; }
|
||||
}
|
||||
}
|
||||
|
||||
int is_sane(char test_string[], unsigned char source[], int length)
|
||||
{ /* Verifies that a string only uses valid characters */
|
||||
unsigned int i, j, latch;
|
||||
unsigned int lt = strlen(test_string);
|
||||
|
||||
for(i = 0; i < length; i++) {
|
||||
latch = FALSE;
|
||||
for(j = 0; j < lt; j++) {
|
||||
if (source[i] == test_string[j]) {
|
||||
latch = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!(latch)) {
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int posn(char set_string[], char data)
|
||||
{ /* Returns the position of data in set_string */
|
||||
unsigned int i, n = strlen(set_string);
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
if (data == set_string[i]) { return i; } }
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lookup(char set_string[], char *table[], char data, char dest[])
|
||||
{ /* Replaces huge switch statements for looking up in tables */
|
||||
unsigned int i, n = strlen(set_string);
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
if (data == set_string[i]) { concat(dest, table[i]); } }
|
||||
}
|
||||
|
||||
int module_is_set(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
return (symbol->encoded_data[y_coord][x_coord / 7] & (1 << (x_coord % 7))) ? 1 : 0;
|
||||
#if 0
|
||||
switch(x_sub) {
|
||||
case 0: if((symbol->encoded_data[y_coord][x_char] & 0x01) != 0) { result = 1; } break;
|
||||
case 1: if((symbol->encoded_data[y_coord][x_char] & 0x02) != 0) { result = 1; } break;
|
||||
case 2: if((symbol->encoded_data[y_coord][x_char] & 0x04) != 0) { result = 1; } break;
|
||||
case 3: if((symbol->encoded_data[y_coord][x_char] & 0x08) != 0) { result = 1; } break;
|
||||
case 4: if((symbol->encoded_data[y_coord][x_char] & 0x10) != 0) { result = 1; } break;
|
||||
case 5: if((symbol->encoded_data[y_coord][x_char] & 0x20) != 0) { result = 1; } break;
|
||||
case 6: if((symbol->encoded_data[y_coord][x_char] & 0x40) != 0) { result = 1; } break;
|
||||
}
|
||||
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_module(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
if(module_is_set(symbol, y_coord, x_coord)) { return; }
|
||||
symbol->encoded_data[y_coord][x_coord / 7] += 1 << (x_coord % 7);
|
||||
#if 0
|
||||
int x_char, x_sub;
|
||||
|
||||
|
||||
x_char = x_coord / 7;
|
||||
x_sub = x_coord % 7;
|
||||
|
||||
switch(x_sub) {
|
||||
case 0: symbol->encoded_data[y_coord][x_char] += 0x01; break;
|
||||
case 1: symbol->encoded_data[y_coord][x_char] += 0x02; break;
|
||||
case 2: symbol->encoded_data[y_coord][x_char] += 0x04; break;
|
||||
case 3: symbol->encoded_data[y_coord][x_char] += 0x08; break;
|
||||
case 4: symbol->encoded_data[y_coord][x_char] += 0x10; break;
|
||||
case 5: symbol->encoded_data[y_coord][x_char] += 0x20; break;
|
||||
case 6: symbol->encoded_data[y_coord][x_char] += 0x40; break;
|
||||
} /* The last binary digit is reserved for colour barcodes */
|
||||
#endif
|
||||
}
|
||||
|
||||
void unset_module(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
if(!(module_is_set(symbol, y_coord, x_coord))) { return; }
|
||||
symbol->encoded_data[y_coord][x_coord / 7] -= 1 << (x_coord % 7);
|
||||
#if 0
|
||||
int x_char, x_sub;
|
||||
|
||||
x_char = x_coord / 7;
|
||||
x_sub = x_coord % 7;
|
||||
|
||||
switch(x_sub) {
|
||||
case 0: symbol->encoded_data[y_coord][x_char] -= 0x01; break;
|
||||
case 1: symbol->encoded_data[y_coord][x_char] -= 0x02; break;
|
||||
case 2: symbol->encoded_data[y_coord][x_char] -= 0x04; break;
|
||||
case 3: symbol->encoded_data[y_coord][x_char] -= 0x08; break;
|
||||
case 4: symbol->encoded_data[y_coord][x_char] -= 0x10; break;
|
||||
case 5: symbol->encoded_data[y_coord][x_char] -= 0x20; break;
|
||||
case 6: symbol->encoded_data[y_coord][x_char] -= 0x40; break;
|
||||
} /* The last binary digit is reserved for colour barcodes */
|
||||
#endif
|
||||
}
|
||||
|
||||
void expand(struct zint_symbol *symbol, char data[])
|
||||
{ /* Expands from a width pattern to a bit pattern */
|
||||
|
||||
unsigned int reader, n = strlen(data);
|
||||
int writer, i;
|
||||
char latch;
|
||||
|
||||
writer = 0;
|
||||
latch = '1';
|
||||
|
||||
for(reader = 0; reader < n; reader++) {
|
||||
for(i = 0; i < ctoi(data[reader]); i++) {
|
||||
if(latch == '1') { set_module(symbol, symbol->rows, writer); }
|
||||
writer++;
|
||||
}
|
||||
if(latch == '1') {
|
||||
latch = '0';
|
||||
} else {
|
||||
latch = '1';
|
||||
}
|
||||
}
|
||||
|
||||
if(symbol->symbology != BARCODE_PHARMA) {
|
||||
if(writer > symbol->width) {
|
||||
symbol->width = writer;
|
||||
}
|
||||
} else {
|
||||
/* Pharmacode One ends with a space - adjust for this */
|
||||
if(writer > symbol->width + 2) {
|
||||
symbol->width = writer - 2;
|
||||
}
|
||||
}
|
||||
symbol->rows = symbol->rows + 1;
|
||||
}
|
||||
|
||||
int is_stackable(int symbology) {
|
||||
/* Indicates which symbologies can have row binding */
|
||||
if(symbology < BARCODE_PDF417) { return 1; }
|
||||
if(symbology == BARCODE_CODE128B) { return 1; }
|
||||
if(symbology == BARCODE_ISBNX) { return 1; }
|
||||
if(symbology == BARCODE_EAN14) { return 1; }
|
||||
if(symbology == BARCODE_NVE18) { return 1; }
|
||||
if(symbology == BARCODE_KOREAPOST) { return 1; }
|
||||
if(symbology == BARCODE_PLESSEY) { return 1; }
|
||||
if(symbology == BARCODE_TELEPEN_NUM) { return 1; }
|
||||
if(symbology == BARCODE_ITF14) { return 1; }
|
||||
if(symbology == BARCODE_CODE32) { return 1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_extendable(int symbology) {
|
||||
/* Indicates which symbols can have addon */
|
||||
if(symbology == BARCODE_EANX) { return 1; }
|
||||
if(symbology == BARCODE_UPCA) { return 1; }
|
||||
if(symbology == BARCODE_UPCE) { return 1; }
|
||||
if(symbology == BARCODE_ISBNX) { return 1; }
|
||||
if(symbology == BARCODE_UPCA_CC) { return 1; }
|
||||
if(symbology == BARCODE_UPCE_CC) { return 1; }
|
||||
if(symbology == BARCODE_EANX_CC) { return 1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int roundup(float input)
|
||||
{
|
||||
float remainder;
|
||||
int integer_part;
|
||||
|
||||
integer_part = (int)input;
|
||||
remainder = input - integer_part;
|
||||
|
||||
if(remainder > 0.1) {
|
||||
integer_part++;
|
||||
}
|
||||
|
||||
return integer_part;
|
||||
}
|
||||
|
||||
int istwodigits(unsigned char source[], int position)
|
||||
{
|
||||
if((source[position] >= '0') && (source[position] <= '9')) {
|
||||
if((source[position + 1] >= '0') && (source[position + 1] <= '9')) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float froundup(float input)
|
||||
{
|
||||
float fraction, output = 0.0;
|
||||
|
||||
fraction = input - (int)input;
|
||||
if(fraction > 0.01) { output = (input - fraction) + 1.0; } else { output = input; }
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[], int *length)
|
||||
{
|
||||
int j, i, next;
|
||||
|
||||
/* Convert Unicode to Latin-1 for those symbologies which only support Latin-1 */
|
||||
j = 0;
|
||||
i = 0;
|
||||
do {
|
||||
next = -1;
|
||||
if(source[i] < 128) {
|
||||
preprocessed[j] = source[i];
|
||||
j++;
|
||||
next = i + 1;
|
||||
} else {
|
||||
if(source[i] == 0xC2) {
|
||||
preprocessed[j] = source[i + 1];
|
||||
j++;
|
||||
next = i + 2;
|
||||
}
|
||||
if(source[i] == 0xC3) {
|
||||
preprocessed[j] = source[i + 1] + 64;
|
||||
j++;
|
||||
next = i + 2;
|
||||
}
|
||||
}
|
||||
if(next == -1) {
|
||||
strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
i = next;
|
||||
} while(i < *length);
|
||||
preprocessed[j] = '\0';
|
||||
*length = j;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], int *length)
|
||||
{
|
||||
int bpos, jpos, error_number, done;
|
||||
int next;
|
||||
|
||||
bpos = 0;
|
||||
jpos = 0;
|
||||
error_number = 0;
|
||||
next = 0;
|
||||
|
||||
do {
|
||||
done = 0;
|
||||
|
||||
if(source[bpos] <= 0x7f) {
|
||||
/* 1 byte mode (7-bit ASCII) */
|
||||
vals[jpos] = source[bpos];
|
||||
next = bpos + 1;
|
||||
jpos++;
|
||||
done = 1;
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0x80) && (source[bpos] <= 0xbf)) {
|
||||
strcpy(symbol->errtxt, "Corrupt Unicode data");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xc0) && (source[bpos] <= 0xc1)) {
|
||||
strcpy(symbol->errtxt, "Overlong encoding not supported");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xc2) && (source[bpos] <= 0xdf)) {
|
||||
/* 2 byte mode */
|
||||
vals[jpos] = ((source[bpos] & 0x1f) << 6) + (source[bpos + 1] & 0x3f);
|
||||
next = bpos + 2;
|
||||
jpos++;
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xe0) && (source[bpos] <= 0xef)) {
|
||||
/* 3 byte mode */
|
||||
vals[jpos] = ((source[bpos] & 0x0f) << 12) + ((source[bpos + 1] & 0x3f) << 6) + (source[bpos + 2] & 0x3f);
|
||||
next = bpos + 3;
|
||||
jpos ++;
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if(source[bpos] >= 0xf0) {
|
||||
strcpy(symbol->errtxt, "Unicode sequences of more than 3 bytes not supported");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
bpos = next;
|
||||
|
||||
} while(bpos < *length);
|
||||
*length = jpos;
|
||||
|
||||
return error_number;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,385 @@
|
||||
/*
|
||||
* FreeModbus Libary: Win32 Port
|
||||
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
* File: $Id: porttcp.c,v 1.1 2007/09/12 10:15:56 wolti Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
* Design Notes:
|
||||
*
|
||||
* The xMBPortTCPInit function allocates a socket and binds the socket to
|
||||
* all available interfaces ( bind with INADDR_ANY ). In addition it
|
||||
* creates an array of event objects which is used to check the state of
|
||||
* the clients. On event object is used to handle new connections or
|
||||
* closed ones. The other objects are used on a per client basis for
|
||||
* processing.
|
||||
*/
|
||||
|
||||
/**********************************************************
|
||||
* Linux TCP support.
|
||||
* Based on Walter's project.
|
||||
* Modified by Steven Guo <gotop167@163.com>
|
||||
***********************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "port.h"
|
||||
|
||||
/* ----------------------- Modbus includes ----------------------------------*/
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
|
||||
//#include <QTcpServer>
|
||||
|
||||
/* ----------------------- MBAP Header --------------------------------------*/
|
||||
#define MB_TCP_UID 6
|
||||
#define MB_TCP_LEN 4
|
||||
#define MB_TCP_FUNC 7
|
||||
|
||||
/* ----------------------- Defines -----------------------------------------*/
|
||||
#define MB_TCP_DEFAULT_PORT 5022//5022 /* TCP listening port. */
|
||||
#define MB_TCP_POOL_TIMEOUT 50 /* pool timeout for event waiting. */
|
||||
#define MB_TCP_READ_TIMEOUT 1000 /* Maximum timeout to wait for packets. */
|
||||
#define MB_TCP_READ_CYCLE 100 /* Time between checking for new data. */
|
||||
|
||||
#define MB_TCP_DEBUG 1 /* Set to 1 for additional debug output. */
|
||||
|
||||
#define MB_TCP_BUF_SIZE ( 256 + 7 ) /* Must hold a complete Modbus TCP frame. */
|
||||
|
||||
#define EV_CONNECTION 0
|
||||
#define EV_CLIENT 1
|
||||
#define EV_NEVENTS EV_CLIENT + 1
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
SOCKET xListenSocket;
|
||||
SOCKET xClientSocket = INVALID_SOCKET;
|
||||
static fd_set allset;
|
||||
|
||||
static UCHAR aucTCPBuf[MB_TCP_BUF_SIZE];
|
||||
static USHORT usTCPBufPos;
|
||||
static USHORT usTCPFrameBytesLeft;
|
||||
|
||||
/* ----------------------- External functions -------------------------------*/
|
||||
CHAR *WsaError2String( int dwError );
|
||||
|
||||
/* ----------------------- Static functions ---------------------------------*/
|
||||
BOOL prvMBTCPPortAddressToString( SOCKET xSocket, CHAR * szAddr, USHORT usBufSize );
|
||||
CHAR *prvMBTCPPortFrameToString( UCHAR * pucFrame, USHORT usFrameLen );
|
||||
static BOOL prvbMBPortAcceptClient( void );
|
||||
static void prvvMBPortReleaseClient( void );
|
||||
//static QTcpServer qListenSocket;
|
||||
|
||||
/* ----------------------- Begin implementation -----------------------------*/
|
||||
|
||||
BOOL
|
||||
xMBTCPPortInit( USHORT usTCPPort )
|
||||
{
|
||||
USHORT usPort;
|
||||
struct sockaddr_in serveraddr;
|
||||
int tmp = 1;
|
||||
|
||||
if( usTCPPort == 0 )
|
||||
{
|
||||
usPort = MB_TCP_DEFAULT_PORT;
|
||||
}
|
||||
else
|
||||
{
|
||||
usPort = ( USHORT ) usTCPPort;
|
||||
}
|
||||
memset( &serveraddr, 0, sizeof( serveraddr ) );
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
serveraddr.sin_port = htons( usPort );
|
||||
if( ( xListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
|
||||
{
|
||||
fprintf( stderr, "Create socket failed.\r\n" );
|
||||
return FALSE;
|
||||
}
|
||||
if(setsockopt(xListenSocket, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(int))<0)
|
||||
{
|
||||
fprintf( stderr, "setsockopt socket failed.\r\n" );
|
||||
return FALSE;
|
||||
}
|
||||
if( bind( xListenSocket, ( struct sockaddr * )&serveraddr, sizeof( serveraddr ) ) == -1 )
|
||||
{
|
||||
fprintf( stderr, "Bind socket failed.\r\n" );
|
||||
return FALSE;
|
||||
}
|
||||
else if( listen( xListenSocket, 1 ) == -1 )
|
||||
{
|
||||
fprintf( stderr, "Listen socket failed.\r\n" );
|
||||
return FALSE;
|
||||
}
|
||||
FD_ZERO( &allset );
|
||||
FD_SET( xListenSocket, &allset );
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
vMBTCPPortClose( )
|
||||
{
|
||||
// Close all client sockets.
|
||||
if( xClientSocket != SOCKET_ERROR )
|
||||
{
|
||||
prvvMBPortReleaseClient( );
|
||||
}
|
||||
// Close the listener socket.
|
||||
if( xListenSocket != SOCKET_ERROR )
|
||||
{
|
||||
close( xListenSocket );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
vMBTCPPortDisable( void )
|
||||
{
|
||||
/* Close all client sockets. */
|
||||
if( xClientSocket != SOCKET_ERROR )
|
||||
{
|
||||
prvvMBPortReleaseClient( );
|
||||
}
|
||||
}
|
||||
|
||||
/*! \ingroup port_win32tcp
|
||||
*
|
||||
* \brief Pool the listening socket and currently connected Modbus TCP clients
|
||||
* for new events.
|
||||
* \internal
|
||||
*
|
||||
* This function checks if new clients want to connect or if already connected
|
||||
* clients are sending requests. If a new client is connected and there are
|
||||
* still client slots left (The current implementation supports only one)
|
||||
* then the connection is accepted and an event object for the new client
|
||||
* socket is activated (See prvbMBPortAcceptClient() ).
|
||||
* Events for already existing clients in \c FD_READ and \c FD_CLOSE. In case of
|
||||
* an \c FD_CLOSE the client connection is released (See prvvMBPortReleaseClient() ).
|
||||
* In case of an \c FD_READ command the existing data is read from the client
|
||||
* and if a complete frame has been received the Modbus Stack is notified.
|
||||
*
|
||||
* \return FALSE in case of an internal I/O error. For example if the internal
|
||||
* event objects are in an invalid state. Note that this does not include any
|
||||
* client errors. In all other cases returns TRUE.
|
||||
*/
|
||||
int errorCount = 0;
|
||||
|
||||
BOOL
|
||||
xMBPortTCPPool( void )
|
||||
{
|
||||
int n;
|
||||
fd_set fread;
|
||||
struct timeval tval;
|
||||
|
||||
tval.tv_sec = 1;
|
||||
tval.tv_usec = 60000;
|
||||
int ret;
|
||||
USHORT usLength;
|
||||
|
||||
if( xClientSocket == INVALID_SOCKET )
|
||||
{
|
||||
/* Accept to client */
|
||||
if( ( n = select( xListenSocket + 1, &allset, NULL, NULL, NULL ) ) < 0 )
|
||||
{
|
||||
if( errno == EINTR )
|
||||
{
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
if( FD_ISSET( xListenSocket, &allset ) )
|
||||
{
|
||||
( void )prvbMBPortAcceptClient( );
|
||||
}
|
||||
}
|
||||
|
||||
while( TRUE )
|
||||
{
|
||||
FD_ZERO( &fread );
|
||||
FD_SET( xClientSocket, &fread );
|
||||
if( ( ( ret = select( xClientSocket + 1, &fread, NULL, NULL, &tval ) ) == SOCKET_ERROR )
|
||||
|| !ret )
|
||||
{
|
||||
sleep(1);
|
||||
errorCount++;
|
||||
if(errorCount > 10)
|
||||
{
|
||||
errorCount = 0;
|
||||
close( xClientSocket );
|
||||
xClientSocket = INVALID_SOCKET;
|
||||
return TRUE;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if( ret > 0 )
|
||||
{
|
||||
if( FD_ISSET( xClientSocket, &fread ) )
|
||||
{
|
||||
if( ( ( ret =
|
||||
recv( xClientSocket, &aucTCPBuf[usTCPBufPos], usTCPFrameBytesLeft,
|
||||
0 ) ) == SOCKET_ERROR ) || ( !ret ) )
|
||||
{
|
||||
close( xClientSocket );
|
||||
xClientSocket = INVALID_SOCKET;
|
||||
return TRUE;
|
||||
}
|
||||
usTCPBufPos += ret;
|
||||
usTCPFrameBytesLeft -= ret;
|
||||
if( usTCPBufPos >= MB_TCP_FUNC )
|
||||
{
|
||||
/* Length is a byte count of Modbus PDU (function code + data) and the
|
||||
* unit identifier. */
|
||||
usLength = aucTCPBuf[MB_TCP_LEN] << 8U;
|
||||
usLength |= aucTCPBuf[MB_TCP_LEN + 1];
|
||||
|
||||
/* Is the frame already complete. */
|
||||
if( usTCPBufPos < ( MB_TCP_UID + usLength ) )
|
||||
{
|
||||
usTCPFrameBytesLeft = usLength + MB_TCP_UID - usTCPBufPos;
|
||||
}
|
||||
/* The frame is complete. */
|
||||
else if( usTCPBufPos == ( MB_TCP_UID + usLength ) )
|
||||
{
|
||||
( void )xMBPortEventPost( EV_FRAME_RECEIVED );
|
||||
return TRUE;
|
||||
}
|
||||
/* This can not happend because we always calculate the number of bytes
|
||||
* to receive. */
|
||||
else
|
||||
{
|
||||
assert( usTCPBufPos <= ( MB_TCP_UID + usLength ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup port_win32tcp
|
||||
* \brief Receives parts of a Modbus TCP frame and if complete notifies
|
||||
* the protocol stack.
|
||||
* \internal
|
||||
*
|
||||
* This function reads a complete Modbus TCP frame from the protocol stack.
|
||||
* It starts by reading the header with an initial request size for
|
||||
* usTCPFrameBytesLeft = MB_TCP_FUNC. If the header is complete the
|
||||
* number of bytes left can be calculated from it (See Length in MBAP header).
|
||||
* Further read calls are issued until the frame is complete.
|
||||
*
|
||||
* \return \c TRUE if part of a Modbus TCP frame could be processed. In case
|
||||
* of a communication error the function returns \c FALSE.
|
||||
*/
|
||||
|
||||
BOOL
|
||||
xMBTCPPortGetRequest( UCHAR ** ppucMBTCPFrame, USHORT * usTCPLength )
|
||||
{
|
||||
*ppucMBTCPFrame = &aucTCPBuf[0];
|
||||
*usTCPLength = usTCPBufPos;
|
||||
|
||||
/* Reset the buffer. */
|
||||
usTCPBufPos = 0;
|
||||
usTCPFrameBytesLeft = MB_TCP_FUNC;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBTCPPortSendResponse( const UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
{
|
||||
BOOL bFrameSent = FALSE;
|
||||
BOOL bAbort = FALSE;
|
||||
int res;
|
||||
int iBytesSent = 0;
|
||||
int iTimeOut = MB_TCP_READ_TIMEOUT;
|
||||
|
||||
do
|
||||
{
|
||||
res = send( xClientSocket, &pucMBTCPFrame[iBytesSent], usTCPLength - iBytesSent, 0 );
|
||||
switch ( res )
|
||||
{
|
||||
case -1:
|
||||
if( iTimeOut > 0 )
|
||||
{
|
||||
iTimeOut -= MB_TCP_READ_CYCLE;
|
||||
usleep( MB_TCP_READ_CYCLE );
|
||||
}
|
||||
else
|
||||
{
|
||||
bAbort = TRUE;
|
||||
}
|
||||
break;
|
||||
case 0:
|
||||
prvvMBPortReleaseClient( );
|
||||
bAbort = TRUE;
|
||||
break;
|
||||
default:
|
||||
iBytesSent += res;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while( ( iBytesSent != usTCPLength ) && !bAbort );
|
||||
|
||||
bFrameSent = iBytesSent == usTCPLength ? TRUE : FALSE;
|
||||
|
||||
return bFrameSent;
|
||||
}
|
||||
|
||||
void
|
||||
prvvMBPortReleaseClient( )
|
||||
{
|
||||
( void )recv( xClientSocket, &aucTCPBuf[0], MB_TCP_BUF_SIZE, 0 );
|
||||
|
||||
( void )close( xClientSocket );
|
||||
xClientSocket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
BOOL
|
||||
prvbMBPortAcceptClient( )
|
||||
{
|
||||
SOCKET xNewSocket;
|
||||
BOOL bOkay;
|
||||
|
||||
/* Check if we can handle a new connection. */
|
||||
|
||||
if( xClientSocket != INVALID_SOCKET )
|
||||
{
|
||||
fprintf( stderr, "can't accept new client. all connections in use.\n" );
|
||||
bOkay = FALSE;
|
||||
}
|
||||
else if( ( xNewSocket = accept( xListenSocket, NULL, NULL ) ) == INVALID_SOCKET )
|
||||
{
|
||||
bOkay = FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
xClientSocket = xNewSocket;
|
||||
usTCPBufPos = 0;
|
||||
usTCPFrameBytesLeft = MB_TCP_FUNC;
|
||||
bOkay = TRUE;
|
||||
}
|
||||
return bOkay;
|
||||
}
|
||||
Reference in New Issue
Block a user