


Summary:The automatic batching system is organically combined with the PC to form an RS-232 bus-type batching, and uses a visual graphical interface to realize real-time monitoring of the entire batching process.
A multi-machine communication bus based on RS-232 is proposed for the multi-machine real-time communication of the automatic dosage system. The bus is designed with a hardware logic circuit, through which each terminal in the system inquires about the status of the bus and locks the bus when it is occupied to avoid the data conflict problem encountered in RS-232 serial communication, and a reasonable data bus allocation method is proposed on the basis of this, which not only ensures the reasonable allocation of the bus, but also meets the real-time communication tasks of different priority levels of the system. The PC/CAS bus-based automatic dosage system is not only simple in hardware implementation, but also reliable in software implementation, which better realizes the real-time communication of multiple machines in the system and solves the data conflict problem. Long time communication test results show that the system of multi-machine communication operation is reliable, real-time is good, better meet the system requirements.
1 multi-computer real-time communication hardware realization
Automatic feeding system composed of computer and multiple devices
The weighing and dosing system uses RS-232 serial communication to realize the real-time monitoring of the whole system by the upper computer, and at the same time, in order to improve the hardware stability and optimize the system performance, this paper adopts the hardware arbitration circuit to realize the allocation of the data bus and reasonably solves the problem of data conflict, simplifies the software design, and improves the reliability of automatic dosage and the anti-interference ability.
1.1 RS-232 multi-computer communication mechanism
Ordinary RS-232 adopts point-to-point serial data transmission mode, which can not meet the real-time communication between multiple devices, so it is necessary to make some improvements to its connection.The point-to-point communication of RS-232 is to connect the data transmitting end of the interface (TXD) to the data receiving end of the other party (RXD), RXD is connected to the TXD of the other party, and the ground end of the two sides (GND) is directly connected. However, some changes occur when the computer is connected to multiple devices. The data sending end of the computer is directly connected to the data receiving end of each device, the ground end is directly connected to the ground end of each device, but the data sending end of each device can not be connected to the data receiving end of the computer at the same time, because the data sending end of the data sent in the absence of a high level, while the data sent for the high and low levels of the composition of the pulse sequence, the direct connection of the two or more data sending end together to access the computer, it will cause uncertainty in the level of its data receiving end, resulting in a data conflict problem. In order to solve the above problem, a specialized hardware arbitration circuit is designed to connect the TXD and data transmission request (DTR) of each device, and then connect the output of the arbitration circuit to the data receiving end of the computer.
1.2 Arbitration circuit structure and working principle
Since the transmitter of the upper computer (PC) is a one-to-many broadcast transmission, the data frame sent by the upper computer can be received by all the lower computers (CAS), and which lower computer receives the data frame is determined by the destination address ID in the communication protocol. However, for the lower computer, its transmitter is a many-to-one connection, and if it is connected directly, a data conflict will occur. The arbitration circuit shown in Figure 3 ensures that only one lower computer at a time can send data to the upper computer's data receiving line. It should be noted that the high and low levels used in the RS-232 interface are different from the high and low levels in TTL, and level conversion is required.
Each lower unit initializes with its DTR set to low, because gates 1, 2, and 3 have low level inputs, so points A, B, and C are all low after initialization, and gates 4, 5, and 6 also output low levels all the time. Assuming that the lower computer 1 is the first lower computer to send data, before sending their DTR port is set to high, and at this time, the two points B, C are still low, after the inverter into a high level input with the gate 1, so with the output of the gate 1, point A level is also high, at this time, the data sending service program reads the data sending query terminal (DSR) level state (i.e., the A point level state) is "1", indicating that the bus is not occupied and is in idle state. Since the DTR port of the lower unit 1 has been set high and the query learns that the bus is idle, its DTR port remains unchanged and is still set high, so that the lower unit 1 occupies the bus and can start sending data. Data from the TXD terminal through the gate 4 after the input to the upper computer RXD terminal, because at this time the level of point A is high, to ensure that the data through the gate 4 after no distortion directly uploaded to the upper computer, while the level of point A by the inverter into a low level after the input and the gate 2, 3, so the level of the B, C two points is low, and the gate 5, 6 is always a low output, to avoid the data conflict. When the lower computer 1 data sent to the end of its DTR port low, point A level becomes low, releasing the bus, and back to the initialization of the state, you can supply the next to send data to the next bit!
Different types of data sending have different priorities, the higher the priority of the data sending task, the shorter the query cycle, ensuring the real-time communication of the weighing and loading system. Due to the difference in the sending time of each sender, the first lower computer to query the bus idle can use the data bus, and before sending the data, it has already turned its DTR from low to high, and the arbitration circuit will turn the bus state to busy, so as to avoid the rest of the lower computer to use the bus at the same time to cause the data conflict. When the data transmission is finished, DTR is turned to low level and the arbitration circuit turns the bus state to idle. Since the PC is a one-to-many transmitter, the lower computer locks the bus through the arbitration mechanism only to ensure that only one CAS on the data upload bus sends data to the PC at the same moment, and will not affect the PC to send data to each CAS through the data downlink bus, thus realizing a reasonable allocation of the bus and solving the data conflict problem.
2 Software realization of multi-computer real-time communication
2.1 RS-232 data transmission format
RS-232 data is sent in the form of bytes, a byte is sent at a time
Containing a start bit, 8 data bits and a parity bit and stop bit, the data bit for the object being sent, and the data frame is a byte sequence of multiple bytes packaged in a certain format and sent in the form of a byte stream.
2.2 Frame Driver
The frame driver of the weighing and feeding system is a software toolkit designed to enable communication with third-party devices, stored in the CAS application ROM, which completely controls the hardware part of the communication without changing the form of the frames. Normally for data communication, where read and write operations are performed on the data, the user must know the details of the port very well in order to be able to realize the operations on the pins of the interface through programming. The frame driver will centralize these operations, the user only needs to understand the details of the communication protocol of the third-party product (including the composition of the information frame format, etc.), and use the frame driver to write the same communication protocol with the third-party product communication protocol of the same communication protocol, it is easy to realize the communication between CAS and the third-party product. At the same time, the frame driver supports RS-232, RS-422, RS-485, CAN and other interfaces, so as long as there are some slight changes in the software, it can support different interfaces for data communication, which is scalable and has a wide range of application prospects.
2.3 Communication protocol development
The communication between PC and equipment and metering module needs to formulate a reasonable communication protocol before communication can be carried out. To this end, the structure of the data frame is defined in the principle of full utilization of resources, simple structure and strong expandability.
The destination ID and source ID are allocated one byte to mark the receiving device and sending device of the data frame, so that the receiving device can judge the reception and call the sending device to retransmit in case of reception error. The command word gives the type of data frame (data or control word), indicating whether the subsequent DATA is a data content or a control word. This allows each device in the system to packetize the control commands or data it wishes to send into the DATA according to a pre-established communication protocol, or to unpack the received data frame into control commands and execute them or process the received data according to the protocol. The number of frames specifies the number of data frames currently being sent so that the receiving device can determine whether any data frames are missing to call the sending device to retransmit. Finally, the received check digit and self-calculated check digit results are consistent, consistent with the corresponding operation, otherwise the frame is discarded and call the sending device to retransmit.
In order to ensure the reliability of data reception, the loading system uses the answer method to communicate, the receiver returns an answer frame to confirm the data received, the transmitter receives the frame and then sends the next frame of data, if there is no answer, then the cycle cycle sends the same frame, such as the timeout is not answered, then the data frame is sent to the host computer to report an error, the host computer receives the display of the transmission and reception device ID and the type of error and the alarm to alert the monitor The upper computer will display the ID of the transmitting and receiving devices and the type of error and alarm to prompt the monitoring personnel to eliminate the error.
2.4 Communication between devices in the system
①Communication between PC and devices. PC sends data frames to the lower computer by broadcasting, but only the lower computer that matches the target address ID can accept it, and the lower computer sends data to the upper computer by using an arbitration mechanism.
② Communication between dosage equipment and weighing. The communication between the equipment and the weighing is in the form of data frame transmission by data forwarding through the PC. When the monitoring computer receives a data frame which is not the destination address of itself, it sends it directly to the data transmission bus to be accepted by the destination CAS.
2.5 Realization of CAS data communication based on C language
① Lower computer programming. Use the standard PC provided.
The PC sends the program flow because of one-to-many broadcast sending, omitting the query bus process, and sends it directly. The device receiving program flow is basically the same as the PC receiving program flow, but after judging that the destination ID of the received data frame does not match the local ID, the device directly exits the event-triggered service program and returns, while the PC sends the obtained data frame to each downstream device so that the destination device receives the data frame.
② Programming the upper computer. Weighing and loading system using VisualBasic, the process needs to send data or commands as long as the defined communication protocol for data framing and call the data sending program to send it through the serial port. In order to realize the real-time reception of data, in the VB project imported PCommPro (this software with the help of WindowsAPI function calls to control the use of the serial communications port) of the communication function library and controls to achieve real-time triggering of the acceptance of data. Event triggering in addition to the timer or loop polling (Polling) way, but also can use the callback function to achieve the PComm for the event processing is to use the callback function to deal with the way to establish the event, you need to give a function of the address, when the event occurs, it will be to the address of the callback function to the execution of the program code. Therefore, the callback function is defined as an event-triggered service program that accepts data, and when the event of accepting data occurs, the event-triggered program will be executed to save the accepted data and unpack the data frames according to the protocol, and then the data will be taken out and processed accordingly and displayed on the monitor, so as to monitor it in real time or to execute the corresponding commands.
3 Conclusion
In this paper, the hardware arbitration circuit is used to realize the data bus allocation skillfully, solve the data conflict problem, simplify the design of dosage system and optimize the system performance. The automatic dosage system constitutes RS-232 bus dosage through organic combination with PC, and realizes real-time monitoring of the whole dosage process with visualized graphical interface. The experiment proves that the system runs well and the monitoring is accurate, and it can also be applied to other distributed measurement and control systems and similar systems, which has certain engineering application value.