使用NModbus的Modbus

时间:2018-07-13 10:00:03

标签: c# winforms serial-port modbus

尝试使用以下代码使用NModbus在端口上发送数据。此处的目的是通过Modbus协议将数据发送到PCBA。

private bool SendDataToBoard1(byte slaveId, SerialPort Port, MainScreen.ParametersSendToModbusProtocol ParameterToWrite, MainScreen.modbusConfigPara modbusCongiData)
    {
            //Port.Close();

            //string portName = Port.PortName.ToString();

        using (Port = new SerialPort("COM4"))
        {
            Port.BaudRate = 115200;
            Port.DataBits = 8;
            Port.Parity = Parity.Even;
            Port.StopBits = StopBits.One;
            Port.Open();
            var adapter = new SerialPortAdapter(Port);
            // Create the factory
            var factory = new ModbusFactory();

            // Create Modbus Master
            IModbusMaster master = factory.CreateRtuMaster(adapter);

            // IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(Port);


            int temp = ushort.Parse("1") - 1;
            //byte slaveId = modbusCongiData.SelectedREVBoardNo;
            ushort startAddress = (ushort)(temp);
            ushort points = modbusCongiData.BytesToRead;
            ushort[] data = new ushort[120]; //123 Max characters 

            try
            {

                data[0] = ParameterToWrite.OeratingModeType_Pump1;
                data[1] = ParameterToWrite.CommunicationTypeSel_Pump1;
                data[2] = ParameterToWrite.OeratingModeLowSpeed_Pump1LByte;
                data[3] = ParameterToWrite.OeratingModeLowSpeed_Pump1HByte;
                data[4] = ParameterToWrite.OeratingModeHighSpeed_Pump1LByte;
                data[5] = ParameterToWrite.OeratingModeHighSpeed_Pump1HByte;
                data[6] = ParameterToWrite.OeratingModeTimeDuration_Pump1;
                data[7] = ParameterToWrite.OeratingModeTimeDuration_Pump1HByte;
                data[8] = ParameterToWrite.OperatingModeLowAnalogInputP1;
                data[9] = ParameterToWrite.OperatingModehighAnalogInputP1;

                data[10] = ParameterToWrite.OeratingModeType_Pump2;
                data[11] = ParameterToWrite.CommunicationTypeSel_Pump2;
                data[12] = ParameterToWrite.OeratingModeLowSpeed_Pump2Lbyte;
                data[13] = ParameterToWrite.OeratingModeLowSpeed_Pump2Hbyte;
                data[14] = ParameterToWrite.OeratingModeHighSpeed_Pump2LByte;
                data[15] = ParameterToWrite.OeratingModeHighSpeed_Pump2HByte;
                data[16] = ParameterToWrite.OeratingModeTimeDuration_Pump2;
                data[17] = ParameterToWrite.OeratingModeTimeDuration_Pump2HByte;
                data[18] = ParameterToWrite.OperatingModeLowAnalogInputP2;
                data[19] = ParameterToWrite.OperatingModehighAnalogInputP2;

                data[20] = ParameterToWrite.OeratingModeType_Pump3;
                data[21] = ParameterToWrite.CommunicationTypeSel_Pump3;
                data[22] = ParameterToWrite.OeratingModeLowSpeed_Pump3LByte;
                data[23] = ParameterToWrite.OeratingModeLowSpeed_Pump3HByte;
                data[24] = ParameterToWrite.OeratingModeHighSpeed_Pump3LByte;
                data[25] = ParameterToWrite.OeratingModeHighSpeed_Pump3HByte;
                data[26] = ParameterToWrite.OeratingModeTimeDuration_Pump3;
                data[27] = ParameterToWrite.OeratingModeTimeDuration_Pump3HByte;
                data[28] = ParameterToWrite.OperatingModeLowAnalogInputP3;
                data[29] = ParameterToWrite.OperatingModehighAnalogInputP3;

                data[30] = ParameterToWrite.OeratingModeType_Pump4;
                data[31] = ParameterToWrite.CommunicationTypeSel_Pump4;
                data[32] = ParameterToWrite.OeratingModeLowSpeed_Pump4LByte;
                data[33] = ParameterToWrite.OeratingModeLowSpeed_Pump4HByte;
                data[34] = ParameterToWrite.OeratingModeHighSpeed_Pump4LByte;
                data[35] = ParameterToWrite.OeratingModeHighSpeed_Pump4HByte;
                data[36] = ParameterToWrite.OeratingModeTimeDuration_Pump4;
                data[37] = ParameterToWrite.OeratingModeTimeDuration_Pump4HByte;
                data[38] = ParameterToWrite.OperatingModeLowAnalogInputP4;
                data[39] = ParameterToWrite.OperatingModehighAnalogInputP4;

                //Temperature adjustment setting Data
                data[40] = ParameterToWrite.Pump1DegreeCelsius;
                data[41] = ParameterToWrite.Pump1SpeedPerRPMLByte;
                data[42] = ParameterToWrite.Pump1SpeedPerRPMHByte;
                data[43] = ParameterToWrite.Pump1ActionIncDec;

                data[44] = ParameterToWrite.Pump2DegreeCelsius;
                data[45] = ParameterToWrite.Pump2SpeedPerRPMLByte;
                data[46] = ParameterToWrite.Pump2SpeedPerRPMHByte;
                data[47] = ParameterToWrite.Pump2ActionIncDec;

                data[48] = ParameterToWrite.Pump3DegreeCelsius;
                data[49] = ParameterToWrite.Pump3SpeedPerRPMLByte;
                data[50] = ParameterToWrite.Pump3SpeedPerRPMHByte;
                data[51] = ParameterToWrite.Pump3ActionIncDec;

                data[52] = ParameterToWrite.Pump4DegreeCelsius;
                data[53] = ParameterToWrite.Pump4SpeedPerRPMLByte;
                data[54] = ParameterToWrite.Pump4SpeedPerRPMHByte;
                data[55] = ParameterToWrite.Pump4ActionIncDec;

                //Prime Seeting Data
                data[56] = ParameterToWrite.Pump1PrimeSettingType;
                data[57] = ParameterToWrite.Pump1PrimeTimeDuration;
                data[58] = ParameterToWrite.Pump1PrimeSpeedPerRPMLByte;
                data[59] = ParameterToWrite.Pump1PrimeSpeedPerRPMHByte;

                data[60] = ParameterToWrite.Pump2PrimeSettingType;
                data[61] = ParameterToWrite.Pump2PrimeTimeDuration;
                data[62] = ParameterToWrite.Pump2PrimeSpeedPerRPMLByte;
                data[63] = ParameterToWrite.Pump2PrimeSpeedPerRPMHByte;

                data[64] = ParameterToWrite.Pump3PrimeSettingType;
                data[65] = ParameterToWrite.Pump3PrimeTimeDuration;
                data[66] = ParameterToWrite.Pump3PrimeSpeedPerRPMLByte;
                data[67] = ParameterToWrite.Pump3PrimeSpeedPerRPMHByte;

                data[68] = ParameterToWrite.Pump4PrimeSettingType;
                data[69] = ParameterToWrite.Pump4PrimeTimeDuration;
                data[70] = ParameterToWrite.Pump4PrimeSpeedPerRPMLByte;
                data[71] = ParameterToWrite.Pump4PrimeSpeedPerRPMHByte;

                data[72] = ParameterToWrite.Pump1CalibrationRequired;
                data[73] = ParameterToWrite.Pump2CalibrationRequired;
                data[74] = ParameterToWrite.Pump3CalibrationRequired;
                data[75] = ParameterToWrite.Pump4CalibrationRequired;

                data[76] = ParameterToWrite.PumpStoreValuestoSSTFlash;
                data[77] = ParameterToWrite.PumpVoltageSelection;


                master.WriteMultipleRegisters(slaveId, startAddress, data);

                return true;
            }
            catch (Exception ex)
            {
                log.Debug("Error in SendDataToBoard1", ex);

                return false;

            }
        }
    }

应用程序在遇到时挂起

  

master.WriteMultipleRegisters(slaveId,startAddress,data);

这是怎么了?以及如何实现Modbus数据传输?使用git中的NModbus库。

谢谢。

因此,通过添加以下两行,应用程序不再挂起,但我仍然无法在端口上发送数据。

            adapter.WriteTimeout = 2000;
            adapter.ReadTimeout = 1000;

0 个答案:

没有答案