Java read/write Mitsubishi PLC using TCP/IP protocol

Source: Internet
Author: User



This article will use a GitHub open Source component library technology to read and write Mitsubishi PLC and Siemens PLC data, using Ethernet-based TCP/IP implementation, no additional components, read operation as long as the background thread will not be stuck thread, this component supports super convenient high-performance read and write operation



GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also reward support, please look for the source code project.



This project currently supports the C # language and Java language, the C # language is more complete, the Java version of the library is still in development and improvement.









Click to download the jar package for this component. Hslcommunication.jar



Examples of code use support can be referenced in the C # version, and they are almost identical, and the supported data types are consistent.









Technical Support QQ Group: 592132877 (the component version update details will be released in the group first) Last modified: June 13, 2018 08:28:34



Inside all kinds of small partners, for you to answer data interaction, programming skills, if you have any questions about the API provided by this interface, you can add group consultation, if there are better suggestions, welcome to put forward.






The complete information and API description of the component refer to: http://www.cnblogs.com/dathlin/p/7703805.html component usage limits, update logs, all within this page.









This article will show you how to configure the network parameters and how to use the code to access the PLC data, hoping to give the people in need to solve some practical problems. Mainly to the Mitsubishi Q series PLC x,y,m,l,b,v,f,s,d,w,r area data Read and write, the Siemens PLC M,Q,I,DB block data read and write, pro-test effective.



Here the use of the direct way of the network cable, if the PLC is connected to the LAN, you can read and write remotely ^_^



There are 2 namespaces used here:





Import HslCommunication.Core.Types.OperateResultExOne;
Import HslCommunication.Profinet.Melsec.MelsecMcNet;




Just talk.


When a host computer needs to read 100 Siemens PLC equipment (here is just for example, usually use Modbus TCP is the same), you use the server initiative to request the mechanism of 100 devices is a great test of performance, If you open 100 threads to poll 100 devices, then the performance loss will be very large, not to mention the addition of equipment, if the installation of Modbus TCP server, can be a perfect solution to the performance problem, because the connection pressure will be evenly distributed to each plc, Server side as long as a new timestamp to know that the client has no connection.



We add the Modbus TCP method to 100 plc, send the data to the server's IP and port, the server distinguishes the device according to station number. This makes it possible to build a high-performance terminus. This component supports the fast setting up of a high performance Modbus TCP terminus.



Http://www.cnblogs.com/dathlin/p/7782315.html






About two modes



On the PLC side, including the Mitsubishi, Siemens, OMRON and Modbus TCP client accessors, both support two modes, short connection mode and long connection mode, now to explain what the principle.



Short Connection : Each read and write is a separate request, the request is closed, if the server's port only supports single connection, then the port can be reused after shutdown, but in frequent network requests, prone to abnormal, there will be other requests unsuccessful, Especially in the case of multithreading.



Long Connection : Create a common connection channel, all the read and write requests to use this channel to complete, so that the read and write performance faster, instant multi-threaded calls will not affect the internal synchronization mechanism. If the server's port only supports a single connection, then this port is occupied, such as the port mechanism of Mitsubishi, Siemens Modbus TCP port mechanism is also the case. The following code uses long connections by default, with higher performance and multi-threaded synchronization.



In the short-connected mode, each request is a separate access, so there is no reconnection, in the long-connected mode, if this request fails, the next time the request, it will automatically reconnect to the server until the request is successful. In addition, as far as possible, all reading and writing are judged on the success of the results.






About Logging



Temporarily not supported





Configuration of PLC





Environment 1: here with GX WORKS3 as an example, the FX5U is configured as follows: (thanks to the pictures provided by Hawthorn )











Environment 2: Here is the GX Works2 example, the Test plc is L02CPU, the built-in Ethernet protocol













Environment 3: Here with the GX Works2 as an example, add the Ethernet module, the model is qj71e71-100, configuration, after the addition of the Ethernet parameters configured, it should be noted here: Parameters are configured to correspond to the configuration parameters in the following code






Note : In the configuration of the PLC Ethernet module, can not set the network number is 0, and can not set the station number is 0, so here are set to 1, in C # program also use the above configuration, in the code is configured to 0, if you customize the network 2, station number 8, Then write the corresponding data in the code. If the communication still fails, re-test 0, 0.

Open Settings : In the Open settings option, the configuration of other parameters, just to cite an example, opened 4 ports to support read and write operations:














Port number Setting rules:


    • In order not to conflict with a previously existing system, you try to use your own port when you add your own port.
    • If both reads and writes are required, it is better to differentiate between the read port and the write port as much as possible.
    • If your network status is not particularly stable, the read port uses 2, and a blocked switch to another read can improve the stability of the system.



This document is for component testing only, so only one port is used for reading and writing. If your program also uses a port, then when you read the data, it is also written (the asynchronous operation may occur), then the write will fail! (without this problem in long connection mode)

Mitsubishi PLC Data mainly consists of two types of data, bit data and word data, in-place data, such as x,y,m,l are bit data, Word data such as d,w. There is a small difference between the two types of data in reading and decoding. (In fact, you can first assign 16 m to a D, read D data and then parse, when the number of M is more than the time, so that the operation more efficient)

Initializing the Access Plc object



If you want to use the data read function of this component, you must first initialize the data Access object and fill in the data according to the actual situation. The following are just the data in the test:





Melsecmcnet melsec_net = new Melsecmcnet ("192.168.1.192", 6001);








Then you can specify a number of parameters, network number, network station number, and so on, the general situation is not required to specify








   melsec_net.setNetworkNumber((byte) 0x00);
        melsec_net.setNetworkStationNumber((byte) 0x00);
        melsec_net.setConnectTimeOut(1000);








Open connection





Melsec_net. ConnectServer ();


If you want to know if there's any connection,





OperateResult connectResult = melsec_net.ConnectServer();
         if(connectResult.IsSuccess){
             System.out.print("Connected successfully");
         }
         else {
             System.out.print("Connection failed:"+connectResult.Message);
         }








about how addresses are represented


Using a string representation, all read and write operations in this component provide a string representation of overloaded methods, all supported access types correspond to the following, and the string representation exists in decimal and hexadecimal differences:


Input relays: "X100", "x1a0"//string 16 binary mechanism
Output relays: "Y100", "y1a0"//string 16 binary mechanism
Internal relay: "M100", "M200"//String = Decimal
Latching relays: "L100", "L200"//String = Decimal
Alarm: "F100", "F200"//String is decimal
Edge relay: "V100", "V200"//String = Decimal
Link relay: "B100", "b1a0"//String hexadecimal
Step relay: "S100", "S200"//string is in decimal
Data register: "D100", "D200"//String is decimal
Link register: "W100", "w1a0"//string is hexadecimal
File register: "R100", "R200"//String is decimal

About data classification


The above address data is divided into bits data and Word data, bit data can only call Readbool, Word data with read and its extension method





Simple Read and write examples
boolean[] M100 = melsec_net.ReadBool("M100",(short) 1).Content; // read whether M100 works, decimal address
        boolean[] X1A0 = melsec_net.ReadBool("X1A0",(short) 1).Content; // Whether to read X1A0, hexadecimal address
        boolean[] Y1A0 = melsec_net.ReadBool("Y1A0",(short) 1).Content; // Read whether Y1A0 works, hexadecimal address
        boolean[] B1A0 = melsec_net.ReadBool("B1A0",(short) 1).Content; // Whether to read B1A0, hexadecimal address
        short short_D1000 = melsec_net.ReadInt16("D1000").Content; // Read the short value of D1000, W3C0, R3C0 have the same effect
        int int_D1000 = melsec_net.ReadInt32("D1000").Content; // read int data composed of D1000-D1001
        float float_D1000 = melsec_net.ReadFloat("D1000").Content; // Read the float data composed of D1000-D1001
        long long_D1000 = melsec_net.ReadInt64("D1000").Content; // Read long data composed of D1000-D1003
        double double_D1000 = melsec_net.ReadDouble("D1000").Content; // Read the double data composed of D1000-D1003
        String str_D1000 = melsec_net.ReadString("D1000", (short) 10).Content; // Read the barcode data composed of D1000-D1009



        melsec_net.Write("M100", new boolean[] {true} ); // Write M100 to pass
        melsec_net.Write( "Y1A0", new boolean[] {true} ); // Write Y1A0 to pass
        melsec_net.Write( "X1A0", new boolean[] {true} ); // Write X1A0 to pass
        melsec_net.Write( "B1A0", new boolean[] {true} ); // Write B1A0 to pass
        melsec_net.Write( "D1000", (short)1234); // Write D1000 short value, W3C0, R3C0 have the same effect
        melsec_net.Write( "D1000", 1234566); // write D1000 int value
        melsec_net.Write( "D1000", 123.456f); // write D1000 float value
        melsec_net.Write( "D1000", 123.456d); // write D1000 double value
        melsec_net.Write( "D1000", 123456661235123534L); // write D1000 long value
        melsec_net.Write( "D1000", "K123456789"); // write D1000 string value





Read and write instructions for x,y,m,l,f,v,b,s bit data

    • X Input Relays
    • Y Output Relays
    • M Internal Relays
    • L Latching Relays
    • F Alarm
    • V Edge Relay
    • B Link Relay
    • S Step Relay





This section will show the reading of eight bits of data, although more often just read the D data, or the bit data into the D data in bulk, but still here to introduce a separate read x,y,m,l,f,v,b,s, because these eight kinds of reading methods consistent, so for the M data is introduced, You can test the others yourself.

The following method demonstrates reading the value of the m200-m209 10 m, note: The read length must be even, write an odd number immediately, will be filled to even, read and write the maximum length of 7168, otherwise error. If the actual demand is indeed greater than 7168, please read it in batches.
Return value resolution: A total of 10 bytes of data is returned if the read is normal, and the following sample data is read in batches





OperateResultExOne<boolean[]> read = melsec_net.ReadBool("M100",(short)10);
         if(read.IsSuccess){
             boolean m100 = read.Content[0];
             boolean m101 = read.Content[1];
             boolean m102 = read.Content[2];
             boolean m103 = read.Content[3];
             boolean m104 = read.Content[4];
             boolean m105 = read.Content[5];
             boolean m106 = read.Content[6];
             boolean m107 = read.Content[7];
             boolean m108 = read.Content[8];
             boolean m109 = read.Content[9];
         }
         else {
             System.out.print("Read failed:"+read.Message);
         }







// Write test, M100-M104 write test. After writing here, M100: on M101: off M102: off M103: on M104: on
         boolean[] values = new boolean[]{true,false,false,true,true};
         OperateResult write = melsec_net.Write("M100",values);
         if(write.IsSuccess){
             System.out.print("Write successfully");
         }
         else {
             System.out.print("Write failure:"+write.Message);
         }








Read and write operation of d,w,r Word data

Read here for the existence of integer data in the middle of the case, because both read the same way, so that only a data read, using the component to read data, read or write up to 960 words at a time, exceeding the failure. If the length of the read does exceed the limit, consider reading in batches.







OperateResultExOne<byte[]> read1 = melsec_net.Read("D100",(short)5);
         if(read1.IsSuccess){
             short D100 = melsec_net.getByteTransform().TransByte(read1.Content,0);
             short D101 = melsec_net.getByteTransform().TransByte(read1.Content,2);
             short D102 = melsec_net.getByteTransform().TransByte(read1.Content,4);
             short D103 = melsec_net.getByteTransform().TransByte(read1.Content,6);
             short D104 = melsec_net.getByteTransform().TransByte(read1.Content,8);
         }
         else {
             System.out.print("Read failed:"+read1.Message);
         }







// D100 is 1234, D101 is 8765, D102 is 1234, D103 is 4567, D104 is -2563
         short[] values2 = new short[]{1335, 8765, 1234, 4567, -2563 };
         OperateResult write = melsec_net.Write("M100",values2);
         if(write.IsSuccess){
             System.out.print("Write successfully");
         }
         else {
             System.out.print("Write failure:"+write.Message);
         }





A practical example of a complex demonstration

The actual situation may be encountered is very complex, a device needs to upload data containing temperature, pressure, output, specifications and so on, in a string of data will contain a variety of different data, the above reading D, reading M, the way to read the bar code is not very good, so here to do a complete example of the demonstration, Suppose we need to read d4000-d4009 data, assuming that D4000 stored the temperature data, 55.1 ℃ in D for the 551,d4001 storage pressure data, 1.23MPa stored in D for 123,d4002 storage device state, 0 for the stop, 1 for the operation, D4003 Storage of production, 1000 means 1000, D4004 standby, d4005-d4009 storage specifications, the following code demonstrates how to parse the data:








//Analyze complex data
         OperateResultExOne<byte[]> read3 = melsec_net.Read("D4000", (short) 10);
         if (read3.IsSuccess)
         {
             double temperature = melsec_net.getByteTransform().TransInt16(read3.Content, 0) / 10d;//Index is very important
             double pressure = melsec_net.getByteTransform().TransInt16(read3.Content, 2) / 100d;
             boolean IsRun = melsec_net.getByteTransform().TransInt16(read3.Content, 4) == 1;
             int output =melsec_net.getByteTransform().TransInt32(read3.Content, 6);
             String specification = melsec_net.getByteTransform().TransString(read3.Content, 10, 10,"ascii");
         }
         else
         {
             System.out.print("Read failed:"+read3.Message);
         } 








For more detailed information, you can refer to the test items in the source code.





Java read/write Mitsubishi PLC using TCP/IP protocol


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.