C# Modbus Read multiple sensor's data at a time
C# Modbus Read multiple sensor's data at a time
I hope I can explain. I am trying to develop Modbus in ASCII mode. What I need to do is I have different sensor for that I have numeric textbox for selection device address of sensors, it looks like :-
When I check a checkbox sensor should have read one sensor's data and then another sensor's data. And my code for reading is below
public string cmdMake(int cmd, int rw)
{
string strCmd = "D";
if(ChkDev1.Checked == true)
{
strCmd = strCmd + Convert.ToInt32(numSlave.Value).ToString("X2");
}
if(ChkDev2.Checked == true)
{
strCmd = strCmd + Convert.ToInt32(numSlave1.Value).ToString("X2");
}
AsciiCode ascCode = new AsciiCode();
if (rw == CMD_RD) //読み込み
{
strCmd = strCmd + "07" + cmd.ToString("X2");
strCmd = strCmd + dterr_chk.CalCRC16(strCmd);
}
strCmd = strCmd + ascCode.STR_CRLF;
return (strCmd);
}
In Above code the main problem is read only last one of sensor data. In my above code numslave.value and numslave1.value is numeric textbox to choose device address and . In above code I took only two slave address's example actually I have 10 slave address. Please help me out this. Thanks in advance
@PepitoSh thank you for your reply. Do you mean I have to create function more to read different device address?
– Niksan Karkee
2 days ago
In a bus architecture, it is the job of the controller to manage the communications. Your code is the controller. Each of the slave devices will likely communicate only when properly addressed. In the case of the UI presented, I can only guess, but it likely iterates through each checkbox, establishes communication with the device, and queries for the data. As written, your code is trying to combine device addresses, which will not work. Iterate hand over hand.
– JamieMeyer
2 days ago
@JamieMeyer thank you for your comment. do you have any suggestion for this. I am thinking if I use loop for checkbox it will work but I don't no how to do this
– Niksan Karkee
2 days ago
Loop though the devices you wish to communicate to. For each device query the data items you need. In the method you compose the actual command the device id should be an incoming parameter.
– PepitoSh
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I think you have to address devices one by one and read values one by one.
– PepitoSh
2 days ago