📄 ethernet_to_serial.c
字号:
/*******************************************************************************
Samples\TcpIp\RabbitWeb\ethernet_to_serial.c
Rabbit Semiconductor, 2004
Uses the RabbitWeb HTTP enhancements to configure a simple
Ethernet-to-serial converter. This sample only supports listening
TCP sockets, meaning that Ethernet-to-serial devices can only be
started by another device initiating the network connection to the
Rabbit.
Each serial port can be associated with a specific TCP port. The Rabbit
will listen on each of these TCP ports for a connection, which will
then be associated with a specific serial port. Data will then be
shuttled between the serial and Ethernet connections.
*******************************************************************************/
/***********************************
* Configuration *
* ------------- *
* All fields in this section must *
* be altered to match your local *
* network settings. *
***********************************/
/*
* NETWORK CONFIGURATION
* Please see the function help (Ctrl-H) on TCPCONFIG for instructions on
* compile-time network configuration.
*/
#define TCPCONFIG 1
/*
* The following array defines which serial ports are used in this sample.
* Please check the documentation for this board to find which serial ports
* are convenient to use. In particular, some Rabbit 3000-based boards make
* ports E and F available rather than B and C. Note, also, that you can have
* any number of serial ports defined below, not just two.
*/
const char ports_config[] = { 'B', 'C' };
#define E2S_BUFFER_SIZE 1024
/*
* Only one server is really needed for the HTTP server as long as
* tcp_reserveport() is called on port 80.
*/
#define HTTP_MAXSERVERS 1
/*
* Define the number of TCP socket buffers to the number of sockets needed for
* the HTTP server (HTTP_MAXSERVERS) + the number of serial ports configured
* (sizeof(ports_config)).
*/
#define MAX_TCP_SOCKET_BUFFERS (HTTP_MAXSERVERS + sizeof(ports_config))
/*
* Define the sizes of the input and output buffers for each of the serial
* ports
*/
#define SERINBUFSIZE 127
#define SEROUTBUFSIZE 127
#define LOCAL_IP "192.168.10.15"
#define LOCAL_NETMASK "255.255.255.0"
#define LOCAL_GATEWAY "192.168.10.1"
#define _PRIMARY_STATIC_IP "192.168.10.15"
/********************************
* End of configuration section *
********************************/
#memmap xmem
/*
* This is needed to be able to use the RabbitWeb HTTP enhancements and the
* ZHTML scripting language.
*/
#define USE_RABBITWEB 1
#use "dcrtcp.lib"
#use "http.lib"
/*
* This page contains the configuration interface for the serial ports.
*/
#ximport "samples/tcpip/rabbitweb/pages/config.zhtml" config_zhtml
/* The default mime type for '/' must be first */
SSPEC_MIMETABLE_START
// This handler enables the ZHTML parser to be used on ZHTML files...
SSPEC_MIME_FUNC(".zhtml", "text/html", zhtml_handler),
SSPEC_MIME(".html", "text/html"),
SSPEC_MIME(".gif", "image/gif")
SSPEC_MIMETABLE_END
/* Associate the #ximported files with the web server */
SSPEC_RESOURCETABLE_START
SSPEC_RESOURCE_XMEMFILE("/", config_zhtml),
SSPEC_RESOURCE_XMEMFILE("/index.zhtml", config_zhtml)
SSPEC_RESOURCETABLE_END
/*
* Function declarations
*/
void restart_socket(int i);
void update_tcp(void);
void restart_serial(int i);
void update_serial(void);
void serial_open(int i);
void e2s_init(void);
void e2s_tick(void);
/*
* This structure contains the configuration information for each serial port /
* TCP port pair.
*/
struct SerialPort {
word tcp_port;
struct {
char port;
long baud;
int databits;
int parity;
int stopbits;
} ser;
};
/*
* This stores the configuration information on the serial ports. The members
* will be registered with the #web statement so that the HTTP enhancements can
* be used.
*/
struct SerialPort serial_ports[sizeof(ports_config)];
/*
* This will function as a copy of the above. It is used to determine which
* port information changed when the update function is called (this will be
* explained later in the program).
*/
struct SerialPort serial_ports_copy[sizeof(ports_config)];
/*
* #web statements
*/
// A #web registration for the TCP port. Note that the only rule in the guard
// is that the new value must be greater than 0.
#web serial_ports[@].tcp_port ($serial_ports[@].tcp_port > 0)
//#web serial_ports[@].ser_port groups=all(ro)
// The character ('B', 'C', etc.) representing the serial port. This is a
// read-only variable.
#web serial_ports[@].ser.port
// The following two #web statements correspond to the baud rate. The guards
// are split into two so that the WEB_ERROR() feature can be used. WEB_ERROR()
// will indicated why the guard statement failed; the string message can later
// be used in the ZHTML scripting.
#web serial_ports[@].ser.baud (($serial_ports[@].ser.baud >= 300)? \
1:WEB_ERROR("too low"))
#web serial_ports[@].ser.baud (($serial_ports[@].ser.baud <= 115200)? \
1:WEB_ERROR("too high"))
// Each of the following are selection variables, since each of the variables
// can only take on a few values.
#web serial_ports[@].ser.databits select("7" = 7, "8" = 8)
#web serial_ports[@].ser.parity select("None" = 0, "Even", "Odd")
#web serial_ports[@].ser.stopbits select("1" = 1, "2" = 2)
// The #web_update feature will initiate a function call when the corresponding
// variables are updated. Note that update_tcp() will be called when the TCP
// port changes, and update_serial() will be called when any of the other
// members are updated.
#web_update serial_ports[@].tcp_port update_tcp
#web_update serial_ports[@].ser.baud,serial_ports[@].ser.databits,\
serial_ports[@].ser.stopbits update_serial
// The following simply sets the buffer sizes for the serial ports based on the
// user configuration above.
#define AINBUFSIZE SERINBUFSIZE
#define AOUTBUFSIZE SEROUTBUFSIZE
#define BINBUFSIZE SERINBUFSIZE
#define BOUTBUFSIZE SEROUTBUFSIZE
#define CINBUFSIZE SERINBUFSIZE
#define COUTBUFSIZE SEROUTBUFSIZE
#define DINBUFSIZE SERINBUFSIZE
#define DOUTBUFSIZE SEROUTBUFSIZE
#define EINBUFSIZE SERINBUFSIZE
#define EOUTBUFSIZE SEROUTBUFSIZE
#define FINBUFSIZE SERINBUFSIZE
#define FOUTBUFSIZE SEROUTBUFSIZE
// The following symbols represent different states in the Ethernet-to-serial
// state machine
enum {
E2S_INIT,
E2S_LISTEN,
E2S_PROCESS
};
// This is the core of the Ethernet-to-serial state machine.
struct {
int state; // Current state of the the state machine
tcp_Socket sock; // Socket associated with this serial port
// The following members are function pointers for accessing this serial
// port
int (*open)();
int (*close)();
int (*read)();
int (*write)();
int (*setdatabits)();
int (*setparity)();
} e2s_state[sizeof(ports_config)];
// A temporary buffer for copying data between the serial ports and TCP ports.
char e2s_buffer[E2S_BUFFER_SIZE];
// Aborts and restarts the given socket (index i).
void restart_socket(int i)
{
printf("Restarting socket %d\n", i);
// Abort the socket
sock_abort(&(e2s_state[i].sock));
// Set up the state machine to reopen the socket
e2s_state[i].state = E2S_INIT;
}
// This function is called when a TCP port is updated via the HTML interface.
// It determines which TCP port(s) changed, and then restarts them with the new
// parameters.
void update_tcp(void)
{
auto int i;
// Check which TCP port(s) changed
for (i = 0; i < sizeof(ports_config); i++) {
if (serial_ports[i].tcp_port != serial_ports_copy[i].tcp_port) {
// This port has changed, restart the socket on the new port
restart_socket(i);
// Save the new port, so we can check which one changed on the next
// update
serial_ports_copy[i].tcp_port = serial_ports[i].tcp_port;
}
}
}
// Closes and reopens the given serial port (index i).
void restart_serial(int i)
{
printf("Restarting serial port %d\n", i);
// Close the serial port
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -