using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace PLC
{
public partial class Form1 : Form
{
SerialPort sPort = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!sPort.IsOpen)
{
MessageBox.Show("串口未打开!");
return;
}
if (comboBox1.SelectedIndex == -1)
{
MessageBox.Show("请选择读写!");
return;
}
if (textBox4.Text.Trim() == String.Empty)
{
MessageBox.Show("请输入位数!");
return;
}
if (Convert.ToInt32(textBox4.Text) % 2 != 0)
{
MessageBox.Show("位数只能为偶数位");
return;
}
int a = Convert.ToInt32(textBox1.Text);
int b = a * 2 + 4096;
string str = Convert.ToString(b, 16).ToUpper();
String str2 = BitConverter.ToString(Encoding.Default.GetBytes(str)).Replace("-", "");//转ascii码
string str1;
string str3;
string str4 = "02";
if (comboBox1.SelectedIndex == 0&&comboBox1.SelectedIndex != -1)
{
str1 = "30";
}
else
{
str1 = "31";
}
string str5;
string str7 = "03'";
int weishu = Convert.ToInt32(textBox4.Text);
str5 = Convert.ToString(weishu, 16).ToUpper();
if (weishu < 10)
{
str5 = "0" + textBox4.Text;
}
if (weishu > 64)
{
MessageBox.Show("位数超过64的限制");
return;
}
String str6 = BitConverter.ToString(Encoding.Default.GetBytes(str5)).Replace("-", "");
str3 = str1 + str2 + str6 + str7;
long sum = 0;
foreach (Match match in Regex.Matches(str3, @"\d{2}"))
{
long s = Convert.ToInt64(match.Value, 16);
sum += s;
}
string str8 = Convert.ToString(sum, 16).ToUpper();
string str9 = str8.Substring(str8.Length - 1);
str9 = "3" + str9;
string str10 = str8.Substring(str8.Length - 2, str8.Length - 2);
str10 = "3" + str10;
string str13 = str4 + str3 + str10 + str9;
string str14 = str13.Replace("'", "");
string result = Regex.Replace(str14, @"(\d{2})", "$0 ");
textBox3.Text = result;
//string str15 = Regex.Replace(str3, @".{2}", "$0 ");
//for (int i = 0; i < str1.Length; i++)
//{
// textBox3.AppendText(arr[i] + " ");
//}
//02 30 31 30 46 36 30 34 03 37 34
str14 = str14.Replace(" ", "");
byte[] returnBytes = new byte[str14.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(str14.Substring(i * 2, 2), 16);
}
sPort.Write(returnBytes, 0, returnBytes.Length);
MessageBox.Show("发送成功");
// StringBuilder st = new StringBuilder();
// for (int i = 0; i < data.Length; i++)
//{
// st.AppendFormat("{0:x2}" + " ", data[i]);
// }
// AddContent(st.ToString().ToUpper());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//private byte[] strToHexByte(string hexString)
//{
// hexString = hexString.Replace(" ", "");
// byte[] returnBytes = new byte[hexString.Length / 2];
// for (int i = 0; i < returnBytes.Length; i++)
// {
// returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
// }
// return returnBytes;
//}
private void button2_Click(object sender, EventArgs e)
{
//try
//{
// //string data = sPort.ReadExisting();
//byte[] ReDatas = new byte[sPort.BytesToRead];
//sPort.Read(ReDatas, 0, ReDatas.Length);//读取数据
// this.AddData(ReDatas);//
//textBox2.Text = data.ToString();
if (sPort.BytesToRead == 0)
{
MessageBox.Show("请先发送数据!");
return;
}
byte[] ReDatas=new byte[sPort.BytesToRead];
sPort.Read(ReDatas,0,ReDatas.Length);
this.AddData(ReDatas);
textBox5.Text = textBox2.Text;
textBox2.Text = textBox2.Text.Replace(" ", "");
int weishu = Convert.ToInt32(textBox4.Text);
string str1 = textBox2.Text.Remove(0, 2);
string str4 = str1.Remove(str1.Length - 6, 6);
//string str2 = str4.Substring(0, weishu * 4 - 1);
byte[] buff = new byte[str4.Length / 2];
int index = 0;
for (int i = 0; i < str4.Length; i += 2)
{
buff[index] = Convert.ToByte(str4.Substring(i, 2), 16);
++index;
}
string str3 = Encoding.Default.GetString(buff);
//string result1 = Regex.Replace(str3, @"(\d{2})", ",");
string[] array = new string[str3.Length];
string[] array1 = new string[4];
string result = null;
string str = null;
for (int i = 0; i < str3.Length / 4; i++)
{
array[i] = str3.Substring(i * 4, 4);
for (int j = 0; j < 2; j++)
{
array1[j] = array[i].Substring(j * 2, 2);
str = array1[1] + array1[0];
}
long a = Convert.ToInt64(str, 16);
string str6 = a.ToString() + ",";
result += str6;
//Console.WriteLine(array1[0]);
}
textBox2.Text = result;
//foreach (Match match in Regex.Matches(str3, @"\d{2}"))
//{
// string[] array =new string(match.Value);
// textBox5.Text = array[1].ToString();
//}
//long a = Convert.ToInt64(str3, 16);
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (sPort.IsOpen == true)
{
sPort.Close();
}
sPort.PortName = "COM5";
sPort.BaudRate = 9600;
sPort.DataBits = 7;
sPort.StopBits = StopBits.One;
sPort.Parity = Parity.Even;
sPort.Open();
if (sPort.IsOpen == true)
{
MessageBox.Show("串口打开成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void AddData(byte[] data)
{
if (textBox2.Text != null)
{
textBox2.Text = null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:x2}" + " ", data[i]);
}
AddContent(sb.ToString().ToUpper().Trim());
}
private void AddContent(string content)
{
//this.BeginInvoke(new MethodInvoker(delegate
//{
if ( textBox2.Text.Length > 0)
{
textBox2.AppendText("\r\n");
}
textBox2.AppendText(content);
}
private void button4_Click(object sender, EventArgs e)
{
sPort.Close();
MessageBox.Show("串口已关闭");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
{
e.Handled = true;
}
// if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
// e.Handled = true;
// //小数点的处理。
// if ((int)e.KeyChar == 46) //小数点
// {
// if (textBox1.Text.Length <= 0)
// e.Handled = true; //小数点不能在第一位
// else
// {
// float f;
// float oldf;
// bool b1 = false, b2 = false;
// b1 = float.TryParse(textBox1.Text, out oldf);
// b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
// if (b2 == false)
// {
// if (b1 == true)
// e.Handled = true;
// else
// e.Handled = false;
// }
// }
// }
}
public void button5_Click(object sender, EventArgs e)
{
textBox5.Text = null;
textBox4.Text = null;
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
}
}
}