454 lines
19 KiB
C#
454 lines
19 KiB
C#
|
|
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 System.IO;
|
|||
|
|
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Threading;
|
|||
|
|
|
|||
|
|
namespace SOH.OnLine
|
|||
|
|
{
|
|||
|
|
public partial class OnLine : Form
|
|||
|
|
{
|
|||
|
|
bool _xybc2900 = false;
|
|||
|
|
|
|||
|
|
public OnLine()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
if (MessageBox.Show("你确定要退出仪器通信程序吗?退出后将无法及时收到仪器的检查结果!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
|
|||
|
|
{
|
|||
|
|
if (listenSocket != null)
|
|||
|
|
DestroySocket(listenSocket);
|
|||
|
|
System.Environment.Exit(System.Environment.ExitCode);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnLine_FormClosing(object sender, FormClosingEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (e.CloseReason == CloseReason.UserClosing)
|
|||
|
|
{
|
|||
|
|
e.Cancel = true;
|
|||
|
|
this.Hide();
|
|||
|
|
sp.Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
private static Encoding encode = Encoding.Default;
|
|||
|
|
|
|||
|
|
const string asb = "\x000b";
|
|||
|
|
const string aeb = "\x001c";
|
|||
|
|
const string acr = "\x000d";
|
|||
|
|
const string enq = "\x0005";
|
|||
|
|
const string ack = "\x0006";
|
|||
|
|
const string stx = "\x0002";
|
|||
|
|
const string eot = "\x0004";
|
|||
|
|
const string lf = "\x000a";
|
|||
|
|
|
|||
|
|
Socket listenSocket = null;
|
|||
|
|
private void OnLine_Load(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
//FileStream fs = new FileStream(Application.StartupPath + "\\lis.txt", FileMode.Open);
|
|||
|
|
//byte[] bs = new byte[fs.Length];
|
|||
|
|
//fs.Read(bs, 0, bs.Length);
|
|||
|
|
//fs.Close();
|
|||
|
|
//byte hh = bs[0];
|
|||
|
|
//for (int i = 1; i < bs.Length; i++)
|
|||
|
|
//{
|
|||
|
|
// hh += bs[i];
|
|||
|
|
//}
|
|||
|
|
//string ssss = hh.ToString("X2");
|
|||
|
|
/*
|
|||
|
|
* <add key="PortType" value="NET"/>
|
|||
|
|
<add key="netaddstr" value="127.0.0.1"/>
|
|||
|
|
<add key="netaddstrport" value="5500"/>
|
|||
|
|
*/
|
|||
|
|
System.Configuration.Configuration conf = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.StartupPath + "\\local.ext");
|
|||
|
|
string PortType = conf.AppSettings.Settings["PortType"].Value;
|
|||
|
|
string yqbm = conf.AppSettings.Settings["yqbm"].Value;
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(PortType))
|
|||
|
|
{
|
|||
|
|
PortType = "COM";
|
|||
|
|
}
|
|||
|
|
if (PortType == "NET")
|
|||
|
|
{
|
|||
|
|
string lisfile1 = Application.StartupPath + "\\result";
|
|||
|
|
if (!Directory.Exists(lisfile1))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(lisfile1);
|
|||
|
|
}
|
|||
|
|
lisfile1 += "\\lis.txt";
|
|||
|
|
if (File.Exists(lisfile1))
|
|||
|
|
{
|
|||
|
|
File.Delete(lisfile1);
|
|||
|
|
}
|
|||
|
|
string netaddstr = conf.AppSettings.Settings["netaddstr"].Value;
|
|||
|
|
string netaddstrport = conf.AppSettings.Settings["netaddstrport"].Value;
|
|||
|
|
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
listenSocket.Bind(new IPEndPoint(IPAddress.Any, int.Parse(netaddstrport)));
|
|||
|
|
listenSocket.Listen(100);
|
|||
|
|
//Console.WriteLine("Listen " + port + " ...");
|
|||
|
|
new Thread(() =>
|
|||
|
|
{
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
Socket acceptSocket = listenSocket.Accept();
|
|||
|
|
string recStr = "";
|
|||
|
|
string receiveData = "";
|
|||
|
|
if (yqbm == "HT800")
|
|||
|
|
{
|
|||
|
|
//处理解码回HT800数据
|
|||
|
|
new Thread(() =>
|
|||
|
|
{
|
|||
|
|
Socket writeSocket = acceptSocket;
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
Thread.Sleep(100);
|
|||
|
|
if (File.Exists(lisfile1))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
StreamReader sr = new StreamReader(lisfile1, System.Text.Encoding.Default);
|
|||
|
|
string data = sr.ReadToEnd();
|
|||
|
|
sr.Close();
|
|||
|
|
if (data.StartsWith(enq) && data.EndsWith(eot))
|
|||
|
|
{
|
|||
|
|
File.Delete(lisfile1);
|
|||
|
|
string[] ss = data.Split(new string[] { "$#$" }, StringSplitOptions.None);
|
|||
|
|
foreach (string s in ss)
|
|||
|
|
{
|
|||
|
|
writeSocket.Send(System.Text.Encoding.Default.GetBytes(s));
|
|||
|
|
Thread.Sleep(100);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch { }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}).Start();
|
|||
|
|
new Thread(() =>
|
|||
|
|
{
|
|||
|
|
Socket readSocket = acceptSocket;
|
|||
|
|
bool recenq = false;
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
receiveData = Receive(readSocket, 50000); //5 seconds timeout.
|
|||
|
|
|
|||
|
|
if (receiveData == enq)
|
|||
|
|
{
|
|||
|
|
readSocket.Send(encode.GetBytes(ack));
|
|||
|
|
recStr = enq;
|
|||
|
|
recenq = true;
|
|||
|
|
}
|
|||
|
|
else if (receiveData == eot)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
if (recenq)
|
|||
|
|
{
|
|||
|
|
recStr += eot;
|
|||
|
|
//DestroySocket(acceptSocket); //import
|
|||
|
|
string path = Application.StartupPath + "\\raw";
|
|||
|
|
string datapath = path + "\\data.txt";
|
|||
|
|
string rawpath = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
|
|||
|
|
FileStream fsraw = new FileStream(rawpath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
StreamWriter rawwriter = new StreamWriter(fsraw);
|
|||
|
|
FileStream data = new FileStream(datapath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
StreamWriter datawriter = new StreamWriter(data);
|
|||
|
|
rawwriter.Write(recStr);
|
|||
|
|
rawwriter.Close();
|
|||
|
|
datawriter.Write(recStr);
|
|||
|
|
datawriter.Close();
|
|||
|
|
}
|
|||
|
|
recenq = false;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else if (receiveData.StartsWith(stx) && receiveData.EndsWith(acr + lf))
|
|||
|
|
{
|
|||
|
|
recStr += receiveData;
|
|||
|
|
acceptSocket.Send(encode.GetBytes(ack));
|
|||
|
|
}
|
|||
|
|
else if (receiveData == ack)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
acceptSocket.Send(encode.GetBytes(ack));
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}).Start();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (receiveData.StartsWith(asb) && receiveData.EndsWith(aeb + acr))
|
|||
|
|
{
|
|||
|
|
string path = Application.StartupPath + "\\raw";
|
|||
|
|
if (!Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(path);
|
|||
|
|
}
|
|||
|
|
string datapath = path + "\\data.txt";
|
|||
|
|
string rawpath = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
|
|||
|
|
FileStream fsraw = new FileStream(rawpath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
StreamWriter rawwriter = new StreamWriter(fsraw);
|
|||
|
|
FileStream data = new FileStream(datapath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
StreamWriter datawriter = new StreamWriter(data);
|
|||
|
|
rawwriter.Write(receiveData);
|
|||
|
|
rawwriter.Close();
|
|||
|
|
datawriter.Write(receiveData);
|
|||
|
|
datawriter.Close();
|
|||
|
|
receiveData = receiveData.Replace(asb, "");
|
|||
|
|
receiveData = receiveData.Replace(aeb + acr, "");
|
|||
|
|
string[] datas = receiveData.Split('\x001d');
|
|||
|
|
string msgid = "";
|
|||
|
|
string msgtype = "";
|
|||
|
|
foreach (string d in datas)
|
|||
|
|
{
|
|||
|
|
string[] zs = d.Split('|');
|
|||
|
|
if (zs[0] == "MSH")
|
|||
|
|
{
|
|||
|
|
msgtype = zs[8];
|
|||
|
|
if (msgtype == "ORU^R01")
|
|||
|
|
{
|
|||
|
|
msgid = zs[9];
|
|||
|
|
}
|
|||
|
|
else if (msgtype == "QRY^Q02")
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (msgtype == "ORU^R01")
|
|||
|
|
{
|
|||
|
|
string returnstr = "";
|
|||
|
|
returnstr = asb + "MSH|^~\\&|||SanCaiNet|SohLis|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "||ACK^R01|" + msgid + "|P|2.3.1||||0||ASCII|||" + acr +
|
|||
|
|
"MSA|AA|" + msgid + "|Message accepted|||0|" + acr +
|
|||
|
|
aeb + acr;
|
|||
|
|
acceptSocket.Send(encode.GetBytes(returnstr));
|
|||
|
|
}
|
|||
|
|
else if (msgtype == "QRY^Q02")
|
|||
|
|
{
|
|||
|
|
int c = 40;
|
|||
|
|
int h = 0;
|
|||
|
|
while (c > 0)
|
|||
|
|
{
|
|||
|
|
Thread.Sleep(100);
|
|||
|
|
string lisfile = Application.StartupPath + "\\result";
|
|||
|
|
if (!Directory.Exists(lisfile))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(lisfile);
|
|||
|
|
}
|
|||
|
|
lisfile += "\\lis.txt";
|
|||
|
|
if (File.Exists(lisfile))
|
|||
|
|
{
|
|||
|
|
StreamReader sw = new StreamReader(lisfile, Encoding.Default);
|
|||
|
|
string text = sw.ReadToEnd();
|
|||
|
|
sw.Close();
|
|||
|
|
acceptSocket.Send(encode.GetBytes(text));
|
|||
|
|
File.Delete(lisfile);
|
|||
|
|
h++;
|
|||
|
|
if (h == 2)
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
c--;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}//Console.WriteLine("Receive:" + receiveData);
|
|||
|
|
|
|||
|
|
DestroySocket(acceptSocket); //import
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}).Start();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
string BaundRate = conf.AppSettings.Settings["BaundRate"].Value;
|
|||
|
|
string DataBits = conf.AppSettings.Settings["DataBits"].Value;
|
|||
|
|
string Parity = conf.AppSettings.Settings["Parity"].Value;
|
|||
|
|
string PortName = conf.AppSettings.Settings["PortName"].Value;
|
|||
|
|
string StopBits = conf.AppSettings.Settings["StopBits"].Value;
|
|||
|
|
nf.Text = yqbm + nf.Text;
|
|||
|
|
if (conf.AppSettings.Settings.AllKeys.Contains("XYBC2900"))
|
|||
|
|
{
|
|||
|
|
string z = conf.AppSettings.Settings["XYBC2900"].Value;
|
|||
|
|
if (z == "1")
|
|||
|
|
{
|
|||
|
|
_xybc2900 = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
sp.BaudRate = int.Parse(BaundRate);
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("仪器通信参数没有设置,请联系管理员设置!");
|
|||
|
|
}
|
|||
|
|
sp.DataBits = int.Parse(DataBits);
|
|||
|
|
sp.PortName = PortName;
|
|||
|
|
switch (StopBits)
|
|||
|
|
{
|
|||
|
|
case "None":
|
|||
|
|
sp.StopBits = System.IO.Ports.StopBits.None;
|
|||
|
|
break;
|
|||
|
|
case "One":
|
|||
|
|
sp.StopBits = System.IO.Ports.StopBits.One;
|
|||
|
|
break;
|
|||
|
|
case "OnePointFive":
|
|||
|
|
sp.StopBits = System.IO.Ports.StopBits.OnePointFive;
|
|||
|
|
break;
|
|||
|
|
case "Two":
|
|||
|
|
sp.StopBits = System.IO.Ports.StopBits.Two;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
switch (Parity)
|
|||
|
|
{
|
|||
|
|
case "Even":
|
|||
|
|
sp.Parity = System.IO.Ports.Parity.Even;
|
|||
|
|
break;
|
|||
|
|
case "Mark":
|
|||
|
|
sp.Parity = System.IO.Ports.Parity.Mark;
|
|||
|
|
break;
|
|||
|
|
case "None":
|
|||
|
|
sp.Parity = System.IO.Ports.Parity.None;
|
|||
|
|
break;
|
|||
|
|
case "Odd":
|
|||
|
|
sp.Parity = System.IO.Ports.Parity.Odd;
|
|||
|
|
break;
|
|||
|
|
case "Space":
|
|||
|
|
sp.Parity = System.IO.Ports.Parity.Space;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
sp.Open();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("打开串口" + PortName + "失败" + ex.Message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 发送数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="host"></param>
|
|||
|
|
/// <param name="port"></param>
|
|||
|
|
/// <param name="data"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string Send(string host, int port, string data)
|
|||
|
|
{
|
|||
|
|
string result = string.Empty;
|
|||
|
|
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
clientSocket.Connect(host, port);
|
|||
|
|
clientSocket.Send(encode.GetBytes(data));
|
|||
|
|
Console.WriteLine("Send:" + data);
|
|||
|
|
result = Receive(clientSocket, 5000 * 2); //5*2 seconds timeout.
|
|||
|
|
Console.WriteLine("Receive:" + result);
|
|||
|
|
DestroySocket(clientSocket);
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 接收数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="socket"></param>
|
|||
|
|
/// <param name="timeout"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private static string Receive(Socket socket, int timeout)
|
|||
|
|
{
|
|||
|
|
string result = string.Empty;
|
|||
|
|
socket.ReceiveTimeout = timeout;
|
|||
|
|
List<byte> data = new List<byte>();
|
|||
|
|
byte[] buffer = new byte[1024];
|
|||
|
|
int length = 0;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
while ((length = socket.Receive(buffer)) > 0)
|
|||
|
|
{
|
|||
|
|
for (int j = 0; j < length; j++)
|
|||
|
|
{
|
|||
|
|
data.Add(buffer[j]);
|
|||
|
|
}
|
|||
|
|
if (length < buffer.Length)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch { }
|
|||
|
|
if (data.Count > 0)
|
|||
|
|
{
|
|||
|
|
result = encode.GetString(data.ToArray(), 0, data.Count);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 销毁Socket对象
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="socket"></param>
|
|||
|
|
private static void DestroySocket(Socket socket)
|
|||
|
|
{
|
|||
|
|
if (socket.Connected)
|
|||
|
|
{
|
|||
|
|
socket.Shutdown(SocketShutdown.Both);
|
|||
|
|
}
|
|||
|
|
socket.Close();
|
|||
|
|
}
|
|||
|
|
private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
|||
|
|
{
|
|||
|
|
int readcount = sp.BytesToRead;
|
|||
|
|
string path = Application.StartupPath + "\\raw";
|
|||
|
|
if (!Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(path);
|
|||
|
|
}
|
|||
|
|
string datapath = path + "\\data.txt";
|
|||
|
|
string rawpath = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
|
|||
|
|
FileStream fsraw = new FileStream(rawpath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
BinaryWriter rawwriter = new BinaryWriter(fsraw);
|
|||
|
|
FileStream data = new FileStream(datapath, FileMode.Append, FileAccess.Write, FileShare.None);
|
|||
|
|
BinaryWriter datawriter = new BinaryWriter(data);
|
|||
|
|
while (readcount > 0)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
byte[] bs = new byte[readcount];
|
|||
|
|
|
|||
|
|
sp.Read(bs, 0, readcount);
|
|||
|
|
if (_xybc2900)
|
|||
|
|
{
|
|||
|
|
if (bs[0] == 0x10)
|
|||
|
|
{
|
|||
|
|
byte[] b = new byte[1];
|
|||
|
|
b[0] = 0x06;
|
|||
|
|
sp.Write(b, 0, 1);
|
|||
|
|
}
|
|||
|
|
if (bs[bs.Length - 1] == 0x0f)
|
|||
|
|
{
|
|||
|
|
byte[] b = new byte[1];
|
|||
|
|
b[0] = 0x06;
|
|||
|
|
sp.Write(b, 0, 1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
datawriter.Write(bs);
|
|||
|
|
rawwriter.Write(bs);
|
|||
|
|
readcount = sp.BytesToRead;
|
|||
|
|
}
|
|||
|
|
rawwriter.Close();
|
|||
|
|
datawriter.Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|