private void HandleAinuoBSerialData()
{
string cmd = "";
//double V = 0.0;
//double A = 0.0;
//double W = 0.0;
//double WH = 0.0;
byte[] bytesCmd = new byte[8] { 0x7B, 0x00, 0x08, 0x01, 0xF0, 0x00, 0xF9, 0x7D };//request power consumption
while (sComm4.IsOpen && EnableReceive)
{
try
{
sComm4.DiscardInBuffer();
sComm4.DiscardOutBuffer();
sComm4.Write(bytesCmd, 0, 8);
byte[] bytesReceive = new byte[12];
byte CalChecksum = 0;
for (int i = 0; i < bytesReceive.Length; i++)
{
bytesReceive[i] = (byte)sComm4.ReadByte();
CalChecksum ^= (byte)bytesReceive[i];
cmd += bytesReceive[i].ToString();
}
//string StrBuffer = sComm3.ReadLine();
// string[] strData = StrBuffer.Split(',');
// byte[] ReceiveChecksumBytes = new byte[] { (byte)bytesReceive[bytesReceive.Length - 2], (byte)bytesReceive[bytesReceive.Length - 1] };
// string ChecksumStr = Encoding.ASCII.GetString(ReceiveChecksumBytes);
// byte ReceiveChecksum = byte.Parse(ChecksumStr, System.Globalization.NumberStyles.HexNumber);
// if (ReceiveChecksum == CalChecksum)
// {
// Log.SetNewData_AinuoB(ref strData);
// NumReceived4++;
// IsNeedUpdateUI = true;
// }
}
catch (Exception)
{
//MessageBox.Show(e.Message);
}
Thread.Sleep(1000);
}
}
private void ToolStripButton_OpenPort_Click(object sender, EventArgs e)
{
string statusPort4 = "已关闭";
if (ToolStripButton_OpenPort.Text == "打开端口")
{
try
{
bool operatePort4 = false;
if (cmPort4.Text.StartsWith("COM"))
{
operatePort4 = true;
sComm4 = new SerialPort();
sComm4.PortName = cmPort4.Text;
}
else
{
sComm4 = null;
}
if (operatePort4)
{ }
else
{
MessageBox.Show("端口号无效");
return;
}
PortUtil.OpenPort(sComm4);
if (operatePort4 && sComm4.IsOpen)
{
NumReceived4 = 0;
updateTimer.Enabled = true;
EnableReceive = true;
if (serialThread_AinuoB != null)
{
serialThread_AinuoB.Abort();
}
serialThread_AinuoB = new Thread(HandleAinuoBSerialData);
serialThread_AinuoB.Start();
statusPort4 = "已打开 " + Convert.ToString(sComm4.PortName) + ' ' + Convert.ToString(sComm4.BaudRate) + ' ' + Convert.ToString(sComm4.DataBits) + ' ' + Convert.ToString(sComm4.Parity) + ' ' + Convert.ToString(sComm4.StopBits);
}