Modbus Logs out of order, timeout issue

Below is simplified code. I am polling Modbus records from my device and missing the last one and always getting an error 80 on the first poll.

function testing(){
local old_id=0
local logid=0
local maxlogid=0
//get the present LogID and MaxLogID
 modbus.read(SLAVE_ADDRESS, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 3200 , 4, function(error, results) {
        if (error) {
				server.error(error);
			} 
			else 
			{
                logid = results[0];
                maxlogid = results[1];
                server.log("Log ID: " + logid)
                server.log("Max ID: " + maxlogid)
			    //set the log id to 1
			    modbus.write(SLAVE_ADDRESS, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 710, 1, [old_id], function(error, result) {  //put point on requested log
                if (error) {
        				server.error(error);
        			} 
        			else 
        			{
        			    
        			  do{
        			    old_id++; //When polling 710, the pointer will get increment automatically
        			    modbus.read(SLAVE_ADDRESS, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 710 , 18, function(error, results) { //request log at pointer
            			if (error) {
            				server.error(error);
            			} 
            			else 
        			    {
        			    server.log("Num: "+ old_id)
        			    }
        			    }.bindenv(this));
        			  }while(old_id!=logid)
        			   
        			   server.log("Hooray")
        			}
                }.bindenv(this));
			}
            }.bindenv(this));
		}

Here the results when I remove the modbus.read(SLAVE_ADDRESS, MODBUSRTU_TARGET_TYPE.HOLDING_REGISTER, 710 , 18, function(error, results) { //request log at pointer

2020-03-25T14:24:34.586 +00:00 [Status] Downloading new code; 19.10% program storage used
2020-03-25T14:24:35.480 +00:00 [Device] Outgoing ADU : 01 03 0C 80 00 04 46 B1
2020-03-25T14:24:35.607 +00:00 [Device] Incoming ADU : 01 03 08 00 03 00 03 00 04 00 04 A2 D5
2020-03-25T14:24:35.607 +00:00 [Device] Log ID: 3
2020-03-25T14:24:35.637 +00:00 [Device] Max ID: 3
2020-03-25T14:24:35.637 +00:00 [Device] Outgoing ADU : 01 06 02 C6 00 00 68 4F
2020-03-25T14:24:35.638 +00:00 [Device] Incoming ADU : 01 06 02 C6 00 00 68 4F
2020-03-25T14:24:35.638 +00:00 [Device] Num: 1
2020-03-25T14:24:35.638 +00:00 [Device] Num: 2
2020-03-25T14:24:35.639 +00:00 [Device] Num: 3
2020-03-25T14:24:35.640 +00:00 [Device] Hooray

Now add it back and this is what I get

2020-03-25T14:26:52.802 +00:00 [Status] Downloading new code; 19.17% program storage used
2020-03-25T14:26:53.712 +00:00 [Device] Outgoing ADU : 01 03 0C 80 00 04 46 B1
2020-03-25T14:26:53.718 +00:00 [Device] Incoming ADU : 01 03 08 00 03 00 03 00 04 00 04 A2 D5
2020-03-25T14:26:53.719 +00:00 [Device] Log ID: 3
2020-03-25T14:26:53.735 +00:00 [Device] Max ID: 3
2020-03-25T14:26:53.742 +00:00 [Device] Outgoing ADU : 01 06 02 C6 00 00 68 4F
2020-03-25T14:26:53.761 +00:00 [Device] Incoming ADU : 01 06 02 C6 00 00 68 4F
2020-03-25T14:26:53.762 +00:00 [Device] Hooray
2020-03-25T14:26:53.762 +00:00 [Device] Outgoing ADU : 01 03 02 C6 00 12 24 42
2020-03-25T14:26:56.801 +00:00 [Device] ERROR: 80
2020-03-25T14:26:56.803 +00:00 [Device] Outgoing ADU : 01 03 02 C6 00 12 24 42
2020-03-25T14:26:56.852 +00:00 [Device] Incoming ADU : 01 03 24 4A 1D 36 2C 4A 1D 36 2C 47 7F FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F2 E6
2020-03-25T14:26:56.852 +00:00 [Device] Num: 3
2020-03-25T14:26:56.852 +00:00 [Device] Outgoing ADU : 01 03 02 C6 00 12 24 42
2020-03-25T14:26:56.953 +00:00 [Device] Incoming ADU : 01 03 24 46 FE 10 00 47 58 1C 00 42 8C 00 00 00 00 00 00 41 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 49 42
2020-03-25T14:26:56.953 +00:00 [Device] Num: 3

Notice that Num Just goes to 3, not counting any more, First request for log is a timeout (ERROR: 80)… Ideas…

Can I get clarification on the issue? A quick glance implies this is not a post about logging, it’s a post about modbus. Is that correct?

It’s a little hard to read your code without the proper formatting. From what I can see, your problem may be related to mixing synchronous and asynchronous code. You have a do{}while loop that calls modbus.read() asynchronously. Is that what you want? You have no guarantee whether the callback will be invoked before or after your while loop condition is tested. It’s risky. I’ll wager that your code is quickly queuing 3 modbus requests, none of which are sent until your while loop exits. I suggest you look at recursion rather than a loop. That way you can test your exit condition in the callback handler.

Also, modbus devices may be busy after you send a write command to them. You could consider inserting a hold-off period before attempting another request.