Reopen port

12 views
Skip to first unread message

Efim Zabarsky

unread,
Nov 3, 2009, 2:12:05 AM11/3/09
to NModbus
Hello
Please, help me!
I use NModbus for C#
in timer i read slave and send it to UI
problem - when serial port disconnected - i can not renew the sending/
recieving of data from slave
How can i fix it?


my test code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FtdAdapter;
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
using System.IO.Ports;
using System.Threading;


namespace modbus1
{
public partial class Form1 : Form
{
ModbusSerialMaster master;
SerialPort port;

public delegate void SendDataDelegate(ushort[] reg);
void SendDataCallBack(ushort[] reg)
{
string str = "";
for (int i = 0; i < 20; i++)
{
str += reg[i].ToString() + " ";
}
label1.Text = str;
}

public Form1()
{
InitializeComponent();

port = new SerialPort("COM4");
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.ReadTimeout = 500;
port.WriteTimeout = 500;
port.Open();

// create modbus master
master = ModbusSerialMaster.CreateRtu(port);

System.Timers.Timer tm = new System.Timers.Timer(300);
tm.Enabled = true;
tm.Elapsed += new System.Timers.ElapsedEventHandler
(tm_Elapsed);
tm.Start();
}

void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
if (port.IsOpen == true)
{
try
{
ushort[] registers = master.ReadHoldingRegisters
(1, 1, 38);
if (label1.InvokeRequired == true)
{
SendDataDelegate sdd = new SendDataDelegate
(SendDataCallBack);
label1.Invoke(sdd, new Object[]
{ registers });
}
}
catch (Exception ex)
{
master = ModbusSerialMaster.CreateRtu(port);
}
}
}
}
}

Łukasz Specjał

unread,
Nov 5, 2009, 4:41:10 AM11/5/09
to nmodbus...@googlegroups.com
Hello Efim Zabarsky

I had the same problem as you occurred. To solved it I use Microsoft devcon tool. It will help you to manage yours devices connected to PC. It is work as Microsoft Device Manager. Take a look here:

http://support.microsoft.com/kb/311272

Some code sample how to use this tool:

devconPath declaration:

public string devconPath = Application.StartupPath.ToString();

You can set the path as you wish. The sample above will try to find devcon.exe in application run path. 

call method:

Process P = new Process();
ProcessStartInfo DevCon = new ProcessStartInfo(devconPath + "\\devcon\\devcon.exe", "restart =ports *Class_02*");
DevCon.WindowStyle = ProcessWindowStyle.Hidden;
P.StartInfo = DevCon;
P.StartInfo.RedirectStandardOutput = true;
P.StartInfo.UseShellExecute = false;
P.Start();
P.WaitForExit();

As you can see I use command restart=ports *Class_02* This will restart all devices with class_02. To find your device class you have to first run Microsoft Device Manager and find yours device properties. Check properties of device and you will find which class and sub class your device using. You can also do this by devcon.
--
Best Regards 

Lucas Specjal
Reply all
Reply to author
Forward
0 new messages