Home > Net >  C # the modbus communication
C # the modbus communication

Time:10-20

The letter jie in the PLC data stored in the MySQL (RTU& TCP)
Q: the long linlin YaoYao wu wife wife

CodePudding user response:

Nobody answered, not time-consuming project

CodePudding user response:

Letter jie PLC has a data interface? Can take out the data? If can, which is not on the database of new operation?

CodePudding user response:

refer to the second floor jameswjw response:
letter jie PLC has a data interface? Can take out the data? If can, which is not on the database of new operation?

Have, and I also took out the deposit to the database, but the data are missing, but finding someone to help you write only one, with a limited knowledge of the
Link is the https://download.csdn.net/download/nebuung/12928267 I wrote

CodePudding user response:

refer to the second floor jameswjw response:
letter jie PLC has a data interface? Can take out the data? If can, which is not on the database of new operation?

See set free download also collect COINS, C code all posted
 using System; 
using System.Windows.Forms;
Using Modbus. Device;
Using System. IO. Ports;
Using MySql. Data. MySqlClient;

The namespace Modbus72
{
Public partial class MainForm: Form
{
Private static IModbusMaster master;
Private static SerialPort port;
//write coil or register array
Private bool [] coilsBuffer;
Private ushort [] registerBuffer;
//parameter (station number respectively, and the start address and length)
Private byte slaveAddress;
Private ushort startAddress;
Private ushort startAddress1;
Private ushort numberOfPoints;
Private ushort numberOfPoints1;
//serial port parameters calibration data baud rate of serial port a stop bit
Private string portName;
Private int baudRate;
Private Parity Parity;
Private int dataBits;
Private StopBits StopBits;
Public MainForm ()
{
InitializeComponent();
}

Private void MainForm_Load (object sender, EventArgs e)
{
Timer1. Interval=3000;
Btnstart. Enabled=true;
Btnstop. Enabled=false;
}
Private SerialPort InitSerialPortParameter ()
{
If (cbxcom SelectedIndex & lt; 0)
{
MessageBox. Show (" please select serial number ");
return null;
}
The else
{
PortName=cbxcom. SelectedItem. ToString ();
BaudRate=19200;
Parity=parity. Even;
DataBits=8;
StopBits=stopBits. One;
Port=new SerialPort (portName baudRate, parity, dataBits, stopBits);
Return to the port.
}
}

Private void btnread_Click (object sender, EventArgs e)
{
Try
{
//initialize serial port parameters
InitSerialPortParameter ();
Master=ModbusSerialMaster. CreateRtu (port);
ExecuteFunction ();
}
The catch (Exception)
{
MessageBox. Show (" initialization exception ");
}
}
Private void ExecuteFunction ()
{
SlaveAddress=byte. Parse (" 1 ");
String today=DateTime. Now. ToString (" MM - dd yyyy - HH: MM: ss ");
String connetStr="server=10.10.20.5; Port=3309; User=root; Password=rootqsr; The database=db_qualitymanager; ";
MySqlConnection conn=new MySqlConnection (connetStr);
Try
{
//each operation is to open the serial port operation need to be closed after the completion of a serial port
//in order to change slave connection is not an error
If (port. IsOpen==false)
{
Port. The Open ();
}
StartAddress=ushort. Parse (" 0 ");
NumberOfPoints=ushort. Parse (" 20 ");
//read the coil
CoilsBuffer=master. ReadCoils (slaveAddress startAddress, numberOfPoints);
for (int i=0; I & lt; CoilsBuffer. Length; I++)
{
Bool m=coilsBuffer [I];//coil state
If (m==true)
{
//read the register
Int j=I * 4 + 6;
StartAddress1=ushort. Parse (j + "");
NumberOfPoints1=ushort. Parse (" 3 ");
RegisterBuffer=master. ReadHoldingRegisters (slaveAddress startAddress1, numberOfPoints1);
String x=registerBuffer [0] + "";
String y=registerBuffer [2] + "";
If (x=="0")
{
continue;
}
The else
{
If (I & lt; 10)
{
# region
String DPR="z_0" + I;
Conn. The Open ();
String SQL=the string. Format (" insert into "+ DPR +" (vultime opetime, prdate) values (' {0} ', '{1}' and '{2}) ", x, y, today).
MySqlCommand CMD=new MySqlCommand (SQL, conn);
CMD. ExecuteNonQuery ();
Conn. Close ();

//modify register
String s=j + "";
StartAddress=ushort. Parse (s);
String [] strarr={" 0 "};
RegisterBuffer [0]=ushort. Parse (strarr [0]).
Master. WriteSingleRegisterAsync (slaveAddress startAddress, registerBuffer [0]).
# endregion
}
The else
{
# region
String DPR="z_" + I;
Conn. The Open ();
String SQL=the string. Format (" insert into "+ DPR +" (vultime opetime, prdate) values (' {0} ', '{1}' and '{2}) ", x, y, today).
MySqlCommand CMD=new MySqlCommand (SQL, conn);
CMD. ExecuteNonQuery ();
Conn. Close ();

//modify register
String s=j + "";
StartAddress=ushort. Parse (s);
String [] strarr={" 0 "};
RegisterBuffer [0]=ushort. Parse (strarr [0]).
Master. WriteSingleRegisterAsync (slaveAddress startAddress, registerBuffer [0]).
# endregion
}
}
}
//SetMsg (coilsBuffer [I] + "");
}
//SetMsg (" \ r \ n ");
Port. The Close ();
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related