User Control
User Control
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Linq;
namespace LISA
{
public partial class UserControlAnalyzer : UserControl
{
public DataRow dtCentral;
private SerialPort ObjSPAccess;
private LISAINTERFACE_SERIAL LisaInterfaceComm_Serial = null;
private LISAINTERFACE_NETWORK LisaInterfaceComm_Network = null;
private LISAINTERFACE_NETWORK_CLIENT LISACLIENTTCPCommInterface = null;
private LISAINTERFACE_FILEBASE LISAFILEBASECOMMINTERFACE = null;
private StreamWriter sw;
public TcpListener tcpListener;
public Thread listenThread;
public static TcpClient client;
private IPEndPoint fetchedEP;
private TcpClient tcpClient;
public ASCIIEncoding encoder;
public string ReceivedString = null;
public string TCPInstance;
public static List<SerialPort> sPorts = new List<SerialPort>();
private Socket comm_client_conn;
private Socket client_conn;
private IPEndPoint fetched_endpoint;
private byte[] comm_bytemsg;
private bool check = false;
private IPAddress hostIPAddress;
public int Ports;
if (ports.Contains(comport))
{
richTextBox_analyzerString.Text += "Channelizing !!!!";
ObjSPAccess = new SerialPort();
ObjSPAccess.PortName =
dtCentral["portnumber"].ToString().Trim();
ObjSPAccess.BaudRate =
Convert.ToInt32(dtCentral["baudrate"].ToString().Trim());
ObjSPAccess.DataBits =
Convert.ToInt32(dtCentral["databits"].ToString().Trim());
ObjSPAccess.Parity = (Parity)Enum.Parse(typeof(Parity),
dtCentral["parity"].ToString().Trim());
ObjSPAccess.StopBits =
(StopBits)Enum.Parse(typeof(StopBits), dtCentral["stopbits"].ToString().Trim());
ObjSPAccess.Handshake =
(Handshake)Enum.Parse(typeof(Handshake), dtCentral["handshake"].ToString().Trim());
if (dtCentral["dtr_rts"].ToString().Trim() == "Y")
{
ObjSPAccess.DtrEnable = true; //
ObjSPAccess.RtsEnable = true; //
}
if (ObjSPAccess.IsOpen)
{
ObjSPAccess.Close();
}
ObjSPAccess.Open();
richTextBox_analyzerString.Text += Environment.NewLine
+ "Communication Started !!!!";
ObjSPAccess.DataReceived += sPort_DataReceived;
string Class_Name = "LISA." +
dtCentral["device_name"].ToString().Trim() + ",LISA";
this.LisaInterfaceComm_Serial =
(LISAINTERFACE_SERIAL)Activator.CreateInstance(System.Type.GetType(Class_Name,
true), (object)dtCentral["deviceid"].ToString().Trim());
label_machinename.Text =
dtCentral["device_name"].ToString().Trim();
label_port.Text =
dtCentral["protocol"].ToString().Trim() + " (" +
dtCentral["portnumber"].ToString().Trim() + ")";
label1.Text = "COMPORT:";
pictureBox_status.Image = Properties.Resources.correct;
sPorts.Add(ObjSPAccess);
}
else
{
MessageBox.Show("Port Not Available");
}
}
else if (dtCentral["protocol"].ToString().ToUpper() == "NETWORK")
{
if (dtCentral["mode"].ToString().ToUpper() == "SERVER")
{
IPAddress localaddr =
IPAddress.Parse(dtCentral["hostip"].ToString());
int iPort =
Int32.Parse(dtCentral["portnumber"].ToString());
string instance_name = "LISA." +
dtCentral["device_name"].ToString().Trim() + ",LISA";
TCPInstance = instance_name;
if (localaddr != null)
{
tcpListener = new TcpListener(localaddr, iPort);
listenThread = new Thread(AttendClientsComm);
listenThread.Start();
}
}
else if (dtCentral["mode"].ToString().ToUpper() == "CLIENT")
{
string instance_name = "LISA." +
dtCentral["device_name"].ToString().Trim() + ",LISA";
TCPInstance = instance_name;
Ports = Int32.Parse(dtCentral["portnumber"].ToString());
hostIPAddress =
IPAddress.Parse(dtCentral["deviceip"].ToString());
fetched_endpoint = new IPEndPoint(hostIPAddress, Ports);
label_machinename.Text =
dtCentral["device_name"].ToString().Trim();
label_port.Text = dtCentral["protocol"].ToString().Trim() + "
(" + dtCentral["portnumber"].ToString().Trim() + ")";
label1.Text = "PORT NUMBER:";
pictureBox_status.Image = Properties.Resources.correct;
}
else if (dtCentral["protocol"].ToString().ToUpper() == "FILEBASED")
{
try
{
string Class_Name = "LISA." +
dtCentral["device_name"].ToString().Trim() + ",LISA";
this.LISAFILEBASECOMMINTERFACE =
(LISAINTERFACE_FILEBASE)Activator.CreateInstance(System.Type.GetType(Class_Name,
true), (object)dtCentral["deviceid"].ToString().Trim(),
(object)dtCentral["filepath"].ToString().Trim());
label_machinename.Text =
dtCentral["device_name"].ToString().Trim();
label_port.Text = dtCentral["protocol"].ToString().Trim();
label1.Text = "PROTOCOL:";
pictureBox_status.Image = Properties.Resources.correct;
}
catch (Exception ex)
{
sw = File.AppendText(Application.StartupPath + "\\
Instance_FILEBASE.txt");
sw.WriteLine(ex.Message);
sw.Close();
}
}
}
catch (Exception ex)
{
pictureBox_status.Image = Properties.Resources.incorrect;
sw = File.AppendText(Application.StartupPath + "\\
Comm_Port_load.log");
sw.WriteLine(DateTime.Now.ToString() + ex.Message);
sw.Close();
}
//TCP CLIENT
}
catch (Exception ex)
{
pictureBox_status.Image = Properties.Resources.incorrect;
sw = File.AppendText(Application.StartupPath + "\\
TCPClient_Load.log");
sw.WriteLine(DateTime.Now.ToString() + ex.Message);
sw.Close();
}
}
}
}
}
}
public void Recieve(IAsyncResult iar)
{
try
{
Socket client_conn = (Socket)iar.AsyncState;
client_conn.EndReceive(iar);
if (comm_bytemsg.Length != 0)
{
string Data3 = null;
Data3 = Encoding.ASCII.GetString(comm_bytemsg, 0,
comm_bytemsg.Length);
Data3 = Data3.Replace("\0", "").Trim();
if (Data3.Trim() == "")
{
check = false;
}
else
{
AppendTextBox(Data3);
RecieveDataCLIENTCOMMTCP(TCPInstance, Data3, client_conn);
comm_client_conn.BeginReceive(comm_bytemsg, 0,
comm_bytemsg.Length, SocketFlags.None, Recieve, comm_client_conn);
}
}
}
catch (SocketException)
{
}
}
if (richTextBox_analyzerString.Text.Trim() == string.Empty)
{
richTextBox_analyzerString.Text += value;
}
else
{
richTextBox_analyzerString.Text += Environment.NewLine +
value;
}
});
//SERIAL PORT
richTextBox_analyzerString.Invoke((Action)delegate
{
if (richTextBox_analyzerString.Text.Trim() == string.Empty)
{
richTextBox_analyzerString.Text += oBytes;
}
else
{
richTextBox_analyzerString.Text += Environment.NewLine +
oBytes;
}
});
LisaInterfaceComm_Serial.ParseData_Serial(oBytes, ObjSPAccess);
}
catch (Exception ex)
{
pictureBox_status.Image = Properties.Resources.incorrect;
sw = File.AppendText(Application.StartupPath + "\\
Data_Recieved_Serial.log");
sw.WriteLine(DateTime.Now.ToString() + ex.Message);
sw.Close();
}
}
//TCP SERVER
});
tcpClient.Close();
}
richTextBox_analyzerString.Invoke((Action)delegate
{
if (richTextBox_analyzerString.Text.Trim() == string.Empty)
{
richTextBox_analyzerString.Text += ReceivedString;
}
else
{
richTextBox_analyzerString.Text += Environment.NewLine
+ ReceivedString;
}
});
}
tcpClient.Close();
if (!tcpClient.Connected)
{
richTextBox_analyzerString.Invoke((Action)delegate
{
richTextBox_analyzerString.Text += Environment.NewLine +
DateTime.Now + "Disconnected from Client IP Address :" + fetchedEP;
});
AttendClientsComm();
}
}
catch (Exception ex)
{
pictureBox_status.Image = Properties.Resources.incorrect;
sw = File.AppendText(Application.StartupPath + "\\Tc1p.txt");
sw.WriteLine(ex.Message);
sw.Close();
}
}
}
}
}