using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Data;
namespace COMSpectators
{
class Program
{
static Timer mainTimer;
static DataSet ds3;
static DataTable dt3;
static List<OneCOMSpectator> allListeners;
static void Main(string[] args)
{
InitAll();
NpgsqlConnection conn = new NpgsqlConnection(("Server=" + list[0] + ";Port=" + list[1] + ";User Id=" + list[2] + ";Password=" + list[3] + ";Database=" + list[4] + ";")); //
string sql = " SELECT \"ID_TagName\", \"ID_RealHardWare\", \"F_ParseFunction\", \"F_ComQuery\", \"F_AnswerLenght\", \"F_AnswerKey\", \"F_AnswerKeyPosition\", \"F_ComPort\", \"F_ComPortIPAdress\", \"F_ComPortBaudRate\", \"F_ComPortParity\", \"F_ComPortDataBits\", \"F_ComPortStopBit\", \"F_ComPortFlowControl\", \"F_ComPortTimeOut\" , \"ID_HardWareTag\", \"F_OPCServerName\", \"F_IDOPCTag\", \"F_TagReadTime\", \"Transmit\", \"F_RealHardWare_ID\" FROM \"SC_Tag\".\"T_TagName\" as tn,\"SC_Tag\".\"T_HardWareTag\" as hw , \"SC_Tag\".\"T_RealHardWare\" as rh where rh.\"ID_RealHardWare\" = hw.\"F_RealHardWare_ID\" and tn.\"F_HardWare_ID\" = hw.\"F_TagName_ID\" and \"F_ServerName\" =\'" + Environment.MachineName + "\' and \"F_ComPortIPAdress\" = \'127.0.0.1\' ";
// string sql = " SELECT \"F_name\", \"F_OPCServerName\", \"F_IDOPCTag\", \"F_TagReadTime\", \"ID_TagName\" FROM \"SC_Tag\".\"T_TagName\" as tn, \"SC_Tag\".\"T_HardWareTag\" as hw where tn.\"F_HardWare_ID\" = hw.\"ID_HardWareTag\" and \"F_ServerName\" =\'" + Environment.MachineName + "\' ";
NpgsqlDataAdapter da3 = new NpgsqlDataAdapter(sql, conn);
ds3.Reset();
da3.Fill(ds3);
dt3 = ds3.Tables[0];
dataGridView3.DataSource = dt3;
int mainInterval = 1000;//Convert.ToInt32(dt3.Rows[0][14]);
mainTimer = new Timer();
mainTimer.Interval = mainInterval;
mainTimer.Enabled = true;
mainTimer.Tick+=new EventHandler(mainTimer_Tick);
}
static void mainTimer_Tick(object sender, EventArgs e)
{
foreach(OneCOMSpectator comListener in allListeners)
{
comListener.TransferData();
}
}
private static void InitAll()
{
allListeners = new List<OneCOMSpectator>();
for(int xx = 0; xx < ds3.Tables[0].Rows.Count; ++xx)
{
string com = "COM" + dt3.Rows[xx][7];
string send = dt3.Rows[xx][3].ToString() + "\r";
allListeners.Add(new OneCOMSpectator(com, 9600, send));
}
}
}
class OneCOMSpectator
{
SerialPort _COM;
string DataToSend;
internal OneCOMSpectator(string portNumber, int bodRate, string dataTosend)
{
DataToSend = dataTosend;
_COM = new SerialPort(portNumber, bodRate, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
_COM.DataReceived += new SerialDataReceivedEventHandler(_COM_DataReceived);
}
void _COM_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Пришли данные
}
internal void TransferData()
{
if(_COM.IsOpen)
{
// Если порт открыт, значит какой-то косяк, девайс не ответил, здесь обрабатываем
// Закрываем
_COM.Close();
}
// Открываем
_COM.Open();
_COM.Write(DataToSend);
}
}
}
но вот незадача - устройства на втором ком порте вот думаю как проверить
|