/// <summary> /// Сформировать пакет по протоколу DSN /// </summary> /// <param name="device">номер устройства</param> private byte[] CreateDSNProtocol(int device, typeCRC CrcType) { byte[] packet = { 0x7E, 0x00, 0x0A, 0x02, 0x20, 0x03, 0xAA, 0x55, 0x55, 0x00, 0x00 }; packet[1] = (byte)device; switch (crcType) { case typeCRC.CycleOneByte: CalculateCycleOneByte(packet); break; case typeCRC.CycleTwoByte: CalculateCycleTwoByte(packet); break; case typeCRC.CRC16: ushort crc = CalculateCRC16(packet, packet.Length - 2); byte[] crc_byte = BitConverter.GetBytes(crc); packet[packet.Length - 2] = crc_byte[1]; packet[packet.Length - 1] = crc_byte[0]; break; } return(packet); }
/// <summary> /// переключить устройство на протокол DSN /// </summary> /// <param name="device">номер устройства</param> private void SwitchDeviceToModbus(int device, typeCRC CrcType) { byte[] packet = CreateDSNProtocol(device, crcType); string toScreenString = ToStringPacket(packet); listBoxStatusViewer.Items.Add(string.Format("Отправка пакета:{0}", toScreenString)); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); mevent.Reset(); serialPort.Write(packet, 0, packet.Length); if (mevent.WaitOne(300)) { byte[] buffer = new byte[packet.Length]; int bytesRead = serialPort.Read(buffer, 0, buffer.Length); string answerString = ToStringPacket(buffer); listBoxStatusViewer.Items.Add(string.Format("Ответ :{0}", answerString)); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); Application.DoEvents(); if (bytesRead == packet.Length) { packet[1] |= 0x80; for (int index = 0; index < packet.Length - 2; index++) { if (packet[index] != buffer[index]) { // ---- Ошибка ---- listBoxStatusViewer.Items.Add(string.Format("Устройство: {0} не удалось переключить на протокол Modbus", device)); return; } } listBoxStatusViewer.Items.Add(string.Format("Устройство номер {0} успешно переключенно на протокол Modbus", device)); } else { listBoxStatusViewer.Items.Add(string.Format("Устройство: {0} не удалось переключить на протокол Modbus", device)); } } else { listBoxStatusViewer.Items.Add(string.Format("Устройство: {0} не отвечает", device)); } }
/// <summary> /// переключили тип контрольной суммы /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBoxTypeCrc_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxTypeCrc.SelectedIndex == 0) { crcType = typeCRC.CRC16; } else if (comboBoxTypeCrc.SelectedIndex == 1) { crcType = typeCRC.CycleOneByte; } else { crcType = typeCRC.CycleTwoByte; } }
/// <summary> /// переключить одно устройство /// </summary> /// <param name="deviceNumber">номер устройства</param> /// <param name="pType">Тип протокола</param> private void SwitchOne(int device, ProcolType pType, typeCRC CrcType) { switch (pType) { case ProcolType.DSN: SwitchDeviceToDSN(device); listBoxStatusViewer.SelectedIndex = listBoxStatusViewer.Items.Count - 1; break; case ProcolType.ModBus: SwitchDeviceToModbus(device, crcType); listBoxStatusViewer.SelectedIndex = listBoxStatusViewer.Items.Count - 1; break; } }
/// <summary> /// переключить все устройства /// </summary> /// <param name="pType">Тип протокола</param> private void SwitchAll(ProcolType pType, typeCRC CrcType) { switch (pType) { case ProcolType.DSN: for (int device = 1; device < 32; device++) { SwitchDeviceToDSN(device); listBoxStatusViewer.SelectedIndex = listBoxStatusViewer.Items.Count - 1; } break; case ProcolType.ModBus: for (int device = 1; device < 32; device++) { SwitchDeviceToModbus(device, CrcType); listBoxStatusViewer.SelectedIndex = listBoxStatusViewer.Items.Count - 1; } break; } }
/// <summary> /// Сформировать пакет по протоколу DSN /// </summary> /// <param name="device">номер устройства</param> private byte[] CreateDSNProtocol(int device, typeCRC CrcType) { byte[] packet = { 0x7E, 0x00, 0x0A, 0x02, 0x20, 0x03, 0xAA, 0x55, 0x55, 0x00, 0x00 }; packet[1] = (byte)device; switch (crcType) { case typeCRC.CycleOneByte: CalculateCycleOneByte(packet); break; case typeCRC.CycleTwoByte: CalculateCycleTwoByte(packet); break; case typeCRC.CRC16: ushort crc = CalculateCRC16(packet, packet.Length - 2); byte[] crc_byte = BitConverter.GetBytes(crc); packet[packet.Length - 2] = crc_byte[1]; packet[packet.Length - 1] = crc_byte[0]; break; } return packet; }
/// <summary> /// переключили тип контрольной суммы /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBoxTypeCrc_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxTypeCrc.SelectedIndex == 0) crcType = typeCRC.CRC16; else if (comboBoxTypeCrc.SelectedIndex == 1) crcType = typeCRC.CycleOneByte; else crcType = typeCRC.CycleTwoByte; }