Home > Net >  C # write modbus communication single coil problem
C # write modbus communication single coil problem

Time:09-22

 
Private void ExecuteFunction ()
{
String connetStr="server=10.10.20.5; Port=3309; User=root; Password=rootqsr; The database=db_qualitymanager; ";
MySqlConnection conn=new MySqlConnection (connetStr);
Try
{
If (port. IsOpen==false)
{
Port. The Open ();
}
If (functionCode!=null)
{
SetReadParameters ();
CoilsBuffer=master. ReadCoils (slaveAddress startAddress, numberOfPoints);
for (int i=0; i <20; I++)
{
Bool m=coilsBuffer [I + 1);//coil state
If (m==true)
{

SetReadParameters ();
RegisterBuffer=master. ReadHoldingRegisters (slaveAddress startAddress, numberOfPoints);
String x=registerBuffer [I * 2 + 10] + "";
String y=registerBuffer [I * 2 + 12] + "";
TextBox2. Text=x;
TextBox3. Text=y;
String mm=m + ";"

Conn. The Open ();
Console. WriteLine (I);
String today=DateTime. Now. ToString (" yyyy - MM - dd ");
String SQL=the string. Format (" insert into z_01 (vultime opetime, prdate, DPR) values (' {0} ', '{1}' and '{2}', '{3}) ", x, y, today, mm).
MySqlCommand CMD=new MySqlCommand (SQL, conn);
CMD. ExecuteNonQuery ();
conn.Close();
SlaveAddress=byte. Parse (" 1 ");
StartAddressm=ushort. Parse (" 0 ");
TextBox1. Text=startAddress + "";
CoilsBuffer [I + 1]=false;
Master. WriteSingleCoilAsync (slaveAddress startAddressm, coilsBuffer [I + 1));
}
SetMsg (coilsBuffer [I] + "");

}
SetMsg (" \ r \ n ");
}
The else
{
MessageBox. Show (" please select a function code!" );
}
Port. The Close ();
}
The catch (Exception ex)
{

MessageBox. Show (ex. Message);
}
}

CodePudding user response:

Under the great god, please give directions, after entering the IF I m will coil into a false, why recycle the same position or show m=true, is that I write coil in the right position, ask for leave how change PLC in m
  • Related