using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Drawing.Printing;
using ZXingDemo;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading;
namespace YD10测试机
{
public partial class Form1 : Form
{
SerialPort serialPort = new SerialPort();
public Form1()
{
InitializeComponent();
}
private float X; private float Y;
//获取控件的width、height、left、top、字体大小的值
//存放在控件的Tag属性中
private void setTag(Control cons)
{
//遍历窗体中的控件
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
setTag(con);
}
}
//根据窗体大小调整控件大小
private void setControls(float newx, float newy, Control cons)
{
//遍历窗体中的控件,重新设置控件的值
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//获取控件的Tag属性值,并分割后存储字符串数组
float a = Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值,宽度
con.Width = (int)a;//宽度
a = Convert.ToSingle(mytag[1]) * newy;//高度
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;//左边距离
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;//上边缘距离
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
//
void Form1_Resize(object sender, EventArgs e)
{
}
int flag_yd07close = 0;
public void ShowMessage(bool message1, bool message2, bool message3,string printname,string znum)
{
flag_yd07close = 1;
yd_ble = message1;
checkBox2.Checked = message2;
checkBox3.Checked = message3;
comboBox1.Text = printname;
comboBox2.Text = znum;
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "设备IMEI";
dataGridView2.Rows[index].Cells[1].Value = readConfig[2].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "设备ICCID";
dataGridView2.Rows[index].Cells[1].Value = readConfig[3].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产日";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[5].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产月份";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[6].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产年份";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[7].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "流水号";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[8].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "批次号";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[14].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "供应商编码";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[13].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "物料编码";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[12].threshold;
}
read_set();
if (che_kind == "YD07k")
{
if (serialPort.IsOpen)
{
_yd_disconnet();
serialPort.Close();
}//关闭串口
// 创建 Form3 的实例
Form3 form3 = new Form3(this);
// 隐藏 Form1
this.Hide();
// 显示 Form3
form3.ShowDialog();
return;
}
//timer1.Enabled = true;
//timer2.Enabled = true;
cmbBox_Port_Update();
my_set();
read();
GetPrinter();
// 添加第四列(按钮列)
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn
{
Name = "ButtonColumn", // 列的名称
HeaderText = "操作", // 列头显示的文本
Text = "写", // 按钮显示的文本
AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,
UseColumnTextForButtonValue = true, // 将按钮文本设置为列的值
DefaultCellStyle = new DataGridViewCellStyle
{
Font = new Font("宋体", 9), // 设置按钮字体为 Arial,大小为 12,加粗
}
};
dataGridView2.Columns.Add(buttonColumn);
// 注册按钮点击事件
dataGridView2.CellClick += DataGridView2_CellClick;
//set_ready();
ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, sslPolicyErrors) => true;
this.Resize += new EventHandler(Form1_Resize);//窗体调整大小时引发事件
X = this.Width;//获取窗体的宽度
Y = this.Height;//获取窗体的高度
setTag(this);//调用方法
}
string[] rsrp_ck,rsrq_ck;
string product,rssi,sinr;
int wd1, wd2,gnss;
private void read()
{
string strLoadConfigFilePath = Application.StartupPath + @"\YD10参考信息.json";//路径
try
{
using (StreamReader configFile = new StreamReader(strLoadConfigFilePath))
{
string str_data_json = configFile.ReadToEnd(); //这一步至关重要 差一点写错了
JObject object_data_prase_json = (JObject)JsonConvert.DeserializeObject(str_data_json);
if (object_data_prase_json != null)
{
product=(string)object_data_prase_json["打印名称设置"];
for (int i = 0; i < readConfig.Length; i++)
{
readConfig[i].Svalue = (string)object_data_prase_json[readConfig[i].name];
int ret_index = 0;
if (readConfig[i].name == "RSSI值")
{
ret_index = readConfig[i].Svalue.IndexOf("(雅迪");
rssi = readConfig[i].Svalue.Substring(0,ret_index);
}
if (readConfig[i].name == "SINR值")
{
ret_index = readConfig[i].Svalue.IndexOf("(雅迪");
sinr = readConfig[i].Svalue.Substring(0, ret_index);
}
if (readConfig[i].name == "RSRP值")
{
rsrp_ck = readConfig[i].Svalue.Split('~');
}
if (readConfig[i].name == "RSRQ值")
{
rsrq_ck = readConfig[i].Svalue.Split('~');
}
if (readConfig[i].name == "外电电压值")
{
wd1 = Convert.ToInt32(readConfig[i].Svalue.Substring(0, 2))-Convert.ToInt32(readConfig[i].Svalue.Substring(5,1));
wd2 = Convert.ToInt32(readConfig[i].Svalue.Substring(0, 2)) + Convert.ToInt32(readConfig[i].Svalue.Substring(5, 1));
}
if (readConfig[i].name == "GNSS SNR值")
{
gnss = Convert.ToInt32(readConfig[i].Svalue.Substring(0, 2));
}
}
for (int i = 0; i < writeConfig.Length; i++)
{
writeConfig[i].Svalue = (string)object_data_prase_json[writeConfig[i].name];
}
for (int i = 0; i < readConfig.Length; i++)
{
readConfig[i].status = (bool)object_data_prase_json[readConfig[i].name + "status"];
}
for (int i = 0; i < writeConfig.Length; i++)
{
writeConfig[i].status = (bool)object_data_prase_json[writeConfig[i].name + "status"];
}
}
}
}
catch (Exception)
{
}
}
bool yd_ble = false;
string che_kind = "";
private void read_set()
{
// string strLoadConfigFilePath = Application.StartupPath + @"\配置.json";//路径
string strLoadConfigFilePath = Path.Combine(Application.StartupPath, "配置.json");
try
{
using (StreamReader configFile = new StreamReader(strLoadConfigFilePath))
{
string str_data_json = configFile.ReadToEnd(); //这一步至关重要 差一点写错了
JObject object_data_prase_json = (JObject)JsonConvert.DeserializeObject(str_data_json);
if (object_data_prase_json != null)
{
che_kind= (string)object_data_prase_json["车型"];
comboBox1.Text = (string)object_data_prase_json["打印机"];
comboBox2.Text = (string)object_data_prase_json["张数"];
checkBox2.Checked = (bool)object_data_prase_json["自动打印机"];
yd_ble = (bool)object_data_prase_json["自动蓝牙连接"];
checkBox3.Checked = (bool)object_data_prase_json["号码校验"];
}
// configFile.Close(); // 显式关闭文件(可选,通常不需要)
}
}
catch (Exception ex)
{
MessageBox.Show($"发生错误: {ex.Message}");
}
}
private void cmbBox_Port_Update()
{
cmbPort.Items.Clear();
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
foreach (string sName in sSubKeys)
{
string sValue = (string)keyCom.GetValue(sName);
cmbPort.Items.Add(sValue);
}
}
}
////
//把十六进制格式的字符串转换成字节数组。
//
//要转换的十六进制格式的字符串
//返回字节数组。
public static byte[] getBytesFromString(string pString)
{
string[] str = pString.Split(' '); //把十六进制格式的字符串按空格转换为字符串数组。
byte[] bytes = new byte[str.Length]; //定义字节数组并初始化,长度为字符串数组的长度。
for (int i = 0; i < str.Length; i++) //遍历字符串数组,把每个字符串转换成字节类型赋值给每个字节变量。
bytes[i] = Convert.ToByte(Convert.ToInt32(str[i], 16));
return bytes; //返回字节数组。
}
//把字节数组转为十六进制字符串
public static string getStringFromBytes(byte[] pByte)
{
string str = ""; //定义字符串类型临时变量。
//遍历字节数组,把每个字节转换成十六进制字符串,不足两位前面添“0”,以空格分隔累加到字符串变量里。
for (int i = 0; i < pByte.Length; i++)
str += (pByte[i].ToString("X").PadLeft(2, '0') + " ");
str = str.TrimEnd(' '); //去掉字符串末尾的空格。
return str; //返回字符串临时变量。
}
// /16进制转字符串
static string ushortToHexString(ushort value)
{
byte[] bytes = BitConverter.GetBytes(value); // 将ushort转换为字节数组
//if (BitConverter.IsLittleEndian) // 检查系统是否为小端字节序
//{
// Array.Reverse(bytes); // 如果是小端字节序,需要反转字节数组
//}
// 将每个字节转换为两位的十六进制字符串,并拼接
string hexString = string.Join(" ", bytes.Select(b => b.ToString("X2")));
return hexString;
}
// CRC16-CCITT (0xFFFF)算法实现
public static ushort ComputeChecksum(byte[] data)
{
ushort crc = 0xFFFF; // 初始化 CRC 为 0xFFFF
foreach (byte b in data)
{
crc ^= b; // 确保 b 是一个整数
for (int i = 0; i < 8; i++)
{
if ((crc & 0x0001) != 0)
{
crc = (ushort)((crc >> 1) ^ 0xA001); // 0xA001 是 0x8005 按位颠倒后的结果
}
else
{
crc = (ushort)(crc >> 1);
}
}
}
return crc;
}
// 将十六进制字符串转换为CRC16校验码的字符串格式("XX XX")
public static string crc16(string hexString)
{
// 将十六进制字符串转换为字节数组
byte[] data = getBytesFromString(hexString);
// 计算CRC16校验码
ushort crc = ComputeChecksum(data);
// 将校验码转换为字符串格式("XX XX")
return $"{(crc >> 8):X2} {(crc & 0xFF):X2}";
}
// 将字符串中的每个字符转换为十六进制字符串
public static string StringToHex(string str)
{
string hexString = "";
foreach (char c in str)
{
hexString += ((int)c).ToString("X2") + " "; // 每个字符转换为两位的十六进制字符串,并用空格分隔
}
return hexString.Trim(); // 去掉尾部多余的空格
}
static string Int32ToTwoHexBytes(int value)
{
// 提取低8位和高8位
byte low8 = (byte)(value & 0xFF); // 低8位
byte high8 = (byte)((value >> 8) & 0xFF); // 高8位
// 转换为两位的十六进制字符串
string lowHex = low8.ToString("X2"); // 低8位的十六进制字符串
string highHex = high8.ToString("X2"); // 高8位的十六进制字符串
return $"{lowHex} {highHex}";
}
public static int ConvertToAsciiHex(string input)
{
// 将输入的字符串转换为 int32 类型的数值
int intValue = int.Parse(input);
// 将 int32 类型的数值转换为十六进制表示(字符串形式)
string hexString = intValue.ToString("X");
// 获取十六进制表示的第一个字符(假设只关心第一个字符)
char hexChar = hexString[0];
// 返回该字符的 ASCII 码值
return (int)hexChar;
}
private void send(string data)
{
if (serialPort.IsOpen)
{
byte[] temdata1 = getBytesFromString(data);
serialPort.Write(temdata1, 0, temdata1.Length); //发到串口
}
else
{
txtStatus.Text = "请先打开串口!!";
}
//this.Invoke(new Action(() =>
//{
// label2.Text = "send:" +data;
//}));
}
int flag_readn = 0;
//建立连接
private void _yd_connet()
{
string head = "59 44 ";
string data = "07 02 00 10 03";
string end = head + data + " " + crc16(data) + " 4B 4A";
send(end);
}
//断开连接
private void _yd_disconnet()
{
string head = "59 44 ";
string data = "07 02 00 10 00";
string end = head + data + " " + crc16(data) + " 4B 4A";
send(end);
}
//心跳包
private void yd_heart()
{
string head = "59 44 ";
string data = "07 02 00 3E 00" ;
string end = head + data + " " + crc16(data) + " 4B 4A";
send(end);
}
//交互指令
private void yd_rid_opt(UInt16 cmd)
{
string head = "59 44 ";
string data = "07 04 00 31 00 " + ushortToHexString(cmd);
string end = head + data + " " + crc16(data) + " 4B 4A";
send(end);
}
//读
private void yd_did_read_one(UInt16 cmd)
{
string head = "59 44 ";
string data = "07 03 00 22 " + ushortToHexString(cmd);
string end = head + data + " " + crc16(data) + " 4B 4A";
send(end);
}
//写
private void yd_did_write_one(UInt16 cmd, string data)
{
if (checkBox3.Checked)
{
return;
}
string cmd_data = "";
if (cmd == 0xcf40)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 99)
{
cmd_data = Convert.ToInt32(data).ToString("X2");
}
else
{
}
}
if (cmd == 0xcf41)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 254)
{
cmd_data = Convert.ToInt32(data).ToString("X2");
}
else
{
}
}
if (cmd == 0xcf42)
{
if (data == "IOT")
{
cmd_data = StringToHex("E");
}
if (data == "BCM")
{
cmd_data = StringToHex("D");
}
else
{
}
}
if (cmd == 0xcf43)
{
if (data == "嘉为")
{
cmd_data = StringToHex("2");
}
if (data == "高兴新")
{
cmd_data = StringToHex("6");
}
if (data == "如新")
{
cmd_data = StringToHex("9");
}
}
if (cmd == 0xcf44)
{
if (data == "无锡雅迪")
{
cmd_data = StringToHex("1");
}
if (data == "天津雅迪")
{
cmd_data = StringToHex("2");
}
if (data == "浙江雅迪")
{
cmd_data = StringToHex("3");
}
if (data == "广州雅迪")
{
cmd_data = StringToHex("4");
}
if (data == "成都雅迪")
{
cmd_data = StringToHex("5");
}
if (data == "越南雅迪")
{
cmd_data = StringToHex("6");
}
}
if (cmd == 0xcf45)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 31)
{
cmd_data = StringToHex(data.Substring(0,1));
yd_did_write_one(0xcf46, data.Substring(1,1));
}
}
if (cmd == 0xcf46)
{
cmd_data = StringToHex(data);
}
if (cmd == 0xcf47)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 12)
{
cmd_data = ConvertToAsciiHex(data).ToString("X");
}
}
if (cmd == 0xcf48 )
{
if (Convert.ToInt32(data.Substring(0, 1)) >= 0 && Convert.ToInt32(data.Substring(0, 1)) <= 9)
{
cmd_data = StringToHex(data.Substring(0, 1));
yd_did_write_one(0xcf49, data.Substring(1, 1));
}
}
if (cmd == 0xcf49)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 9)
{
cmd_data = StringToHex(data);
}
}
if (cmd == 0xcf4a)
{
if (Convert.ToInt32(data) >= 0 && Convert.ToInt32(data) <= 999999)
{
cmd_data = StringToHex(data)+" 00";
}
}
if (cmd == 0xcf4b)
{
if (data.Length<65) {
cmd_data = StringToHex(data) + " 00";
}
}
if (cmd == 0xcf4c)
{
if (data.Length < 65535)
{
cmd_data = Int32ToTwoHexBytes(Convert.ToInt32(data));
}
}
if (cmd == 0xcf4d)
{
string hexString = Convert.ToInt32(data).ToString("X8");
cmd_data = $"{hexString.Substring(6, 2)} {hexString.Substring(4, 2)} {hexString.Substring(2, 2)} {hexString.Substring(0, 2)}";
}
if (cmd == 0xcf4e)
{
if (data.Length < 17)
{
cmd_data = StringToHex(data) + " 00";
}
}
if (cmd == 0xcf4f)
{
if (data.Length < 7)
{
cmd_data = StringToHex(data) + " 00";
}
}
if (cmd == 0xcf50)
{
if (data.Length < 10)
{
cmd_data = StringToHex(data) + " 00 00 00";
}
}
string head = "59 44 ";
string data2 = "07 "+ ((cmd_data.Length+1)/3+3).ToString("X2") + " 00 2E " + ushortToHexString(cmd)+" " + cmd_data;
string end = head + data2 + " " + crc16(data2) + " 4B 4A";
send(end);
}
//自动测试
private void zd_test(int i)
{
if (this.Text.IndexOf("YD10") != -1) {
if (i < 26)
{
ushort did = Convert.ToUInt16(readConfig[i].DID, 16);
yd_did_read_one(did);
}
else if (i > 25)
{
ushort did = Convert.ToUInt16(writeConfig[i - 26].DID, 16);
if (did == 0xcf46)
{
yd_did_read_one(0xcf45);
}
if (did == 0xcf49)
{
yd_did_read_one(0xcf48);
}
yd_did_read_one(did);
}
}
else if (this.Text.IndexOf("YD07") != -1)
{
}
}
private void btnOpen_usb_Click(object sender, EventArgs e)
{
if (btnOpen_usb.Text == "打开")
{
try
{
if (!serialPort.IsOpen)
{
serialPort.PortName = cmbPort.Text;
serialPort.BaudRate = 115200; //波特率
serialPort.DataBits = 8; //数据位
serialPort.StopBits = StopBits.One; //停止位
serialPort.Parity = Parity.None; //奇偶校验
serialPort.Open();//打开串口
}
btnOpen_usb.Text = "关闭";
txtStatus.Text = "串口已打开!";
serialPort.DataReceived += new SerialDataReceivedEventHandler(PortReceivedHandle); //加载接收事件
}
catch
{
txtStatus.Text = "串口打开错误或串口不存在!";
}
}
else //关闭串口
try
{
if (serialPort.IsOpen)
{
serialPort.Close(); //关闭串口
btnOpen_usb.Text = "打开";
txtStatus.Text = "串口已关闭!";
flag_heart = 0;
out_time = -1;
_yd_disconnet();
flag_connet = 0;
clean();
//button1.Text = "开始检测";
}
}
catch
{
txtStatus.Text = "串口关闭错误或串口不存在!";
}
}
// 将十六进制字符串转换为整数
public static int HexStringToInt(string hexString)
{
// 移除字符串中的所有空格
string cleanedHexString = hexString.Replace(" ", "");
// 将十六进制字符串转换为整数
int result = Convert.ToInt32(cleanedHexString, 16);
return result;
}
private void PortReceivedHandle(object sender, SerialDataReceivedEventArgs e)
{
try
{
//第一部分 串口接收
SerialPort serialport = (SerialPort)(sender);
System.Threading.Thread.Sleep(200);
int n = serialport.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
//writeA883SmtLog(smt_log_path,"n = "+ Convert.ToString(n), 0, 2);
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
serialport.Read(buf, 0, n);//读取缓冲数据
serialport.DiscardInBuffer(); //清空接收中断buffer
//str_result = Encoding.UTF8.GetString(buf);
//byte[] str16_result = getBytesFromString(str_result);
string str_result = getStringFromBytes(buf);
int ret_index = -1;
//if (buf[0]==0x59 && buf[1]==0x44 && buf[2]==0x0f) //找到头
//{
// int data_len = buf[4] << 8 | buf[3];
// byte[] data= buf.Skip(2 + 1 + 2).Take(data_len).ToArray();
// int crc = buf[2 + 1 + 2 + data_len] << 8 | buf[2 + 1 + 2 + data_len + 1];
// byte[] end = buf.Skip(2 + 1 + 2 + data_len + 2).Take(data_len + 2 + 2).ToArray();
// // 校验CRC
// byte[] tmp = buf.Skip(2).Take(data_len+1+2).ToArray();
// int tmp_crc16 = HexStringToInt(crc16(getStringFromBytes(tmp)));
// if (crc == tmp_crc16)
// {
// if (end[0] == 0x4b && end[1] == 0x4a)
// {
// recvAnalz(data,data_len);
// }
// }
//}
int index = 0; // 当前处理的索引
while (index < buf.Length)
{
// 检查头部
if (index + 5 < buf.Length && buf[index] == 0x59 && buf[index + 1] == 0x44 && buf[index + 2] == 0x0f)
{
// 提取数据长度
int data_len = buf[index + 4] << 8 | buf[index + 3];
// 提取数据部分
byte[] data = buf.Skip(index + 2 + 1 + 2).Take(data_len).ToArray();
// 提取CRC部分
int crc = buf[index + 2 + 1 + 2 + data_len] << 8 | buf[index + 2 + 1 + 2 + data_len + 1];
// 提取结束标志部分
byte[] end = buf.Skip(index + 2 + 1 + 2 + data_len + 2).Take(2).ToArray();
// 提取用于CRC校验的数据
byte[] tmp = buf.Skip(index + 2).Take(data_len + 1 + 2).ToArray();
// 计算CRC
int tmp_crc16 = HexStringToInt(crc16(getStringFromBytes(tmp)));
// 校验CRC
if (crc == tmp_crc16)
{
// 校验结束标志
if (end.Length == 2 && end[0] == 0x4b && end[1] == 0x4a)
{
// 处理有效数据
Heart = true;
recvAnalz(data, data_len);
//this.Invoke(new Action(() =>
//{
// label2.Text = str_result ;
//}));
}
}
}
// 移动到下一组数据
index++;
}
}
catch (Exception)
{
}
}
int flag_gnss = 0,acc_bl=1000;
string year = "", day = "";
string[] numbers;
//响应数据分析
private void recvAnalz(byte[] data, int data_len)
{
ushort cmd = data[0];
Heart = true;
switch (cmd)
{
case 0x7f://错误码
if (data[1] == 0x10)
{
if (data[2] == 0x12)
{
this.Invoke(new Action(() =>
{
label2.Text = "连接/断开:不受支持的子功能";
}));
}
if (data[2] == 0x13)
{
this.Invoke(new Action(() =>
{
label2.Text = "连接/断开:消息长度或格式不正确";
}));
}
}
if (data[1] == 0x3E)
{
if (data[2] == 0x12)
{
this.Invoke(new Action(() =>
{
label2.Text = "心跳:不受支持的子功能";
}));
}
if (data[2] == 0x13)
{
this.Invoke(new Action(() =>
{
label2.Text = "心跳:消息长度或格式不正确";
}));
}
}
if (data[1] == 0x22)
{
if (data[2] == 0x13)
{
this.Invoke(new Action(() =>
{
label2.Text = "读:消息长度或格式不正确";
}));
}
}
if (data[1] == 0x2E)
{
if (data[2] == 0x13)
{
this.Invoke(new Action(() =>
{
label2.Text = "写:消息长度或格式不正确";
}));
}
if (data[2] == 0x31)
{
this.Invoke(new Action(() =>
{
label2.Text = "写:不支持的DID或DID为只读DID";
}));
}
if (data[2] == 0x72)
{
this.Invoke(new Action(() =>
{
label2.Text = "写:写出错";
}));
}
}
if (data[1] == 0x31)
{
if (data[2] == 0x13)
{
this.Invoke(new Action(() =>
{
label2.Text = "交互:消息长度或格式不正确";
}));
}
if (data[2] == 0x31)
{
this.Invoke(new Action(() =>
{
label2.Text = "交互:不支持的DID或DID为只读DID";
}));
}
if (data[2] == 0x72)
{
this.Invoke(new Action(() =>
{
label2.Text = "交互:写出错";
}));
}
}
break;
case 0x50://建立连接
if (data[1] == 0x03)
{
this.Invoke(new Action(() =>
{
label2.Text = "连接成功";
}));
yd_heart();
flag_connet = 0;
flag_result = 0;
flag_heart = 1;
}
if (data[1] == 0x00)//断开连接
{
}
break;
case 0x7e://心跳报文
if (data[1] == 0x00)
{
//Heart = true;
}
break;
case 0x62://读响应
ushort did = (ushort)((data[2] << 8) | data[1]);
string DID = ((data[2] << 8) | data[1]).ToString("X4");
// 查找特定 value 对应的 name
int index = Array.FindIndex(readConfig, myUnitClass => myUnitClass.DID == DID);
//数字
if ((did >= 0xCF81) && (did <= 0xCF8C) || (did == 0xCF08) || (did == 0xCF09) || (did == 0xCF0C))
{
if (did == 0xCF85)
{
readConfig[index].threshold = string.Join(", ", data.Skip(3).Select(s8 => (s8 > 127) ? s8 - 256 : s8));
// 使用 Split 方法按逗号拆分字符串
numbers = readConfig[index].threshold.Split(',');
flag_gnss = 1;
}
else if ((did == 0xCF86) || (did == 0xCF87))
{
readConfig[index].threshold = ((data[3] > 127) ? data[3] - 256 : data[3]).ToString() + "dbm";
readConfig[index].Ivalue = Convert.ToInt32(((data[3] > 127) ? data[3] - 256 : data[3]).ToString());
}
else if ((did == 0xCF88) || (did == 0xCF89))
{
readConfig[index].threshold = ((data[3] > 127) ? data[3] - 256 : data[3]).ToString() + "db";
readConfig[index].Ivalue = Convert.ToInt32(((data[3] > 127) ? data[3] - 256 : data[3]).ToString());
}
else if (did == 0xCF0C)
{
readConfig[index].threshold =Convert.ToString((data[4] << 8) | data[3]);
}
else
{
readConfig[index].threshold = Convert.ToString(data[3]);
readConfig[index].Ivalue = Convert.ToInt32(readConfig[index].threshold);
}
}
// 显示字符串
if (((did >= 0xCF00) && (did <= 0xCF06)) || (did == 0xCF0A) || (did == 0xCF0B) || (did == 0xCF80) && data[3]!=0xff)
{
readConfig[index].threshold = string.Join("", data.Skip(3).Select(b => (char)b)).Replace("\0", "");
}
// BCD 字符串
if (did == 0xCF07 && data[3] != 0xff)
{
readConfig[index].threshold = string.Concat(data.Skip(3).Select(b => (char)b)).Replace("\0", "");
}
//读写分组
int index2 = Array.FindIndex(writeConfig, myUnitClass => myUnitClass.DID == DID);
// 数字1
if ((did == 0xCF40) || (did == 0xCF41))
{
writeConfig[index2].threshold = Convert.ToString(data[3]).PadLeft(2, '0');
}
if (did == 0xCF42)
{
if (data[3] == 0xff)
{
writeConfig[index2].threshold = "0xFF";
}
else
{
char character = (char)data[3];
if (character == 'E')
{
writeConfig[index2].threshold = "IOT";
}
if (character == 'D')
{
writeConfig[index2].threshold = "BCM";
}
}
}
if (did == 0xCF43)
{
if (data[2] == 0xff)
{
writeConfig[index2].threshold = "0xFF";
}
else
{
char character = (char)data[3];
if (character == '2')
{
writeConfig[index2].threshold = "嘉为";
}
if (character == '6')
{
writeConfig[index2].threshold = "高新兴";
}
if (character == '9')
{
writeConfig[index2].threshold = "如新";
}
}
}
if (did == 0xCF44)
{
if (data[2] == 0xff)
{
writeConfig[index2].threshold = "0xFF";
}
else
{
char character = (char)data[3];
writeConfig[index2].threshold = character + ".";
if (character == '1')
{
writeConfig[index2].threshold += "无锡";
}
if (character == '2')
{
writeConfig[index2].threshold += "天津";
}
if (character == '3')
{
writeConfig[index2].threshold += "浙江";
}
if (character == '4')
{
writeConfig[index2].threshold += "广州";
}
if (character == '5')
{
writeConfig[index2].threshold += "成都";
}
if (character == '6')
{
writeConfig[index2].threshold += "越南";
}
writeConfig[index2].threshold += "雅迪";
}
}
if ((did == 0xCF45))
{
day = Convert.ToString((char)data[3]);
//writeConfig[index2].threshold = Convert.ToString((char)data[3]);
}
if (did == 0xCF47)
{
string years =Convert.ToString( (char)data[3]);
string yeadrss = Convert.ToInt32(years,16).ToString("D2");
writeConfig[index2].threshold = yeadrss;
}
if ((did == 0xCF48))
{
year = Convert.ToString((char)data[3]);
}
if ((did == 0xCF4A) || (did == 0xCF4B) || (did == 0xCF4E) || (did == 0xCF4F) || (did == 0xCF50) )
{
writeConfig[index2].threshold = string.Join("", data.Skip(3).Select(b => (char)b)).Replace("\0", "");
}
// 数字2
if (did == 0xCF4C)
{
writeConfig[index2].threshold = Convert.ToString((data[4] << 8) | data[3]);
}
// 数字4
if (did == 0xCF4D)
{
writeConfig[index2].threshold = Convert.ToString((data[6] << 24 | data[5] << 16 | data[4] << 8 | data[3]));
if (Convert.ToInt32(writeConfig[index2].threshold)>0)
{
acc_bl = Convert.ToInt32(writeConfig[index2].threshold);
}
}
if ((did == 0xCF46))
{
writeConfig[index2].threshold = day + Convert.ToString((char)data[3]);
yd_did_read_one(0xcf45);
}
if ((did == 0xCF49))
{
writeConfig[index2].threshold = year+ Convert.ToString((char)data[3]);
yd_did_read_one(0xcf48);
}
break;
case 0x6e://写响应
ushort did2 = (ushort)((data[2] << 8) | data[1]);
//string DID2 = ((data[2] << 8) | data[1]).ToString("X4");
//int index3= Array.FindIndex(writeConfig, myUnitClass => myUnitClass.DID ==DID2);
//writeConfig[index3].threshold = "";
yd_did_read_one(did2);
this.Invoke(new Action(() =>
{
label2.Text = "写入成功";
}));
flag_write = 0;
break;
case 0x71://交互指令
if (data[1] == 0x00)
{
}
break;
}
}
struct myUnitClass
{
public string DID; //DID
public string name; //名称
public string threshold;
public bool status;
public Int64 Ivalue;
public bool Bvalue;
public string Svalue;
public byte[] Fvalue;
public string sw; //是pass 还是fail , 用于pass排在上上方
};
myUnitClass[] readConfig = new myUnitClass[26];
myUnitClass[] writeConfig = new myUnitClass[15];
myUnitClass[] alm_readConfig = new myUnitClass[23];
myUnitClass[] alm_writeConfig = new myUnitClass[5];
private void my_set()
{
//------只读1次-----
readConfig[0].DID = "CF00";
readConfig[0].name = "串口诊断协议版本号";
readConfig[0].status = true;
readConfig[1].DID = "CF01";
readConfig[1].name = "软件版本号";
readConfig[1].status = true;
readConfig[2].DID = "CF02";
readConfig[2].name = "设备IMEI";
readConfig[2].status = true;
readConfig[3].DID = "CF03";
readConfig[3].name = "设备ICCID";
readConfig[3].status = true;
readConfig[4].DID = "CF04";
readConfig[4].name = "GNSS产品型号";
readConfig[4].status = true;
readConfig[5].DID = "CF05";
readConfig[5].name = "GNSS软件版本号";
readConfig[5].status = true;
readConfig[6].DID = "CF06";
readConfig[6].name = "GNSS硬件版本号";
readConfig[6].status = true;
readConfig[7].DID = "CF07";
readConfig[7].name = "SPI FLASH ID";
readConfig[7].status = true;
readConfig[8].DID = "CF08";
readConfig[8].name = "BOOT底层软件版本";
readConfig[8].status = true;
readConfig[9].DID = "CF09";
readConfig[9].name = "BOOT升级协议版本";
readConfig[9].status = true;
readConfig[10].DID = "CF0A";
readConfig[10].name = "通信协议版本号";
readConfig[10].status = true;
readConfig[11].DID = "CF0B";
readConfig[11].name = "备份区软件版本号";
readConfig[11].status = true;
readConfig[12].DID = "CF0C";
readConfig[12].name = "OTA零部件编码";
readConfig[12].status = true;
//------只读N次-----
readConfig[13].DID = "CF80";
readConfig[13].name = "OTA域名";
readConfig[13].status = true;
readConfig[14].DID = "CF81";
readConfig[14].name = "拨号状态";
readConfig[14].status = true;
readConfig[15].DID = "CF82";
readConfig[15].name = "驻网状态";
readConfig[15].status = true;
readConfig[16].DID = "CF83";
readConfig[16].name = "服务器连接状态";
readConfig[16].status = true;
readConfig[17].DID = "CF84";
readConfig[17].name = "GNSS定位状态";
readConfig[17].status = true;
readConfig[18].DID = "CF85";
readConfig[18].name = "GNSS SNR值";
readConfig[18].status = true;
readConfig[19].DID = "CF86";
readConfig[19].name = "RSSI值";
readConfig[19].status = true;
readConfig[20].DID = "CF87";
readConfig[20].name = "RSRP值";
readConfig[20].status = true;
readConfig[21].DID = "CF88";
readConfig[21].name = "RSRQ值";
readConfig[21].status = true;
readConfig[22].DID = "CF89";
readConfig[22].name = "SINR值";
readConfig[22].status = true;
readConfig[23].DID = "CF8A";
readConfig[23].name = "ACC管脚电平状态";
readConfig[23].status = true;
readConfig[24].DID = "CF8B";
readConfig[24].name = "外电电压值";
readConfig[24].status = true;
readConfig[25].DID = "CF8C";
readConfig[25].name = "K线通讯状态";
readConfig[25].status = true;
////新增YD07K
///
//readConfig[26].DID = "CF0D";
//readConfig[26].name = "GSENSOR地址";
//readConfig[26].status = true;
//readConfig[27].DID = "CF8D";
//readConfig[27].name = "六轴数据校准值";
//readConfig[27].status = true;
//readConfig[28].DID = "CF8E";
//readConfig[28].name = "六轴传感器状态";
//readConfig[28].status = true;
//readConfig[29].DID = "CF8F";
//readConfig[29].name = "音频文件状态";
//readConfig[29].status = true;
//readConfig[30].DID = "CF90";
//readConfig[30].name = "外电电平";
//readConfig[30].status = true;
//---alm读节点----
alm_readConfig[0].DID = "CF01";
alm_readConfig[0].name = "alm软件版本号";
alm_readConfig[0].status = true;
alm_readConfig[1].DID = "CFA0";
alm_readConfig[1].name = "ACC1输出";
alm_readConfig[1].status = true;
alm_readConfig[2].DID = "CFA1";
alm_readConfig[2].name = "ACC1预充";
alm_readConfig[2].status = true;
alm_readConfig[3].DID = "CFA2";
alm_readConfig[3].name = "ACC2输出";
alm_readConfig[3].status = true;
alm_readConfig[4].DID = "CFA3";
alm_readConfig[4].name = "ACC2预充";
alm_readConfig[4].status = true;
alm_readConfig[5].DID = "CFA4";
alm_readConfig[5].name = "ACC1输入";
alm_readConfig[5].status = true;
alm_readConfig[6].DID = "CFA5";
alm_readConfig[6].name = "机械钥匙";
alm_readConfig[6].status = true;
alm_readConfig[7].DID = "CFA6";
alm_readConfig[7].name = "ACC2输入";
alm_readConfig[7].status = true;
alm_readConfig[8].DID = "CFA7";
alm_readConfig[8].name = "右转向灯输入";
alm_readConfig[8].status = true;
alm_readConfig[9].DID = "CFA8";
alm_readConfig[9].name = "蓝牙指示灯输出";
alm_readConfig[9].status = true;
alm_readConfig[10].DID = "CFA9";
alm_readConfig[10].name = "双闪输出";
alm_readConfig[10].status = true;
alm_readConfig[11].DID = "CFAA";
alm_readConfig[11].name = "轮动输入";
alm_readConfig[11].status = true;
alm_readConfig[12].DID = "CFAB";
alm_readConfig[12].name = "左转向灯输入";
alm_readConfig[12].status = true;
alm_readConfig[13].DID = "CFAC";
alm_readConfig[13].name = "MOTO_A输出";
alm_readConfig[13].status = true;
alm_readConfig[14].DID = "CFAD";
alm_readConfig[14].name = "MOTO_B输出";
alm_readConfig[14].status = true;
alm_readConfig[15].DID = "CFAE";
alm_readConfig[15].name = "锁电机输出";
alm_readConfig[15].status = true;
alm_readConfig[16].DID = "CFAF";
alm_readConfig[16].name = "电磁阀输出";
alm_readConfig[16].status = true;
alm_readConfig[17].DID = "CFB0";
alm_readConfig[17].name = "一键启动输入";
alm_readConfig[17].status = true;
alm_readConfig[18].DID = "CFB1";
alm_readConfig[18].name = "坐垫感应输入";
alm_readConfig[18].status = true;
alm_readConfig[19].DID = "CFB2";
alm_readConfig[19].name = "外电电压值";
alm_readConfig[19].status = true;
alm_readConfig[20].DID = "CFB3";
alm_readConfig[20].name = "K线通讯";
alm_readConfig[20].status = true;
alm_readConfig[21].DID = "CFB4";
alm_readConfig[21].name = "蓝牙连接";
alm_readConfig[21].status = true;
alm_readConfig[22].DID = "CFB5";
alm_readConfig[22].name = "弹簧振子";
alm_readConfig[22].status = true;
//---alm写节点----
alm_writeConfig[0].DID = "CF4D";
alm_writeConfig[0].name = "外电电压AD比例值";
alm_writeConfig[0].status = true;
alm_writeConfig[1].DID = "CF61";
alm_writeConfig[1].name = "音量等级";
alm_writeConfig[1].status = true;
alm_writeConfig[2].DID = "CF62";
alm_writeConfig[2].name = "声音主题";
alm_writeConfig[2].status = true;
alm_writeConfig[3].DID = "CF63";
alm_writeConfig[3].name = "ACC电压AD分压比例值";
alm_writeConfig[3].status = true;
alm_writeConfig[4].DID = "CF64";
alm_writeConfig[4].name = "蓝牙MCA地址";
alm_writeConfig[4].status = true;
//-----读写----
writeConfig[0].DID = "CF40";
writeConfig[0].name = "硬件版本号";
writeConfig[0].status = true;
writeConfig[1].DID = "CF41";
writeConfig[1].name = "产品型号";
writeConfig[1].status = true;
writeConfig[2].DID = "CF42";
writeConfig[2].name = "产品识别码";
writeConfig[2].status = true;
writeConfig[3].DID = "CF43";
writeConfig[3].name = "产商识别码";
writeConfig[3].status = true;
writeConfig[4].DID = "CF44";
writeConfig[4].name = "客户识别码";
writeConfig[4].status = true;
// writeConfig[6].DID = "CF45";
// writeConfig[6].name = "生产日十位";
writeConfig[5].DID = "CF46";
writeConfig[5].name = "生产日";
writeConfig[5].status = true;
writeConfig[6].DID = "CF47";
writeConfig[6].name = "生产月份";
writeConfig[6].status = true;
// writeConfig[7].DID = "CF48";
//writeConfig[7].name = "生产年份十位";
writeConfig[7].DID = "CF49";
writeConfig[7].name = "生产年份";
writeConfig[7].status = true;
writeConfig[8].DID = "CF4A";
writeConfig[8].name = "流水号";
writeConfig[8].status = true;
writeConfig[9].DID = "CF4B";
writeConfig[9].name = "服务器域名";
writeConfig[9].status = true;
writeConfig[10].DID = "CF4C";
writeConfig[10].name = "服务器端口号";
writeConfig[10].status = true;
writeConfig[11].DID = "CF4D";
writeConfig[11].name = "外电电压AD比列值";
writeConfig[11].status = true;
writeConfig[12].DID = "CF4E";
writeConfig[12].name = "物料编码";
writeConfig[12].status = true;
writeConfig[13].DID = "CF4F";
writeConfig[13].name = "供应商编码";
writeConfig[13].status = true;
writeConfig[14].DID = "CF50";
writeConfig[14].name = "批次号";
writeConfig[14].status = true;
}
int index = 0;
int index2 = 0, flag_dy = 0;
string flag_pass = "FAIL";
private void timer1_Tick(object sender, EventArgs e)
{
if (serialPort.IsOpen && flag_heart == 0 ) //串口打开且无心跳 进入连接流程
{
out_time = 0;
flag_connet = 1;
}
if (!checkBox3.Checked)
{
//this.dataGridView1.Rows.Clear();
///---------初始化------------
if (index < 1)
{
for (int i = 0; i < readConfig.Length; i++)
{
if (readConfig[i].status)
{
index = this.dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[0].Value = readConfig[i].name;
dataGridView1.Rows[index].Cells[1].Value = readConfig[i].threshold;
}
}
//for (int i = 0; i < alm_readConfig.Length; i++)
//{
// if (alm_readConfig[i].status)
// {
// index = this.dataGridView1.Rows.Add();
// dataGridView1.Rows[index].Cells[0].Value = alm_readConfig[i].name;
// dataGridView1.Rows[index].Cells[1].Value = alm_readConfig[i].threshold;
// }
//}
}
if (index2 < 1)
{
for (int i = 0; i < writeConfig.Length; i++)
{
if (writeConfig[i].status)
{
index2 = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index2].Cells[0].Value = writeConfig[i].name;
dataGridView2.Rows[index2].Cells[1].Value = writeConfig[i].threshold;
}
}
//for (int i = 0; i < alm_writeConfig.Length; i++)
//{
// if (alm_readConfig[i].status)
// {
// index2 = this.dataGridView2.Rows.Add();
// dataGridView2.Rows[index2].Cells[0].Value = alm_writeConfig[i].name;
// dataGridView2.Rows[index2].Cells[1].Value = alm_writeConfig[i].threshold;
// }
//}
}
///---------测试中------------
int smt_result = 1;
if (index <= 0 || index2 <= 0)
{
return;
}
for (int i = 0; i <= index; i++)
{
string name = (string)dataGridView1.Rows[i].Cells[0].Value;
// 查找特定 value 对应的 name
int readindex = Array.FindIndex(readConfig, myUnitClass => myUnitClass.name == name.ToString());
dataGridView1.Rows[i].Cells[1].Value = readConfig[readindex].threshold;
if (readConfig[readindex].name == "拨号状态" || readConfig[readindex].name == "驻网状态" ||
readConfig[readindex].name == "服务器连接状态" || readConfig[readindex].name == "GNSS定位状态" ||
readConfig[readindex].name == "K线通讯状态")
{
if (readConfig[readindex].threshold == "1")
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "GNSS SNR值")
{
int snr_cnt = 0;
if (flag_gnss == 1)
{
for (int a = 0; a < 4; a++)
{
if (Convert.ToInt64(numbers[a]) >= gnss)
{
snr_cnt++;
}
}
}
if (snr_cnt == 4)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "RSSI值")
{
if (readConfig[readindex].Ivalue >= Convert.ToInt32(rssi) && readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "SINR值")
{
if (readConfig[readindex].Ivalue >= Convert.ToInt32(sinr) && readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "RSRP值")
{
if (readConfig[readindex].Ivalue <= Convert.ToInt32(rsrp_ck[0]) && readConfig[readindex].Ivalue >= Convert.ToInt32(rsrp_ck[1]) &&
readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "RSRQ值")
{
if (readConfig[readindex].Ivalue <= Convert.ToInt32(rsrq_ck[0]) && readConfig[readindex].Ivalue >= Convert.ToInt32(rsrq_ck[1]) &&
readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "ACC管脚电平状态")
{
if (readConfig[readindex].Ivalue == Convert.ToInt32(readConfig[readindex].Svalue) && readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else if (readConfig[readindex].name == "外电电压值")
{
if (readConfig[readindex].Ivalue <=wd2 && readConfig[readindex].Ivalue >= wd1 && readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else
{
if (readConfig[readindex].Svalue != null)
{
if (readConfig[readindex].threshold == readConfig[readindex].Svalue)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
else
{
if (readConfig[readindex].threshold != "" && readConfig[readindex].threshold != null)
{
dataGridView1.Rows[i].Cells[2].Value = "pass";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView1.Rows[i].Cells[2].Value = "fail";
dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
}
}
}
int rowIndex = -1;
if (dataGridView2.IsCurrentCellInEditMode)
{
DataGridViewCell currentCell = dataGridView2.CurrentCell;
rowIndex = currentCell.RowIndex; // 当前单元格的行索引
// 获取编辑控件
}
int write_result = 1;
for (int i = 0; i <= index2; i++)
{
string name2 = (string)dataGridView2.Rows[i].Cells[0].Value;
// 查找特定 value 对应的 name
int writeindex = Array.FindIndex(writeConfig, myUnitClass => myUnitClass.name == name2.ToString());
if (i != rowIndex)
{
dataGridView2.Rows[i].Cells[1].Value = writeConfig[writeindex].threshold;
}
if (writeConfig[writeindex].name == "物料编码")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_wl)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf4e, bd_wl);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "供应商编码")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_cs)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf4f, bd_cs);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "生产日")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_day)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf45, bd_day);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "生产月份")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_month)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf47, bd_month);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "生产年份")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_year)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf48, bd_year);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "流水号")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_ls)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf4a, bd_ls);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else if (writeConfig[writeindex].name == "批次号")
{
if (flag_sn == 1)
{
if (writeConfig[writeindex].threshold == bd_ls)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
yd_did_write_one(0xcf50, bd_ls);
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != null && writeConfig[writeindex].threshold != "")
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
else
{
if (writeConfig[writeindex].Svalue != null)
{
if (writeConfig[writeindex].threshold == writeConfig[writeindex].Svalue)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
else
{
if (writeConfig[writeindex].threshold != "" && writeConfig[writeindex].threshold != null)
{
dataGridView2.Rows[i].Cells[2].Value = "pass";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Green;
writeConfig[writeindex].Bvalue = true;
smt_result *= 1;
write_result *= 1;
}
else
{
dataGridView2.Rows[i].Cells[2].Value = "fail";
dataGridView2.Rows[i].Cells[2].Style.BackColor = Color.Red;
writeConfig[writeindex].Bvalue = false;
smt_result *= 0;
write_result *= 0;
}
}
}
}
if (flag_heart == 0)
{
return;
}
if (write_result ==0 && flag_sn == 1)
{
for (int i = 0; i < writeConfig.Length; i++)
{
if (writeConfig[i].Svalue != null && writeConfig[i].Svalue != "(不填)")
{
string hexString = writeConfig[i].DID;
ushort did = ushort.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
yd_did_write_one(did, writeConfig[i].Svalue);
}
}
}
if (smt_result == 0 && flag_sn==1)
{
label2.BackColor = Color.DarkGray;
flag_result = 0;
}
if (smt_result == 0)
{
label2.BackColor = Color.DarkGray;
flag_result = 0;
}
if (smt_result == 1)
{
label2.Text = "测试通过"+out_time;
label2.BackColor = Color.Green;
flag_result = 1;
flag_pass = "PASS";
//-----结束测试-----
//flag_heart = 0;
//out_time = -1;
//_yd_disconnet();
//flag_connet = 0;
//button1.Text = "开始测试";
if (checkBox2.Checked == true && comboBox1.Text != "" && flag_dy == 0 && flag_sn==1)
{
save_zb();
PaperSize paperSize = new PaperSize("Custom", 260, 75); // 宽度500, 高度700
// paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.DefaultPageSettings.Margins.Top = 0;
printDocument1.DefaultPageSettings.Margins.Left = 0;
pagesToPrint = Convert.ToInt32(comboBox2.Text);
//PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
//printPreviewDialog.Document = printDocument1;
//try
//{
// printPreviewDialog.ShowDialog();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //"导出为WPS PDF"; //
printDocument1.Print();
flag_dy = 1;
}
}
}
if (checkBox3.Checked)
{
int smt_result = 1;
dataGridView2.Rows[0].Cells[1].Value = readConfig[2].threshold;
dataGridView2.Rows[1].Cells[1].Value = readConfig[3].threshold;
dataGridView2.Rows[2].Cells[1].Value = writeConfig[5].threshold;
dataGridView2.Rows[3].Cells[1].Value = writeConfig[6].threshold;
dataGridView2.Rows[4].Cells[1].Value = writeConfig[7].threshold;
dataGridView2.Rows[5].Cells[1].Value = writeConfig[8].threshold;
dataGridView2.Rows[6].Cells[1].Value = writeConfig[14].threshold;
dataGridView2.Rows[7].Cells[1].Value = writeConfig[13].threshold;
dataGridView2.Rows[8].Cells[1].Value = writeConfig[12].threshold;
if (jy_imei == readConfig[2].threshold && readConfig[2].threshold != "")
{
dataGridView2.Rows[0].Cells[2].Value = "pass";
dataGridView2.Rows[0].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[0].Cells[2].Value = "fail";
dataGridView2.Rows[0].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_iccid == readConfig[3].threshold && readConfig[3].threshold != "")
{
dataGridView2.Rows[1].Cells[2].Value = "pass";
dataGridView2.Rows[1].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[1].Cells[2].Value = "fail";
dataGridView2.Rows[1].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_year == writeConfig[7].threshold && jy_year == bd_year && bd_year == writeConfig[7].threshold && writeConfig[7].threshold != "")
{
dataGridView2.Rows[2].Cells[2].Value = "pass";
dataGridView2.Rows[2].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[2].Cells[2].Value = "fail";
dataGridView2.Rows[2].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_month == writeConfig[6].threshold && jy_month == bd_month && bd_month == writeConfig[6].threshold && writeConfig[6].threshold != "")
{
dataGridView2.Rows[3].Cells[2].Value = "pass";
dataGridView2.Rows[3].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[3].Cells[2].Value = "fail";
dataGridView2.Rows[3].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_day == writeConfig[5].threshold && jy_day == bd_day && bd_day == writeConfig[5].threshold && writeConfig[5].threshold != "")
{
dataGridView2.Rows[4].Cells[2].Value = "pass";
dataGridView2.Rows[4].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[4].Cells[2].Value = "fail";
dataGridView2.Rows[4].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_ls == writeConfig[8].threshold && jy_ls == bd_ls && bd_ls == writeConfig[8].threshold && writeConfig[8].threshold != "")
{
dataGridView2.Rows[5].Cells[2].Value = "pass";
dataGridView2.Rows[5].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[5].Cells[2].Value = "fail";
dataGridView2.Rows[5].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_ls == writeConfig[14].threshold && jy_ls == bd_ls && bd_ls == writeConfig[14].threshold && writeConfig[14].threshold != "")
{
dataGridView2.Rows[6].Cells[2].Value = "pass";
dataGridView2.Rows[6].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[6].Cells[2].Value = "fail";
dataGridView2.Rows[6].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_cs == writeConfig[13].threshold && jy_cs == bd_cs && bd_cs == writeConfig[13].threshold && writeConfig[13].threshold != "")
{
dataGridView2.Rows[7].Cells[2].Value = "pass";
dataGridView2.Rows[7].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[7].Cells[2].Value = "fail";
dataGridView2.Rows[7].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (jy_wl == writeConfig[12].threshold && jy_wl == bd_wl && bd_wl == writeConfig[12].threshold && writeConfig[12].threshold != "")
{
dataGridView2.Rows[8].Cells[2].Value = "pass";
dataGridView2.Rows[8].Cells[2].Style.BackColor = Color.Green;
smt_result *= 1;
}
else
{
dataGridView2.Rows[8].Cells[2].Value = "fail";
dataGridView2.Rows[8].Cells[2].Style.BackColor = Color.Red;
smt_result *= 0;
}
if (smt_result == 1)
{
string str_get_sn = "YD" + writeConfig[7].threshold.Substring(1, 1) + writeConfig[6].threshold.Substring(1, 1) + writeConfig[5].threshold + writeConfig[8].threshold.Substring(2, 4);
if (str_get_sn == textBox3.Text)
{
smt_result *= 1;
label10.Text = " ";
}
else
{
smt_result *= 0;
label10.Text = "SN号有误";
label10.ForeColor = Color.Red;
}
}
else
{
return;
}
if (smt_result == 1)
{
label2.Text = "测试通过";
label2.BackColor = Color.Green;
flag_pass = "PASS";
flag_result = 1;
//flag_heart = 0;
//out_time = -1;
//_yd_disconnet();
//flag_connet = 0;
//button1.Text = "开始测试";
}
}
}
private void DataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
// 检查是否点击了按钮列
if (e.ColumnIndex == dataGridView2.Columns["ButtonColumn"].Index && e.RowIndex >= 0)
{
// 获取点击的行索引
//flag_write = 1;
int rowIndex = e.RowIndex;
object name = dataGridView2.Rows[rowIndex ].Cells[0].Value;
// 查找特定 value 对应的 name
int index = Array.FindIndex(writeConfig, myUnitClass => myUnitClass.name == name.ToString());
UInt16 did = Convert.ToUInt16(writeConfig[index].DID, 16);
// object cellValue = dataGridView2.Rows[rowIndex].Cells[1].Value;
string data = writeConfig[e.RowIndex].threshold;
yd_did_write_one(did,data);
writeConfig[rowIndex].threshold = "";
//MessageBox.Show($"Button clicked in row {rowIndex + 1}"); // 行索引从 0 开始,显示时加 1
}
}
private void cmbPort_DropDown(object sender, EventArgs e)
{
cmbBox_Port_Update();
}
private string get_date_time()
{
string strdate = string.Empty;
strdate = DateTime.Now.ToString();
return strdate;
}
string tempData=" ";
private void save_zb()
{
string str_get_sn = "YD" + writeConfig[7].threshold.Substring(1, 1) + writeConfig[6].threshold.Substring(1, 1) + writeConfig[5].threshold + writeConfig[8].threshold.Substring(2, 4);
string str_sw = "X" + writeConfig[1].threshold + readConfig[1].threshold.Substring(0, 2) + readConfig[1].threshold.Substring(3, 2);
tempData = tempData + get_date_time() + "," + "E-2" + writeConfig[0].threshold + "," +
str_sw + "," +str_get_sn+ ","+readConfig[2].threshold+","+ readConfig[3].threshold+","+flag_pass;
writeA883SmtLogCSV2(tempData);
tempData = " ";
}
public static void CreateDirectoryOrFile(string newFileName)
{
string strdate = string.Empty;
strdate = DateTime.Now.ToString();
string str = "";
if (strdate.IndexOf("/") != -1)
{
str = strdate.Replace("/", "_");
}
string[] sArray = str.Split(' ');//
//string path = Application.StartupPath + @"\configfile\研发\" + sArray[0];
// string path = @"C:\YD10日志\";//+ sArray[0];
string path = Application.StartupPath + @"\YD10日志\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
/*
if (!File.Exists(path + "\\"))
{
File.Create(path + "\\" + newFileName).Close();
}
*/
}
}
public void writeA883SmtLogCSV2(string data)//保存路径, 索引设备
{
CreateDirectoryOrFile("FileName.log");
string strdate = string.Empty;
strdate = DateTime.Now.ToString();
string str = "";
string str2 = "";
string testtime = DateTime.Now.ToShortTimeString().ToString();
if (strdate.IndexOf("/") != -1)
{
str = strdate.Replace("/", "_");
}
if (testtime.IndexOf(":") != -1)
{
str2 = testtime.Replace(":", "点");
}
// 使用正确的 Split 方法
string[] sArray = str.Split(' '); // 以空格分隔
string smt_log_path2 = Application.StartupPath + @"\YD10日志\";
string str_get_sn = "YD" + writeConfig[7].threshold.Substring(1, 1) + writeConfig[6].threshold.Substring(1, 1) + writeConfig[5].threshold + writeConfig[8].threshold.Substring(2,4);
//判断是否已经有了这个文件
FileStream fs = null;
StreamWriter sw = null;
try
{
if (!System.IO.File.Exists($"{smt_log_path2}\\{sArray[0]}.CSV"))//路径 日期 序列号
{
//没有则创建这个文件
fs = new FileStream($"{smt_log_path2}\\{sArray[0]}.CSV", FileMode.Create, FileAccess.Write);//创建
sw = new StreamWriter(fs);
}
else
{
/*
fs = new FileStream($"{smt_log_path1}\\{My_Log[LogIndex_T].Barcode}_{sArray[0]}.log", FileMode.Open, FileAccess.Write);
sw = new StreamWriter(fs);*/
sw = File.AppendText($"{smt_log_path2}\\{sArray[0]}.CSV");
}
sw.WriteLine(data);
if (sw != null)
{
sw.Close();
}
if (fs != null)
{
fs.Close();
}
}
catch (Exception ex)
{
if (sw != null)
{
sw.Close();
}
if (fs != null)
{
fs.Close();
}
}
//My_Log[LogIndex_T].Status = false;
}
int time = 0,time_over=0, flag_connet=0,flag_heart,time2=0,flag_print=0;
private int pagesToPrint; // 要打印的张数
private BarCodeClass bcc = new BarCodeClass();
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//if ( flag_print == 0) //保存数据
//{
// save_zb();
//}
if (flag_print == 1)
{
string tm2 = "#3-868022069907587 898604F3102340070986&39121-A081-01003560820240724000001";
bcc.QuickMarkHeight = 100;
bcc.QuickMarkWidth = 100;
Image image2 = bcc.CreateQuickMark(tm2);
e.Graphics.DrawImage(image2, 175, 8, 70, 70);
e.Graphics.DrawString("雅 迪 科 技 集 团 有 限 公 司", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, 28, 0);
e.Graphics.DrawString("名称:IOT (JW) 供应商代码:35608", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 13);
e.Graphics.DrawString("物料编码:39110-0047-0100", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 23);
e.Graphics.DrawString("电压:48-72V", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 130, 23);
e.Graphics.DrawString("HW:E-213", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 33);
e.Graphics.DrawString("类型:一键启动/无电池/无手柄", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 50, 33);
e.Graphics.DrawString("SW:X450805", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 43);
//e.Graphics.DrawString("SN:" + myConfig[6].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 70, 43);
e.Graphics.DrawString("IMEI:868022069907587", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 53);
e.Graphics.DrawString("ICCID:898604F3102340070986" + readConfig[3].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 63);
flag_print = 0;
}
else
{
string tm = "#3-" + readConfig[2].threshold + " " + readConfig[3].threshold + "&" + writeConfig[12].threshold + "3560820" +
writeConfig[7].threshold + writeConfig[6].threshold + writeConfig[5].threshold + writeConfig[8].threshold;
string str_get_sn = "YD" + writeConfig[7].threshold.Substring(1, 1) + writeConfig[6].threshold.Substring(1, 1) + writeConfig[5].threshold + writeConfig[8].threshold.Substring(2, 4);
string str_sw = "X" + writeConfig[1].threshold + readConfig[1].threshold.Substring(0, 2) + readConfig[1].threshold.Substring(3, 2);
bcc.QuickMarkHeight = 100;
bcc.QuickMarkWidth = 100;
Image image = bcc.CreateQuickMark(tm);
e.Graphics.DrawImage(image, 175, 8, 70, 70);
e.Graphics.DrawString("雅 迪 科 技 集 团 有 限 公 司", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, 28, 0);
e.Graphics.DrawString("名称:"+ product + " 供应商代码:" + writeConfig[13].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 13);
e.Graphics.DrawString("物料编码:" + writeConfig[12].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 23);
//e.Graphics.DrawString("电压:48-72V", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 130, 23);
e.Graphics.DrawString("HW:" +"E-2"+ writeConfig[0].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 33);
// e.Graphics.DrawString("类型:一键启动/无电池/无手柄", new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 50, 33);
e.Graphics.DrawString("SW:" + str_sw, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 43);
e.Graphics.DrawString("SN:" + str_get_sn, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 70, 43);
e.Graphics.DrawString("IMEI:" + readConfig[2].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 53);
e.Graphics.DrawString("ICCID:" + readConfig[3].threshold, new Font(new FontFamily("宋体"), 6, FontStyle.Bold), System.Drawing.Brushes.Black, 3, 63);
}
pagesToPrint--;
if (pagesToPrint <= 0)
{
e.HasMorePages = false;
}
else
{
e.HasMorePages = true;
}
}
private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView2.IsCurrentCellInEditMode)
{
DataGridViewCell currentCell = dataGridView2.CurrentCell;
int rowIndex = currentCell.RowIndex; // 当前单元格的行索引
if (e.RowIndex == rowIndex)
{
object newValue = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
writeConfig[e.RowIndex].threshold = (string)newValue;
flag_write = 1;
time2 = 20;
}
}
}
public void GetPrinter()
{
//string strList = "";
//System.Drawing.Printing.PrinterSettings.StringCollection PrinterList = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
//foreach (var item in PrinterList)
//{
// strList += item + ",";
// comboBox1.Items.Add(item);
//}
//return strList;
PrintDocument printDoc = new PrintDocument();
PrinterSettings printerSettings;
foreach (string printerName in PrinterSettings.InstalledPrinters)
{
printerSettings = new PrinterSettings();
printerSettings.PrinterName = printerName;
printDoc.PrinterSettings = printerSettings;
if (printerName!= "Microsoft Print to PDF") {
comboBox1.Items.Add(printerName);
}
//Console.WriteLine(printerName);
}
}
private void button3_Click(object sender, EventArgs e)
{
PaperSize paperSize = new PaperSize("Custom", 260, 75); // 宽度500, 高度700
// paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.DefaultPageSettings.Margins.Top = 0;
printDocument1.DefaultPageSettings.Margins.Left = 0;
pagesToPrint = 1;
printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //"导出为WPS PDF"; //
printDocument1.Print();
}
string bd_wl="", bd_cs = "", bd_year = "", bd_year2 = "", bd_month = "", bd_day = "", bd_day1 = "", bd_ls = "";
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
JObject setData_jsonObject = new Newtonsoft.Json.Linq.JObject();
if (flag_yd07close == 0)
{
setData_jsonObject.Add("车型", "YD10");
}
else
{
setData_jsonObject.Add("车型", "YD07k");
}
setData_jsonObject.Add("打印机", comboBox1.Text);
setData_jsonObject.Add("张数", comboBox2.Text);
setData_jsonObject.Add("自动打印机", checkBox2.Checked);
setData_jsonObject.Add("自动蓝牙连接", yd_ble);
setData_jsonObject.Add("号码校验", checkBox3.Checked);
string config_file_str = setData_jsonObject.ToString();
//string savePath = Application.StartupPath + @"\配置.json";
string savePath = Path.Combine(Application.StartupPath, "配置.json");
System.IO.File.WriteAllText(savePath, config_file_str, Encoding.UTF8);
//this.Invoke(new Action(() =>
//{
//
//}));
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void yD10ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Text = "YD10上位机测试V1.9";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
int flag_save = 0;
private void yD07KToolStripMenuItem_Click(object sender, EventArgs e)
{
//this.Text = "YD07K上位机测试V1.9";
if (serialPort.IsOpen)
{
_yd_disconnet();
serialPort.Close();
}//关闭串口
// 创建 Form3 的实例
Form3 form3 = new Form3(this);
// 隐藏 Form1
this.Hide();
// 显示 Form3
form3.ShowDialog();
//this.Close();
}
private void 手动打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
save_zb();
PaperSize paperSize = new PaperSize("Custom", 260, 75); // 宽度500, 高度700
// paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.DefaultPageSettings.Margins.Top = 0;
printDocument1.DefaultPageSettings.Margins.Left = 0;
pagesToPrint = 1;//Convert.ToInt32(comboBox2.Text);
printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //"导出为WPS PDF"; //
printDocument1.Print();
}
string jy_iccid = "", jy_imei = "", jy_wl = "", jy_cs = "", jy_year = "", jy_month = "", jy_day="", jy_ls = "";
private void textBox2_TextChanged(object sender, EventArgs e)
{
//#3-868022069907587 898604F3102340070986&39121-A081-01003560820240724000001
if (textBox2.Text.Length == 74)
{
jy_imei = textBox2.Text.Substring(3, 15);
jy_iccid = textBox2.Text.Substring(19, 20);
jy_wl= textBox2.Text.Substring(40, 15);
jy_cs = textBox2.Text.Substring(55, 5);
jy_year= textBox2.Text.Substring(62,2 );
jy_month = textBox2.Text.Substring(64, 2);
jy_day = textBox2.Text.Substring(66, 2);
jy_ls = textBox2.Text.Substring(68, 6);
label7.Text = "";
}
else
{
jy_iccid = "";
jy_imei = "";
jy_wl = "";
jy_cs = "";
jy_year = "";
jy_month = "";
jy_day = "";
jy_ls = "";
if (textBox2.Text.Length != 0)
{
label7.Text = "长度有误";
}
else
{
label7.Text = "";
}
}
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
index = 0;
index2 = 0;
this.dataGridView1.Rows.Clear();
this.dataGridView2.Rows.Clear();
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "设备IMEI";
dataGridView2.Rows[index].Cells[1].Value = readConfig[2].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "设备ICCID";
dataGridView2.Rows[index].Cells[1].Value = readConfig[3].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产日";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[5].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产月份";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[6].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "生产年份";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[7].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "流水号";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[8].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "批次号";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[14].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "供应商编码";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[13].threshold;
index = this.dataGridView2.Rows.Add();
dataGridView2.Rows[index].Cells[0].Value = "物料编码";
dataGridView2.Rows[index].Cells[1].Value = writeConfig[12].threshold;
}
else
{
textBox2.Enabled = false;
textBox3.Enabled = false;
index = 0;
index2 = 0;
this.dataGridView1.Rows.Clear();
this.dataGridView2.Rows.Clear();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
int flag_sn = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length == 34)
{
bd_wl = textBox1.Text.Substring(0, 15);
bd_cs = textBox1.Text.Substring(15, 5);
bd_year = textBox1.Text.Substring(22, 2);
bd_month = textBox1.Text.Substring(24, 2);
bd_day = textBox1.Text.Substring(26, 2);
bd_ls = textBox1.Text.Substring(28, 6);
//bd_sn= textBox1.Text.Substring(23, 1)+ textBox1.Text.Substring(25, 1)+ textBox1.Text.Substring(26, 2)+ textBox1.Text.Substring(31, 3);
label5.Text = "";
flag_sn = 1;
}
else
{
bd_wl ="";
bd_cs = "";
bd_year = "";
bd_year2 = "";
bd_month = "";
bd_day = "";
bd_day1 = "";
bd_ls = "";
//bd_sn = "";
flag_sn = 0;
if (textBox1.Text.Length != 0)
{
label5.Text = "长度有误";
}
else
{
label5.Text = "";
}
}
}
private void 打印较准页ToolStripMenuItem_Click(object sender, EventArgs e)
{
PaperSize paperSize = new PaperSize("Custom", 260, 75); // 宽度500, 高度700
// paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.DefaultPageSettings.Margins.Top = 0;
printDocument1.DefaultPageSettings.Margins.Left = 0;
pagesToPrint = 1;
flag_print = 1;
printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //"导出为WPS PDF"; //
printDocument1.Print();
}
private void 标定信息设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Form2Closed += Form2_Closed;
form2.Show();
}
private void Form2_Closed()
{
read();
index = 0;
index2 = 0;
this.dataGridView1.Rows.Clear();
this.dataGridView2.Rows.Clear();
}
int out_time = -1, flag_result=1;
//private void button1_Click(object sender, EventArgs e)
//{
// if (!serialPort.IsOpen)
// {
// txtStatus.Text = "请先打开串口!";
// return;
// }
// if (button1.Text=="开始测试")
// {
// clean();
// out_time = 0;
// label2.Text = "测试开始";
// //_yd_disconnet();
// flag_connet = 1;
// button1.Text ="结束测试";
// }
// else if (button1.Text == "结束测试")
// {
// flag_heart = 0;
// _yd_disconnet();
// flag_connet = 0;
// out_time = -1;
// button1.Text = "开始测试";
// label2.Text = "测试已结束";
// }
//}
private void clean()
{
for (int i = 0; i < readConfig.Length; i++)
{
readConfig[i].threshold = "";
}
for (int i = 0; i < writeConfig.Length; i++)
{
writeConfig[i].threshold = "";
writeConfig[i].Bvalue = false;
}
this.Invoke(new Action(() =>
{
label2.Text = "等待测试";
label2.BackColor = Color.DarkGray;
}));
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
flag_gnss = 0;
heart_out = 0;
flag_dy = 0;
flag_result = 0;
flag_pass = "FAIL";
Heart = false;
heart = false;
}
int did_i, flag_write,heart_out,time_heart;
bool heart=false, Heart=false;
private void timer2_Tick(object sender, EventArgs e)
{
time++;
time2++;
time_heart++; //15次约为1s
//超时机制
if (time2 > 15 /*&& out_time<120 */&& out_time>=0)
{
time2 = 0;
out_time++;
if (flag_result == 0)
{
label2.Text = "测试开始" + out_time;
}
//if (flag_connet == 1 && out_time > 10)
//{
// label2.Text = "连接超时";
// label2.BackColor = Color.Red;
// flag_heart = 0;
// flag_connet = 0;
// out_time = -1;
// button1.Text = "开始测试";
//}
//else
//{
// label2.Text = "连接中..." + Convert.ToString(out_time);
//}
//if (out_time>=120)
//{
// label2.Text = "测试超时";
// label2.BackColor = Color.Red;
// flag_heart = 0;
// flag_connet = 0;
// out_time = -1;
// button1.Text = "开始测试";
//}
}
//
if (flag_connet==1)
{
label2.Text = "连接中...";
_yd_connet();
}
if (time > 30 && flag_heart==1) //3s发送一次心跳包(改为2s)
{
time = 0;
yd_heart();
}
if (time_heart >=8 && flag_heart == 1) //500ms来检测一次保活
{
time_heart = 0;
if (!Heart && heart)//说明心跳断开一次
{
heart_out++;
if (heart_out >= 10)//5S不回复说明设备已经离线
{
//-----结束测试-----
flag_heart = 0;
out_time = -1;
_yd_disconnet();
flag_connet = 0;
//button1.Text = "开始测试";
label2.Text = "设备已离线";
clean();
}
}
else
{
heart = Heart;
Heart = false;
heart_out = 0;
}
}
if (flag_write == 1 && time2>0)
{
time2--;
if (time2==0)
{
flag_write = 0;
label2.Text = "写入响应超时";
}
}
if (flag_heart == 1 && flag_write == 0)
{
zd_test(did_i);
did_i++;
if (did_i > 40)
{
did_i = 0;
}
}
}
}
}