Multi-function serial device server (2)

zhaozj2021-02-17  61

This article describes a multi-function serial device server based on Java. This server can implement the following functions: (1) Accept the signal sent from the specified serial port and forward it to another serial port, broadcast to the local area network, or transfer to other devices within the local area network through the TCP / IP link, thereby implementing the local area network inside Information sharing; (2) Control information sent to the serial port device via TCP / IP link or serial port accepts control information to the serial device.

The first part of this article briefly introduces the serial port equipment server, and the system design of this multi-function serial device server is described in detail. This is the second part of this article, introduces the programming and functional test of this multi-function serial device server.

programming

(1) The function of the main control module master module is to initialize the running parameters while starting the remote control module. In this simple serial port device server, no running parameters are defined, so the unique function of the master module is to start the remote control module. This module accepts a parameter from the command line, which specifies the port number used by the remote control module, and the remote customer can control and configure the system via this port. This function module includes the following steps:

Check if the user provides a parameter from the command line. If so, continue to run the next step; if no, output an error message and exit the system. Check if the command line parameters provided by the user can be explained as a port number (an integer). If so, continue to run the next step; if no, output an error message and exit the system. Check if the port number provided by the user is system port (ie, less than 1024) or an invalid port (ie, greater than 65535). If no, continue to run the next step; if yes, output an error message and exit the system. Declare a remote control module and start the module.

The master module is saved as DeviceServer.java.

(2) Remote control module Remote control module accepts user instructions through the TCP / IP link and implements the following functions according to the instruction: changing the serial port parameters, communicating with the specified serial port device, starts or terminates the specified data exchange module. According to system requirements, this module includes the following methods: system configuration method:

Specifies the serial port of the serial port device; specifies the parameters used to communicate with the serial port device, including the baud rate, data bit, stop bit, and parity check. Specifies the serial port where other devices is located; specify the parameters used to communicate with other device, including baud rate, data bits, stop bit, and parity check. Specify the data broadcast port, that is, the client used to listen to real-time data. Specify the data service port, that is, the client used to request real-time data. Specify the data exchange module. For simplicity, this system can only be able to boot a data exchange module to provide data exchange services.

System control method:

Interpret the control command, this method explains the control and configuration instruction sent by the client, and then calls the corresponding system control or configuration method. Start connectivity to the serial device. Cut off the connection with the serial device. The port used or the connection to the client must be turned off before cutting off the connection with the serial port device. Start the data exchange module. Terminate the data exchange module. The port used or the connection to the client is cut off before terminating the data exchange module. Exit system. All ports must be turned off before exiting the system, terminate the data exchange module. Since this functional module needs to be implemented, it is not designed in the following sample programs in order to save space. In the comment line of the program, some descriptions are added to where they may result in an exception.

The remote control module is saved as RemoteControl.java.

(3) The function of the data broadcast module data broadcast module is to accept the output signal of the serial port, and then forward it to the local area network through broadcast. All devices in the LAN can obtain the data provided by the serial port device by listening to the broadcast port, thereby making multiple devices simultaneously sharing a special device. This function module first built a broadcast server and then constantly listens the output stream of the serial port device. When there is data in the output stream, the broadcast server removes it and broadcasts it to the LAN. The data broadcast module is saved as Broadcast.java.

(4) The function of the TCP / IP Data Service Module TCP / IP Data Service Module is to accept the output signal of the serial port, and then forward it to the client via the TCP / IP connection. This module can also accept the remote control and configuration function of the serial port device through the control signal transmitted by the client through the TCP / IP connection and forward it to the serial port device. In this module includes three threads: server thread accepts client connection requests, data service threads monitor the output stream of serial port devices and forward data to clients, control signal service thread monitoring client's output stream and forwards signals to Serial port equipment. For the sake of simplicity, this sample program can only accept a client's connection request, but the user can easily rewrite its ability to serve multiple clients.

The TCP / IP Data Service module is saved as servetcp.java.

(5) The function of the serial data service module serial port data service module is to accept the output signal of the serial port device, and then transfers the data to other devices connected by serial ports via the serial port. Similar to the TCP / IP data service module, this module can also accept the remote control and configuration function of the serial port device through the control signal transmitted by the serial port and forward it to the serial device. In general, two devices communicate via serial ports require that the two devices use the same communication parameters such as baud rate, data bits, stop bit, and parity check. But for many legacy equipment, they often use fixed communication parameters to work, resulting in problems that the data cannot be shared due to the different communication parameters. In this example program, different devices can use different communication parameters to solve data exchange issues between devices with different fixed communication parameters.

In this functional module, two threads include two threads: The data service thread monitors the output stream of the serial port device and forwards the data to the client device, and the control signal service thread monitors the output stream of the client device and forwards the signal to the serial device. For the sake of simplicity, this sample program can only provide data services for a client device at a time, but the user can easily rewrite its ability to provide services to multiple client devices.

The serial data service module is saved as ServeCom.java.

The test of this multi-function serial device server should be performed in the LAN. Preferred test programs should have the following hardware conditions:

Computer A - Run this multi-function serial port device server. This machine must be equipped with a Java running environment and communication extension library Javax.comm from Sun Company, which can download from Java software home www.javasoft.com. Before the test is performed, the file must be installed according to the instructions provided by the package, otherwise the system will not be able to find the native serial port when testing.

Computer B - Run Super Terminal Simulates a serial port device, and is connected to the serial COM1 of Computer A via the RS-232 cable.

Computer C - Run Super Terminal Simulates Other Serial Port Devices, and connects to the serial COM2 of Computer A via the RS-232 cable.

Computer D - Connect to computer A through a local area network, other network devices that request data to multi-function serial device servers in the simulated LAN.

Run the multifunction serial device server on computer A and then enter the remote control status via Telnet. The user controls and configures the server by means of a command line. The control and configuration instructions provided by this server are as follows:

Set Device Port Set the serial port where the serial port device is located. This command accepts an integer parameter that specifies the serial port where the serial device is located. Set Device Param Sets the communication parameters used by the serial port device. This command accepts a parameter string, such as 38400, 8, 1, 0 represents the baud rate 38400, data bit 8, stop bit, no parity test. Set Peer Port Set the serial port where other devices is located. This command accepts an integer parameter that specifies the serial port where the serial device is located. Set Peer Param Sets the communication parameters used by other devices. This command accepts a parameter string, such as 38400, 8, 1, 0 represents the baud rate 38400, data bit 8, stop bit, no parity test. SET BROADCAST Port Sets the data broadcast port. This command accepts an integer parameter that specifies the client to listen to the port of data broadcast. Set Service Port Sets the data service port. This command accepts an integer parameter that specifies the port used to provide data services. Set Service Type Specifies the data service type. This command accepts an integer parameter, 1 represents the data broadcast service, 2 indicates the TCP / IP data service, 3 represents the serial port data service. Other values ​​are invalid parameters. Connect to Device Turn on the serial device. This command does not accept any parameters. Disconnect from Device cuts off the serial device. This command does not accept any parameters. START Service launches data service. This command does not accept any parameters. STOP Service terminates the data service. This command does not accept any parameters. Shut Down terminates a multi-function serial device server. This command does not accept any parameters. Quit exits the remote control system. This command does not accept any parameters. Since the remote control server converts the command line to uppercase before processing the command, the user can use uppercase or lowercase input control and configuration instructions. (1) Data Broadcasting Function Test Computer A Starts Multi-Function Serial Equipment Server, computer B starts the super terminal to simulate serial port equipment. You must specify a port number (such as 8888) to provide remote control service when starting a multi-function serial port device server. With Telnet to enter remote control programs (such as Telnet localhost 8888), you can control and configure multi-function serial port device servers by controlling instructions.

It should be noted that because the Telnet program comes with Windows does not return to the input data, the user can not see the command you entered. It is recommended that users control and configure from a system remote TELNET similar to UNIX to the serial port device server. If the IP of the computer A is 90.0.0.1, the remote control service of the multi-function serial port device server runs in port 8888, then the user can connect to the remote control server via Telnet 90.0.0.1 8888.

After the user is connected to the remote control server, run the following command:

Set Device Port 1

Set Device Param 115200, 8, 1,0

Connect to Device

Set Service Type 1

SET BROADCAST Port 8080

Start Service

The above command is set to the serial port device to connect in COM1, and the communication parameters are 115200, 8, 1, 0. The hyper terminal running on the computer B should use the same communication parameters. Successful and serial port equipment is established to finish the 8080 for the data broadcast port, then specify the system to provide data broadcast service, and finally start the data broadcast service. At this time, other computers in the same LAN (such as computer d) should be able to listen to the data sent by the serial port device (computer B) at the 8080 port.

This article provides an example program that can be used to listen to data broadcast to the specified port. The program accepts a parameter from the command line, this parameter specifies the port of the listener. This program is saved as bclient.java.

Run the above programs on the machine D and specify 8080 to the listening port. In the super terminal window of the machine B, the output data of the serial port device is invited, which is transmitted to the local area network by the multi-function serial port device server, so these data should be simultaneously printed on the screen of the machine D. If there are other computers in the local area network, there are other computers to run or specify 8080 for the listening port, then these computers can also receive the output data of the serial device simultaneously. (2) TCP / IP Data Service Function Test In remote control terminal input, the following control command test TCP / IP data service function:

STOP Service

Set Service Type 2

Set Service Port 8023

Start Service

The above control command specifies the multi-function serial port device server to start TCP / IP data services in port 8023. The data output from the serial port device can be received from the machine D to this port (such as Telnet 90.0.0.1 8023). The data entered on the machine D is also transmitted to the serial device in real time, so the data entered from the machine D can be seen in the Hyper Terminal window of the machine B. Therefore, the equipment in the local area network can not only use the serial port device to make remote data acquisition and processing, but also remote control and configuration of the serial device.

(3) Serial port data service function Test In remote control terminal input, the following control command test serial data service function:

STOP Service

Set Peer Port 2

Set Peer Param 38400, 8, 1,0

Set Service Type 3

Start Service

The above control command specifies that other serial port devices are connected to COM2, and the communication parameters used are 38400, 8, 1, 0. The last two control commands specify the system running serial port data services.

Computer C runs a super terminal program with a communication parameter of 38400, 8, 1, 0. At this time, the serial port device (computer B) can receive the data transmitted by other devices (computer c), and other devices (computer c) can also receive the data sent by the serial device (computer B). It is noted that the communication parameters used by the two serial port devices (Computers B and C) are different, but they can perform data exchange through this multi-function serial device server.

(4) Other functional tests In the remote control terminal input, the following control instructions: Disconnect from Device Control commands are cut off with serial port devices. If a remote data service (such as a data broadcast service, TCP / IP data service, serial data service) is being provided, this service will be turned off.

QUIT The above control command terminates the session of the client and the remote control terminal, all configurations of the multi-function serial port device server remains the state before terminating the session, and the remote data service that is being provided continues to be provided.

SHUT DOWN Control Instructions Force to stop the execution of multi-function serial port device servers. The remote data service currently provided will be closed, and the connection to the serial port will be cut off.

(5) The authors of the test results have a comprehensive test of the function of the system, and the actual test environment is as follows:

Computer A - Windows 2000 English Edition Java SDK 1.3 Java Comm API

Computer B - Windows 98 English version Super Terminal

Computer C - Windows 98 English Edition Super Terminal

Computer D - Windows 98 English Edition Java SDK 1.3

Computer E - Red Hat 6.1

All machines are connected through a 100M high-speed EHTERNET. Computer B is connected to the COM1 of Computer A via an RS-232 cable, and computer C is connected to the COM2 of computer A via an RS-232 cable. In actual testing, remote control and configuration remotely connected to computer A via computer e. Test results show that the system enables all functions such as remote control and configuration and remote data services. Conclusion This article is designed and implemented a Java-based multi-function serial device server. This multi-function serial device server can implement the following features:

Remote control and configuration. Provide remote data services for serial port equipment. The data broadcast service enables multiple devices within the LAN to simultaneously utilize data provided by a serial port device; the TCP / IP data service enables the device within the local area to obtain the data provided by the serial port device through the TCP / IP connection, and can remotely The serial port device is controlled and configured; the serial port data service enables two serial ports that use different communication parameters to be able to perform data exchange. This system not only realizes the sharing of serial device information in the LAN and has greatly improved the speed and efficiency of data exchange. This system is tested on the Windows platform. Due to the collapse of the Java language, this system only needs to be smaller, it can be ported to other platforms. This system has a wide range of application prospects in industries involving data acquisition and processing.

转载请注明原文地址:https://www.9cbs.com/read-29075.html

New Post(0)