tijian_jichuang/Code/SOH.C14OnLine/frm_main.cs
2025-02-20 11:54:48 +08:00

187 lines
7.1 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.IO.Ports;
using System.Configuration;
using System.Threading;
using EAS.Services;
using SOH.BLL;
namespace SOH.C14OnLine
{
public partial class frm_main : Form
{
public frm_main()
{
InitializeComponent();
}
public class temp_item
{
public string id { get; set; }
public string text { get; set; }
}
SerialPort sp;
string port;
string stop;
string databits;
string BaudRate;
string parity;
private void Form1_Load(object sender, EventArgs e)
{
sp = new SerialPort();
//sp.BaudRate
List<temp_item> items = new List<temp_item>();
foreach (var p in System.IO.Ports.SerialPort.GetPortNames())
{
//cbb_port.DataSource = p;
items.Add(new temp_item { id = p, text = p });
}
cbb_port.DataSource = items;
string fileName = System.IO.Path.GetFileName(Application.ExecutablePath);
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileName);
port = config.AppSettings.Settings["port"].Value;
cbb_port.SelectedValue = port;
stop = config.AppSettings.Settings["stop"].Value;
xy_cbb_tzws.SelectedItem = stop;
databits = config.AppSettings.Settings["databits"].Value;
xy_txt_sjws.Text = databits;
parity = config.AppSettings.Settings["Parity"].Value;
xy_cbb_jyw.SelectedItem = parity;
BaudRate = config.AppSettings.Settings["BaudRate"].Value;
xy_txt_btl.Text = BaudRate;
sp.PortName = port;
sp.BaudRate = int.Parse(BaudRate);
sp.DataBits = int.Parse(databits);
switch (parity)
{
case "Even":
sp.Parity = Parity.Even;
break;
case "Mark":
sp.Parity = Parity.Mark;
break;
case "None":
sp.Parity = Parity.None;
break;
case "Odd":
sp.Parity = Parity.Odd;
break;
case "Space":
sp.Parity = Parity.Space;
break;
default:
sp.Parity = Parity.None;
break;
}
switch (stop)
{
case "One":
sp.StopBits = StopBits.One;
break;
case "OnePointFive":
sp.StopBits = StopBits.OnePointFive;
break;
case "Two":
sp.StopBits = StopBits.Two;
break;
default:
sp.StopBits = StopBits.One;
break;
}
sp.DataReceived += sp_DataReceived;
sp.Encoding = Encoding.GetEncoding("gb2312");
sp.Open();
}
string command = "";
string receiveMsg = "";
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string path = Application.StartupPath + "\\raw";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string datapath = path + "\\"+DateTime.Now.ToString("yyyyMMdd") + "_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);
//throw new NotImplementedException();
int readcount = sp.BytesToRead;
while (readcount > 0)
{
byte[] bs = new byte[readcount];
sp.Read(bs, 0, readcount);
datawriter.Write(bs);
rawwriter.Write(bs);
receiveMsg += Encoding.GetEncoding("gb2312").GetString(bs);
if(receiveMsg.IndexOf("*|*") >-1&&receiveMsg.IndexOf("<!>") >0)
{
command = receiveMsg.Substring(receiveMsg.IndexOf("*|*"), receiveMsg.IndexOf("<!>") );
new Thread(() => {
var vser = ServiceContainer.GetService<IC14>();
string[] args = command.Split('_');
if(args[1]=="01")
{
string code = args[2];
var gzb = vser.GetGzb(code);
if (gzb == null)
return;
string responseCode = $"*@*LIS_02_{code}_{gzb.xm}_{(gzb.xb==0?"":"")}_{gzb.nl}_{gzb.tcmc}_NULL_C14_{gzb.tm}_崔万金<!>";
//sp.Encoding = System.Text.Encoding.GetEncoding("gb2312");
sp.Write(responseCode);
}
//
else if(args[2]=="03")
{
vser.uploadC14(int.Parse(args[3]), DateTime.Parse(args[7]), args[4], args[5], args[8]);
string responseCode = "*@*LIS_04<!>";
sp.Write(responseCode);
}
}).Start();
receiveMsg = receiveMsg.Substring(receiveMsg.IndexOf("<!>") + 3);
}
readcount = sp.BytesToRead;
}
rawwriter.Close();
datawriter.Close();
}
private void btn_save_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(Application.ExecutablePath);
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileName);
config.AppSettings.Settings["port"].Value = cbb_port.SelectedValue.ToString();
config.AppSettings.Settings["stop"].Value = xy_cbb_tzws.SelectedItem.ToString();
config.AppSettings.Settings["databits"].Value = xy_txt_sjws.Text;
config.AppSettings.Settings["Parity"].Value = xy_cbb_jyw.SelectedItem.ToString();
config.AppSettings.Settings["BaudRate"].Value = xy_txt_btl.Text;
//sp.PortName = port;
//sp.BaudRate = int.Parse(BaudRate);
//sp.DataBits = int.Parse(databits);
config.Save();
if (sp.IsOpen)
{
sp.Close();
sp.Dispose();
sp = null;
}
Form1_Load(this, EventArgs.Empty);
}
}
}