internal async Task <int> WriteAsync(object value) { try { if (!m_device.Gateway.isConnected) { m_device.Gateway.ConnectAsync().Wait(); } if (m_device.Gateway.isConnected) { ushort index = m_device.AttributeMap[m_attributeName]; short input = 0; switch (index) { case 2: case 3: input = Convert.ToInt16((double)value * 10); m_request = new ModbusRequest(1, 1, 6, index, 1, 2, new ushort[1] { (ushort)input }); m_response = await m_device.Gateway.sendRequestAsync(m_request); break; case 4: case 7: m_request = new ModbusRequest(1, 1, 6, index, 1, 2, new ushort[1] { (ushort)value }); m_response = await m_device.Gateway.sendRequestAsync(m_request); break; case 6: input = Convert.ToInt16(value); m_request = new ModbusRequest(1, 1, 6, index, 1, 2, new ushort[1] { (ushort)input }); m_response = await m_device.Gateway.sendRequestAsync(m_request); break; case 0: case 1: case 5: default: break; } } else { return(-1); } } catch { } return(0); }
}// end disconnect public async Task <ModbusResponse> sendRequestAsync(ModbusRequest request) { ModbusResponse response = null; //Send Read Request uint status = await clientSocket.OutputStream.WriteAsync(request.getByteArray().AsBuffer()); bool done = await clientSocket.OutputStream.FlushAsync(); //Get Read Response response = new ModbusResponse(); uint bufferLen = getResponseBufferLength(request); byte[] readBuffer = new byte[bufferLen]; var readResponse = await clientSocket.InputStream.ReadAsync(readBuffer.AsBuffer(), bufferLen, InputStreamOptions.Partial); response.FunctionCode = readBuffer[7]; if ((readBuffer[7] & 0x80) == 0x80) //check exception: readBuffer[7] & 0x80 { m_errorMsg = String.Format("[Error] Read operation failed. Exception Code: {0}.", readBuffer[8]); response.Status = readBuffer[8]; } else { //Get Read Response switch (response.FunctionCode) { case 1: //Read Coils case 2: //Read Discrete Input case 3: //Read Holding Registers case 4: //Read Input Registers response.ByteCount = readBuffer[8]; response.Data = parseReadResponse(readBuffer); response.Status = 0; break; case 5: //Write Single Coil case 6: //Write Single Register response.Address = (UInt16)((readBuffer[8] << 8) + readBuffer[9]); response.Data = new ushort[1] { (UInt16)((readBuffer[10] << 8) + readBuffer[11]) }; response.Status = 0; break; case 15: //Write Multiple Coils case 16: //Write Multiple Registers response.Address = (UInt16)((readBuffer[8] << 8) + readBuffer[9]); response.RegCount = (UInt16)((readBuffer[10] << 8) + readBuffer[11]); response.Status = 0; break; default: break; } }// end if-else check function code return(response); }//END readHelper
internal async Task <uint> ReadAsync() { try { if (!m_device.Gateway.isConnected) { m_device.Gateway.ConnectAsync().Wait(); } if (m_device.Gateway.isConnected) { ushort index = m_device.AttributeMap[Value.Name]; m_request = new ModbusRequest(1, 1, 3, index, 1); m_response = await m_device.Gateway.sendRequestAsync(m_request); switch (index) { case 0: case 1: case 2: case 3: Value.Data = ((short)m_response.Data[0]) / 10.0; break; case 4: case 7: case 5: Value.Data = m_response.Data[0]; break; case 6: Value.Data = (short)m_response.Data[0]; break; default: break; } } else { return(2); } } catch { return(1); } return(0); }