static void Main(string[] args) { myProtocol = new MbusRtuOverTcpMasterProtocol(); try { retryCnt = int.Parse(""); } catch (Exception) { retryCnt = 0; } try { pollDelay = int.Parse(""); } catch (Exception) { pollDelay = 0; } try { timeOut = int.Parse("1000"); } catch (Exception) { timeOut = 1000; } try { tcpPort = int.Parse("502"); } catch (Exception) { tcpPort = 502; } myProtocol.timeout = timeOut; myProtocol.retryCnt = retryCnt; myProtocol.pollDelay = pollDelay; ((MbusRtuOverTcpMasterProtocol)myProtocol).port = (short)tcpPort; res = ((MbusRtuOverTcpMasterProtocol)myProtocol).openProtocol("192.168.5.178"); if ((res == BusProtocolErrors.FTALK_SUCCESS)) { //lblResult.Text = ("Modbus/TCP port opened successfully with parameters: " + (txtHostName.Text + (", TCP port " + tcpPort))); Console.WriteLine("Modbus/TCP port opened successfully with parameters: " + ("192.168.5.178" + (", TCP port " + tcpPort))); Console.ReadLine(); ReadHoldingRegisters(); } else { //lblResult.Text = ("Could not open protocol, error was: " + BusProtocolErrors.getBusProtocolErrorText(res)); Console.WriteLine("Could not open protocol, error was: " + BusProtocolErrors.getBusProtocolErrorText(res)); } }
public bool Connect(ProtocolTypeEnum protocolType, BaseCommConfig commCfg, out string errorStr) { errorStr = ""; TCPCommConfig ipCfg = commCfg as TCPCommConfig; if (protocol != null) { if (slaveIP == ipCfg.addressMajor) { return(true); } else { errorStr = "Connected to a different device"; return(false); } } try { if (protocolType == ProtocolTypeEnum.TCP) { protocol = new MbusTcpMasterProtocol(); } else { protocol = new MbusUdpMasterProtocol(); } } catch (OutOfMemoryException ex) { return(false); } slaveAddr = ipCfg.devId; protocol.timeout = ipCfg.timeout; protocol.retryCnt = ipCfg.retryCnt; protocol.pollDelay = ipCfg.pollDelay; protocol.setPort((short)ipCfg.addressMinor); int res = protocol.openProtocol(ipCfg.addressMajor); if ((res == BusProtocolErrors.FTALK_SUCCESS)) { slaveIP = ipCfg.addressMajor; errorStr = string.Format("Modbus/TCP Device: {0} Port: {1} opened successfully", ipCfg.addressMajor, ipCfg.addressMinor); return(true); } else { errorStr = ("Could not open protocol, error was: " + BusProtocolErrors.getBusProtocolErrorText(res)); protocol = null; return(false); } }
public bool WriteRegisters(int start, int count, short[] values) { if (protocol == null || !protocol.isOpen()) { return(false); } int res = protocol.writeMultipleRegisters(slaveAddr, start, values, count); if (res != BusProtocolErrors.FTALK_SUCCESS) { string str = BusProtocolErrors.getBusProtocolErrorText(res); System.Diagnostics.Debug.WriteLine($"WriteRegisters => {res}/{str}"); } return(res == BusProtocolErrors.FTALK_SUCCESS); }
public bool WriteRegister(int regNum, short value) { if (protocol == null || !protocol.isOpen()) { return(false); } int res = protocol.writeSingleRegister(slaveAddr, regNum, value); if (res != BusProtocolErrors.FTALK_SUCCESS) { string str = BusProtocolErrors.getBusProtocolErrorText(res); System.Diagnostics.Debug.WriteLine($"WriteRegister => {res}/{str}"); } return(res == BusProtocolErrors.FTALK_SUCCESS); }
public bool ReadRegisters(int start, int count, int[] values) { if (protocol == null || !protocol.isOpen()) { return(false); } sw.Start(); int res = protocol.readInputRegisters(slaveAddr, start, values, count); sw.Stop(); readCount++; if (res != BusProtocolErrors.FTALK_SUCCESS) { string str = BusProtocolErrors.getBusProtocolErrorText(res); System.Diagnostics.Debug.WriteLine($"ReadInputRegisters => {res}/{str}"); } return(res == BusProtocolErrors.FTALK_SUCCESS); }
public bool Connect(BaseCommConfig commCfg, out string errorStr) { errorStr = ""; SerialCommConfig serCfg = commCfg as SerialCommConfig; if (protocol != null) { if (commPort == serCfg.addressMajor) { return(true); } else { errorStr = "Connected to a different device"; return(false); } } try { protocol = new MbusRtuMasterProtocol(); } catch (OutOfMemoryException ex) { return(false); } slaveAddr = serCfg.devId; protocol.timeout = serCfg.timeout; protocol.retryCnt = serCfg.retryCnt; protocol.pollDelay = serCfg.pollDelay; int res = protocol.openProtocol( serCfg.addressMajor, serCfg.addressMinor, (int)serCfg.dataBits, (int)serCfg.stopBits, (int)serCfg.parity); if ((res == BusProtocolErrors.FTALK_SUCCESS)) { commPort = serCfg.addressMajor; errorStr = $"Modbus/TCP Device: {serCfg.addressMajor} Baud: {serCfg.addressMinor} opened successfully"; return(true); } else { errorStr = $"Could not open protocol, error was: {BusProtocolErrors.getBusProtocolErrorText(res)}"; protocol = null; return(false); } }
static void ReadHoldingRegisters() { short[] writeVals = new short[125]; short[] readVals = new short[125]; int slave; int startWrReg; int numWrRegs; int startRdReg; int numRdRegs; int i; int res; int startCoil; int numCoils; bool[] coilVals = new bool[2000]; try { try { slave = int.Parse("1"); } catch (Exception) { slave = 1; } try { startCoil = int.Parse(""); } catch (Exception) { startCoil = 1; } try { numCoils = int.Parse("5"); } catch (Exception) { numCoils = 1; } try { startRdReg = int.Parse("3204"); } catch (Exception) { startRdReg = 1; } try { startWrReg = int.Parse(""); } catch (Exception) { startWrReg = 1; } try { numWrRegs = int.Parse(""); } catch (Exception) { numWrRegs = 1; } try { numRdRegs = int.Parse("5"); } catch (Exception) { numRdRegs = 1; } try { writeVals[0] = Int16.Parse(null); writeVals[1] = Int16.Parse(null); writeVals[2] = Int16.Parse(null); writeVals[3] = Int16.Parse(null); writeVals[4] = Int16.Parse(null); writeVals[5] = Int16.Parse(null); writeVals[6] = Int16.Parse(null); writeVals[7] = Int16.Parse(null); coilVals[0] = (writeVals[0] != 0); coilVals[1] = (writeVals[1] != 0); coilVals[2] = (writeVals[2] != 0); coilVals[3] = (writeVals[3] != 0); coilVals[4] = (writeVals[4] != 0); coilVals[5] = (writeVals[5] != 0); coilVals[6] = (writeVals[6] != 0); coilVals[7] = (writeVals[7] != 0); } catch (Exception) { } res = myProtocol.readMultipleRegisters(slave, startRdReg, readVals, numRdRegs); //lblResult2.Text = ("Result: " + (BusProtocolErrors.getBusProtocolErrorText(res) + "\r\n")); string a = ("Result: " + (BusProtocolErrors.getBusProtocolErrorText(res) + "\r\n")); if ((res == BusProtocolErrors.FTALK_SUCCESS)) { //lblReadValues.Text = ""; for (i = 0; (i <= (numRdRegs - 1)); i++) { //lblReadValues.Text = (a + (readVals[i] + " ")); Console.WriteLine(a + readVals[i] + " "); } Console.ReadLine(); } } catch { } }