Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When you set the Modbus.LogLevel.LEVEL_RELEASE value, The message could not be received in frameReceivedEvent. #67

Open
cattodie opened this issue Mar 29, 2021 · 0 comments

Comments

@cattodie
Copy link

I used and wrote a modbus java program in jlibmodbus-1.2.8.4 and it works fine.
Also, it uses FrameEventListener to receive events.
The code is as follows:


try {
		Modbus.setLogLevel(Modbus.LogLevel.LEVEL_WARNINGS);
		TcpParameters tcpParameters = new TcpParameters();
		tcpParameters.setHost(InetAddress.getByName(modbusServer));	
		tcpParameters.setPort(modbusPort);
		tcpParameters.setKeepAlive(true);

		Modbus.setAutoIncrementTransactionId(true);

		ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
		master.setResponseTimeout(modbusResponseTimeout);

		FrameEventListener listenerModbus = new FrameEventListener() {
			@Override
			public void frameSentEvent(FrameEvent event) {
				System.out.println("frame sent " + DataUtils.toAscii(event.getBytes()));

			}
			@Override
			public void frameReceivedEvent(FrameEvent event) {
				System.out.println("frame recv " + DataUtils.toAscii(event.getBytes()) + ", len = " + event.getBytes().length);
				System.out.println("len = " + event.getBytes().length);
				
				ModbusHoldingRegisters registers = new ModbusHoldingRegisters();
				
				int index = 0;
				byte [] buff = event.getBytes();
				
				JSONObject pubMsg =  new JSONObject();
				pubMsg.put("devInfo", threadName );
				pubMsg.put("pdu", DataUtils.toAscii(buff));
				async_pub.publish(t_channels[1], pubMsg.toJSONString());
				
				pubMsg =  new JSONObject();	
				pubMsg.put("devInfo", threadName );
				
				int tid = DataUtils.BeToIntArray(buff)[index];
				System.out.println(String.format("TID 2byte= 0x%04X", tid));
				index += 2;
				
				int pid = DataUtils.BeToIntArray(buff)[index/2];
				System.out.println(String.format("PID 2byte= 0, ==> 0x%04X", pid));
				index += 2;
				
				int len = DataUtils.BeToIntArray(buff)[index/2];
				System.out.println(String.format("Length 2byte = 0x%04X, %d", len, len));
				index += 2;
				
				int uid = (int)buff[index];
				System.out.println(String.format("UID = 0x%02X", uid));
				pubMsg.put("uid", uid); 
				index += 1;
				
				int func = (int)buff[index];
				System.out.println(String.format("Func 1byte= 0x%02X", func));
				pubMsg.put("func", func); 
				index += 1;
				
				switch(func) {					
					case 0x03: 	// read holding : Bytecount 1, reg 2n
					{
						int bytecount = (int)buff[index];
						System.out.println(String.format("bytecount 1byte = 0x%02X, %d", bytecount, bytecount));
						pubMsg.put("bytecount", bytecount); 
						index += 1;
						
						byte [] byteArray = Arrays.copyOfRange(event.getBytes(), index, event.getBytes().length);
						pubMsg.put("values", DataUtils.toAscii(byteArray));
					}
					break;
						
					case 0x04 : // read input : Bytecount 1, reg 2n
					{
						int bytecount = (int)buff[index];
						System.out.println(String.format("bytecount 1byte = 0x%02X, %d", bytecount, bytecount));
						pubMsg.put("bytecount", bytecount); 
						index += 1;
						
						byte [] byteArray = Arrays.copyOfRange(event.getBytes(), index, event.getBytes().length);
						pubMsg.put("values", DataUtils.toAscii(byteArray));
					}
					break;
						
					case 0x06 : // write single : RegisterAddress 2 , Register Value 2
					{
						int regAddr = DataUtils.BeToIntArray(buff)[index/2];
						System.out.println(String.format("regAddr 1byte = 0x%02X, %d", regAddr, regAddr));
						pubMsg.put("regAddr", regAddr); 
						index += 2;
						
						byte [] byteArray = Arrays.copyOfRange(event.getBytes(), index, event.getBytes().length);
						pubMsg.put("value", DataUtils.toAscii(byteArray));
					}
					break;
						
					case 0x10 : // write Multi : StartAddr 2 , Quantity of Outputs 2
					{
						int startAddr = DataUtils.BeToIntArray(buff)[index/2];
						System.out.println(String.format("regAddr 1byte = 0x%02X, %d", startAddr, startAddr));
						pubMsg.put("startAddr", startAddr); 
						index += 2;
						
						int quantity = DataUtils.BeToIntArray(buff)[index/2];
						pubMsg.put("quantity", quantity);
						
					}
					break;
					
					default : 
					{
						System.out.println("Not Support Modbus Function : " + func);
					}
					break;
					
				}
				
				async_pub.publish(t_channels[0], DataUtils.toAscii(byteArray)); // Redis pub

			}
		};
		master.addListener(listenerModbus);

		if (!master.isConnected()) {
			master.connect();
		}
	} catch (UnknownHostException e) {
		e.printStackTrace();
	} catch (ModbusIOException e) {
		e.printStackTrace();
	}

Lettuce library is used for communication between modules using Redis' pub/sub function.

When setting the value of Modbus.LogLevel.LEVEL_RELEASE for distribution,
The message cannot be received in frameReceivedEvent(FrameEvent event),
There is also no output from System.out.println().

However, there is no problem at all when it is set to Modbus.LogLevel.LEVEL_DEBUG/LEVEL_VERBOSE/LEVEL_WARNINGS.

How to solve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant