⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ethernet_to_serial.c

📁 Dynamic C 程式源碼 用來控制RCM5700 rs232 連接到網路設定
💻 C
📖 第 1 页 / 共 2 页
字号:
	e2s_state[i].close();
   // Open the serial port
   serial_open(i);
}

// This function is called when a serial port is updated via the HTML interface.
// It determines which serial port(s) changed, and then restarts them with the
// new parameters.
void update_serial(void)
{
	auto int i;

	// Check which serial port(s) changed
	for (i = 0; i < sizeof(ports_config); i++) {
		if (memcmp(&(serial_ports[i].ser), &(serial_ports_copy[i].ser),
		    sizeof(serial_ports[i].ser))) {
			// This serial port has changed, so re-open the serial port with the
			// new parameters
			restart_serial(i);
			// Save the new parameters, so we can check which one changed on the
			// next update
			memcpy(&(serial_ports_copy[i].ser), &(serial_ports[i].ser),
			       sizeof(serial_ports[i].ser));
		}
	}
}

// This function does all of the work necessary to open a serial port, including
// setting the number of data bits, stop bits, and parity.
void serial_open(int i)
{
	// Open the serial port
	e2s_state[i].open(serial_ports[i].ser.baud);

	// Set the data bits
	if (serial_ports[i].ser.databits == 7) {
		e2s_state[i].setdatabits(PARAM_7BIT);
	}
	else {
		e2s_state[i].setdatabits(PARAM_8BIT);
	}

	// Set the stop bits
	if (serial_ports[i].ser.stopbits == 1) {
   	if (serial_ports[i].ser.parity == 0) {
      	// No parity
			e2s_state[i].setparity(PARAM_NOPARITY);
      }
      else if (serial_ports[i].ser.parity == 1) {
      	// Even parity
      	e2s_state[i].setparity(PARAM_EPARITY);
      }
      else {
      	// Odd parity (== 2)
      	e2s_state[i].setparity(PARAM_OPARITY);
      }
	}
	else {
   	// 2 stop bits
		e2s_state[i].setparity(PARAM_2STOP);
	}
}

// Initialize the Ethernet-to-serial state machine.
void e2s_init(void)
{
	auto int i;

	for (i = 0; i < sizeof(ports_config); i++) {
	   // Initialize the state
	   e2s_state[i].state = E2S_INIT;

	   // Initialize the serial function pointers
	   switch (ports_config[i]) {
	   case 'A':
	   	e2s_state[i].open = serAopen;
	   	e2s_state[i].close = serAclose;
	   	e2s_state[i].read = serAread;
	   	e2s_state[i].write = serAwrite;
	   	e2s_state[i].setdatabits = serAdatabits;
	   	e2s_state[i].setparity = serAparity;
	   	break;
	   case 'B':
	   	e2s_state[i].open = serBopen;
	   	e2s_state[i].close = serBclose;
	   	e2s_state[i].read = serBread;
	   	e2s_state[i].write = serBwrite;
	   	e2s_state[i].setdatabits = serBdatabits;
	   	e2s_state[i].setparity = serBparity;
	   	break;
	   case 'C':
	   	e2s_state[i].open = serCopen;
	   	e2s_state[i].close = serCclose;
	   	e2s_state[i].read = serCread;
	   	e2s_state[i].write = serCwrite;
	   	e2s_state[i].setdatabits = serCdatabits;
	   	e2s_state[i].setparity = serCparity;
	   	break;
	   case 'D':
	   	e2s_state[i].open = serDopen;
	   	e2s_state[i].close = serDclose;
	   	e2s_state[i].read = serDread;
	   	e2s_state[i].write = serDwrite;
	   	e2s_state[i].setdatabits = serDdatabits;
	   	e2s_state[i].setparity = serDparity;
	   	break;
#if (CPU_ID_MASK(_CPU_ID_)) >= R3000
	   case 'E':
	   	e2s_state[i].open = serEopen;
	   	e2s_state[i].close = serEclose;
	   	e2s_state[i].read = serEread;
	   	e2s_state[i].write = serEwrite;
	   	e2s_state[i].setdatabits = serEdatabits;
	   	e2s_state[i].setparity = serEparity;
	   	break;
	   case 'F':
	   	e2s_state[i].open = serFopen;
	   	e2s_state[i].close = serFclose;
	   	e2s_state[i].read = serFread;
	   	e2s_state[i].write = serFwrite;
	   	e2s_state[i].setdatabits = serFdatabits;
	   	e2s_state[i].setparity = serFparity;
	   	break;
#endif
	   default:
	   	// Error--not a valid serial port
	   	exit(-1);
	   }

	   // Open each serial port
	   serial_open(i);
	}
}

// Drive the Ethernet-to-serial state machine.  This largely concerns itself
// with handling each of the TCP sockets (the different states correspond to
// the state of the TCP socket).  In particular, in the E2S_PROCESS state, it
// does the copying of data from TCP socket to serial port and vice versa.
void e2s_tick(void)
{
	auto int i;
   auto int len;
   auto tcp_Socket *sock;

	for (i = 0; i < sizeof(ports_config); i++) {
   	sock = &(e2s_state[i].sock);
		switch (e2s_state[i].state) {
		case E2S_INIT:
			tcp_listen(sock, serial_ports[i].tcp_port, 0, 0, NULL, 0);
			e2s_state[i].state = E2S_LISTEN;
			break;
		case E2S_LISTEN:
      	if (!sock_waiting(sock)) {
         	// The socket is no longer waiting
            if (sock_established(sock)) {
            	// The socket is established
            	e2s_state[i].state = E2S_PROCESS;
            }
            else if (!sock_alive(sock)) {
            	// The socket was established but then aborted by the peer
               e2s_state[i].state = E2S_INIT;
            }
            else {
            	// The socket was opened, but is now closing.  Just go to the
               // PROCESS state to read off any data.
               e2s_state[i].state = E2S_PROCESS;
            }
         }
			break;
		case E2S_PROCESS:
      	// Check if the socket is dead
         if (!sock_alive(sock)) {
         	e2s_state[i].state = E2S_INIT;
         }
			// Read from TCP socket and write to serial port
			len = sock_fastread(sock, e2s_buffer, E2S_BUFFER_SIZE);
         if (len < 0) {
         	// Error
            sock_abort(sock);
            e2s_state[i].state = E2S_INIT;
         }
         if (len > 0) {
         	// Write the read data to the serial port--Note that for simplicity,
            // this code will drop bytes if more data has been read from the TCP
            // socket than can be written to the serial port.
            e2s_state[i].write(e2s_buffer, len);
         }
         else {
         	// No data read--do nothing
         }
         // Read from the serial port and write to the TCP socket
         len = e2s_state[i].read(e2s_buffer, E2S_BUFFER_SIZE, (unsigned long)0);
			if (len > 0) {
         	// Write the read data to the TCP port--Note that for simplicity,
            // this code will drop bytes if more data has been read from the
            // serial port than can be written to the TCP socket.
            len = sock_fastwrite(sock, e2s_buffer, len);
            if (len < 0) {
            	// Error
               sock_abort(sock);
               e2s_state[i].state = E2S_INIT;
            }
         }
			break;
		}
	}
}

void main(void)
{
	auto int i;

	// Initialize the serial_ports data structure
	for (i = 0; i < sizeof(ports_config); i++) {
		serial_ports[i].tcp_port = 1234 + i;
		serial_ports[i].ser.port = ports_config[i];
		serial_ports[i].ser.baud = 9600;
		serial_ports[i].ser.databits = 8;
      serial_ports[i].ser.parity = 0;
		serial_ports[i].ser.stopbits = 1;
	}
	// Make a copy of the configuration options to be compared against when
	// the update functions are called
	memcpy(serial_ports_copy, serial_ports, sizeof(serial_ports));

	// Initialize the TCP/IP stack, HTTP server, and Ethernet-to-serial state
	// machine.
	// Start network and wait for interface to come up (or error exit).
	sock_init_or_exit(1);
   http_init();
   e2s_init();

	// This is a performance improvement for the HTTP server (port 80),
	// especially when few HTTP server instances are used.
   tcp_reserveport(80);

   while (1) {
		// Drive the HTTP server
      http_handler();
      // Drive the Ethernet-to-serial state machine
      e2s_tick();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -