|
|
|
|
using System;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Scale
|
|
|
|
|
{
|
|
|
|
|
public partial class Form1 : Form
|
|
|
|
|
{
|
|
|
|
|
private SerialPort serialPort;
|
|
|
|
|
|
|
|
|
|
public Form1()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
lblTip.Text = "";
|
|
|
|
|
txtEID.KeyDown += TxtEID_KeyDown;
|
|
|
|
|
this.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
|
txtEID.Focus();
|
|
|
|
|
this.Load += Form1_Load;
|
|
|
|
|
this.FormClosing += Form_FormClosing;
|
|
|
|
|
|
|
|
|
|
SetFont();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetFont()
|
|
|
|
|
{
|
|
|
|
|
btnSend.Text = "\ue7a4 手动上报";
|
|
|
|
|
label3.Text = "\ue635 体检号:";
|
|
|
|
|
label4.Text = "\ue7ca 身高:";
|
|
|
|
|
label5.Text = "\ue696 体重:";
|
|
|
|
|
label8.Text = "\ue602 舒张压:";
|
|
|
|
|
label7.Text = "\ue601 收缩压:";
|
|
|
|
|
label9.Text = "\ue89a 脉搏:";
|
|
|
|
|
btnSend.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
btnSend.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label3.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label4.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label5.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label8.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label7.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
label9.Font = new Font(IconfontHelper.PFCC.Families[0], 18);
|
|
|
|
|
}
|
|
|
|
|
private void Form_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// 在窗口即将关闭时触发
|
|
|
|
|
if (MessageBox.Show(@"确定要关闭联机程序吗?", "确认关闭", MessageBoxButtons.YesNo) == DialogResult.No)
|
|
|
|
|
{
|
|
|
|
|
// 用户取消关闭操作
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (serialPort != null && serialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
serialPort.Close();
|
|
|
|
|
serialPort.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// 处理异常,例如记录日志或显示错误消息
|
|
|
|
|
MessageBox.Show($@"关闭串口时发生错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DelLog();
|
|
|
|
|
Conn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TxtEID_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode != Keys.Enter) return;
|
|
|
|
|
// 读取身高体重数据
|
|
|
|
|
ReadWeightHeight();
|
|
|
|
|
// Thread.Sleep(1000);
|
|
|
|
|
// 上传
|
|
|
|
|
Upload();
|
|
|
|
|
e.SuppressKeyPress = true; // 防止回车键产生额外的换行符
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnOpen_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Conn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnSend_Click(object sender, EventArgs ee)
|
|
|
|
|
{
|
|
|
|
|
Upload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TxtEidFocus()
|
|
|
|
|
{
|
|
|
|
|
Invoke(new Action(() => txtEID.Focus()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 血压仪
|
|
|
|
|
|
|
|
|
|
private void Conn()
|
|
|
|
|
{
|
|
|
|
|
if (serialPort != null && serialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var list = System.IO.Ports.SerialPort.GetPortNames();
|
|
|
|
|
if (serialPort != null && serialPort.IsOpen) serialPort.Close();
|
|
|
|
|
var port = txt1.Text?.Trim() ?? "COM1";
|
|
|
|
|
// if (list.Length<=0||!list.Contains(port))
|
|
|
|
|
// {
|
|
|
|
|
// MessageBox.Show(@"未找到串口:" + port);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// 初始化串口
|
|
|
|
|
serialPort = new SerialPort();
|
|
|
|
|
// 设置串口参数
|
|
|
|
|
serialPort.PortName = port; // 根据实际情况设置串口名称
|
|
|
|
|
serialPort.BaudRate = 9600; // 波特率
|
|
|
|
|
serialPort.DataBits = 8; // 数据位
|
|
|
|
|
serialPort.Parity = Parity.None; // 校验位
|
|
|
|
|
serialPort.StopBits = StopBits.One; // 停止位
|
|
|
|
|
// serialPort.RtsEnable = false;
|
|
|
|
|
// serialPort.DtrEnable = false;
|
|
|
|
|
serialPort.DataReceived += SerialPort_DataReceived;
|
|
|
|
|
// 设置数据接收事件处理程序
|
|
|
|
|
// 打开串口
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
serialPort.Open();
|
|
|
|
|
txt1.Enabled = false;
|
|
|
|
|
Debug.WriteLine("OPEN");
|
|
|
|
|
label1.Invoke(new Action(() => label1.Text = @"血压仪已连接"));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(@"无法打开串口:" + ex.Message);
|
|
|
|
|
txt1.Enabled = true;
|
|
|
|
|
// 关闭串口
|
|
|
|
|
if (serialPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
serialPort.Close();
|
|
|
|
|
}
|
|
|
|
|
label1.Invoke(new Action(() => label1.Text = @"血压仪未连接"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 血压仪数据接收
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 处理接收到的数据
|
|
|
|
|
var buffer = new byte[serialPort.BytesToRead];
|
|
|
|
|
serialPort.Read(buffer, 0, buffer.Length);
|
|
|
|
|
var data = Encoding.ASCII.GetString(buffer);
|
|
|
|
|
if (!(data.Length >= 51))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show($@"从仪器接收到错误的数据:{data}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
TxtEidFocus();
|
|
|
|
|
var txtSsyText = data.Substring(40, 3);
|
|
|
|
|
Debug.WriteLine(txtSsyText);
|
|
|
|
|
txtSsy.Invoke(new Action(() => txtSsy.Text = txtSsyText));
|
|
|
|
|
|
|
|
|
|
var txtSzyText = data.Substring(44, 3);
|
|
|
|
|
Debug.WriteLine(txtSzyText);
|
|
|
|
|
txtSzy.Invoke(new Action(() => txtSzy.Text = txtSzyText));
|
|
|
|
|
|
|
|
|
|
var txtMbText = data.Substring(48, 3);
|
|
|
|
|
Debug.WriteLine(txtMbText);
|
|
|
|
|
txtMb.Invoke(new Action(() => txtMb.Text = txtMbText));
|
|
|
|
|
|
|
|
|
|
var weight = txtWeight.Text?.Trim();
|
|
|
|
|
if (string.IsNullOrEmpty(weight))
|
|
|
|
|
{
|
|
|
|
|
ReadWeightHeight();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(@"错误:" + exception.Message);
|
|
|
|
|
// 关闭串口
|
|
|
|
|
if (serialPort.IsOpen) serialPort.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 身高体重仪
|
|
|
|
|
|
|
|
|
|
public void ReadWeightHeight()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 获取当前程序路径
|
|
|
|
|
var currentPath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
// 组合路径,假设 Data 文件夹在当前路径下
|
|
|
|
|
var dataFolderPath = Path.Combine(currentPath, "data");
|
|
|
|
|
// 获取 Data 文件夹下的所有 .log 文件,并按名称排序
|
|
|
|
|
var files = Directory.GetFiles(dataFolderPath, "*.log").OrderBy(f => f).ToArray();
|
|
|
|
|
|
|
|
|
|
if (files.Length <= 0) return;
|
|
|
|
|
// 读取第一个文本文件的内容
|
|
|
|
|
var firstFilePath = files[0];
|
|
|
|
|
var content = File.ReadAllText(firstFilePath);
|
|
|
|
|
Debug.WriteLine(firstFilePath);
|
|
|
|
|
Debug.WriteLine(content);
|
|
|
|
|
var match = Regex.Match(content, @"W:(\d+\.\d+)\s+H:(\d+\.\d+)");
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
// 提取身高和体重的值
|
|
|
|
|
var weight = double.Parse(match.Groups[1].Value).ToString("0.##");
|
|
|
|
|
var height = double.Parse(match.Groups[2].Value).ToString("0.##");
|
|
|
|
|
Invoke(new Action(() => txtWeight.Text = weight));
|
|
|
|
|
Invoke(new Action(() => txtHeight.Text = height));
|
|
|
|
|
}
|
|
|
|
|
// 删除所有文本文件
|
|
|
|
|
foreach (var filePath in files)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
Debug.WriteLine($"Deleted: {Path.GetFileName(filePath)}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DelLog()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// 获取当前程序路径
|
|
|
|
|
var currentPath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
// 组合路径,假设 Data 文件夹在当前路径下
|
|
|
|
|
var dataFolderPath = Path.Combine(currentPath, "data");
|
|
|
|
|
// 获取 Data 文件夹下的所有 .log 文件,并按名称排序
|
|
|
|
|
var files = Directory.GetFiles(dataFolderPath, "*.log").OrderBy(f => f).ToArray();
|
|
|
|
|
// 删除所有文本文件
|
|
|
|
|
foreach (var filePath in files)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 上传数据库
|
|
|
|
|
|
|
|
|
|
public void Upload()
|
|
|
|
|
{
|
|
|
|
|
var eid = txtEID.Text?.Trim();
|
|
|
|
|
var weight = txtWeight.Text?.Trim();
|
|
|
|
|
var height = txtHeight.Text?.Trim();
|
|
|
|
|
var systolic = txtSsy.Text?.Trim();
|
|
|
|
|
var diastolic = txtSzy.Text?.Trim();
|
|
|
|
|
var pulseRate = txtMb.Text?.Trim();
|
|
|
|
|
if (string.IsNullOrEmpty(eid)) return;
|
|
|
|
|
if (string.IsNullOrEmpty(weight) &&
|
|
|
|
|
string.IsNullOrEmpty(height) &&
|
|
|
|
|
string.IsNullOrEmpty(systolic) &&
|
|
|
|
|
string.IsNullOrEmpty(diastolic) &&
|
|
|
|
|
string.IsNullOrEmpty(pulseRate)) return;
|
|
|
|
|
var isEid = Int64.TryParse(eid, out var id);
|
|
|
|
|
if (!isEid)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(@"体检号错误,请重新输入!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var result = Insert(id, weight, height, systolic, diastolic, pulseRate);
|
|
|
|
|
|
|
|
|
|
lblTip.Text = result ? $"提示:体检号 {eid},数据上报成功" : $"提示:体检号 {eid},数据上报失败";
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
Invoke(new Action(() => txtEID.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtWeight.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtHeight.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtSsy.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtSzy.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtMb.Text = ""));
|
|
|
|
|
}
|
|
|
|
|
TxtEidFocus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string ConnectionString = @"Data Source=192.168.11.5;Initial Catalog=peisdb;User ID=sa;Password=000626;";
|
|
|
|
|
|
|
|
|
|
public static bool Insert(long eid, string weight, string height, string systolic, string diastolic, string pulseRate)
|
|
|
|
|
{
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Open();
|
|
|
|
|
var insertQuery = $@"
|
|
|
|
|
IF NOT EXISTS (
|
|
|
|
|
SELECT 1 FROM Exam_ResultIsm WHERE EID = {eid} AND Weight = '{weight}' AND Height = '{height}' AND Systolic = '{systolic}' AND Diastolic = '{diastolic}' AND PulseRate = '{pulseRate}'
|
|
|
|
|
)
|
|
|
|
|
BEGIN
|
|
|
|
|
INSERT INTO Exam_ResultIsm(EID, Weight, Height, Systolic, Diastolic, PulseRate)
|
|
|
|
|
VALUES ({eid}, '{weight}', '{height}', '{systolic}', '{diastolic}', '{pulseRate}')
|
|
|
|
|
END";
|
|
|
|
|
using (var command = new SqlCommand(insertQuery, connection))
|
|
|
|
|
{
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
connection.Close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(e.Message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Invoke(new Action(() => txtEID.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtWeight.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtHeight.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtSsy.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtSzy.Text = ""));
|
|
|
|
|
Invoke(new Action(() => txtMb.Text = ""));
|
|
|
|
|
|
|
|
|
|
DelLog();
|
|
|
|
|
txtEID.Focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|