|
@@ -0,0 +1,187 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.IO.Ports;
|
|
|
+using System.Threading;
|
|
|
+
|
|
|
+namespace Tps_LQ_Transmitter.com
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// PCM控制与数据发送类
|
|
|
+ /// </summary>
|
|
|
+ class PcmControl:MySerial
|
|
|
+ {
|
|
|
+ SerialPort serial = null;
|
|
|
+ public PcmControl()
|
|
|
+ {
|
|
|
+ serial = new SerialPort();
|
|
|
+ serial.PortName = "COM5"; //DIO控制板与JSY模块都是RS485通信共用计算机的串口COM6
|
|
|
+ serial.BaudRate = 115200;
|
|
|
+ serial.DataBits = 8;
|
|
|
+ serial.StopBits = StopBits.One;//停止位设置为com_stop的值
|
|
|
+ serial.Parity = Parity.None;//获取奇偶校验选项的值
|
|
|
+ serial.ReadTimeout = 1000; //读取等待时间1000
|
|
|
+ serial.RtsEnable = true;
|
|
|
+ if (!serial.IsOpen)
|
|
|
+ {
|
|
|
+ serial.Open();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void portClose()
|
|
|
+ {
|
|
|
+ serial.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// PCM读写方法
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="data">数据</param>
|
|
|
+ /// <param name="delay">延时</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public byte[] WriteRead(byte[] data, int delay = 50)
|
|
|
+ {
|
|
|
+ if (data == null)
|
|
|
+ return null;
|
|
|
+
|
|
|
+ //先读空
|
|
|
+ if (serial.BytesToRead > 0)
|
|
|
+ serial.DiscardInBuffer();
|
|
|
+
|
|
|
+ byte[] crc = crc16(data);
|
|
|
+ byte[] newarray = new byte[data.Length + 2];
|
|
|
+ Array.Copy(data, newarray, data.Length);
|
|
|
+ newarray[data.Length] = crc[0];
|
|
|
+ newarray[data.Length + 1] = crc[1];
|
|
|
+
|
|
|
+ serial.Write(data, 0, data.Length);
|
|
|
+ Thread.Sleep(delay);
|
|
|
+ if (serial.BytesToRead > 0)
|
|
|
+ {
|
|
|
+ byte[] dat = new byte[serial.BytesToRead];
|
|
|
+ serial.Read(dat, 0, dat.Length);
|
|
|
+ return dat;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// PCM设置命令发送
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="mode">"UART"/"HDLC"</param>
|
|
|
+ /// <param name="choice">"ODD"/"EVEN"/"NONE"/"FALL"/"RISE"</param>
|
|
|
+ /// <param name="baudrate">波特率</param>
|
|
|
+ /// <returns>结果</returns>
|
|
|
+ public bool Setting(string mode,string choice,int baudrate )
|
|
|
+ {
|
|
|
+ int u = 0;
|
|
|
+ if (mode=="UART")
|
|
|
+ {
|
|
|
+ u = u | 0x01;
|
|
|
+ }
|
|
|
+ else if (mode == "HDLC")
|
|
|
+ {
|
|
|
+ u = u | 0x41;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (choice == "ODD")//奇校验
|
|
|
+ {
|
|
|
+ u = u | 0x02;
|
|
|
+ }
|
|
|
+ else if (choice == "EVEN")//偶校验
|
|
|
+ {
|
|
|
+ u = u | 0x04;
|
|
|
+ }
|
|
|
+ else if (choice == "NONE")//无校验
|
|
|
+ {
|
|
|
+ u = u | 0x00;
|
|
|
+ }
|
|
|
+ else if (choice == "FALL")//下降沿
|
|
|
+ {
|
|
|
+ u = u | 0x08;
|
|
|
+ }
|
|
|
+ else if (choice == "RISE")//上升沿
|
|
|
+ {
|
|
|
+ u = u | 0x00;
|
|
|
+ }
|
|
|
+ int d =((50000000 / baudrate) + 2);
|
|
|
+ bool bolrd = false;
|
|
|
+ byte[] byst = { 0xF0, 0x0F, 0x02, 0x00, 0x00, 0x03, 0x00, 0xDB, 0x01 };
|
|
|
+ byst[8] = (byte)u;
|
|
|
+ byst[6] = (byte)((d & 0x0000ff00) >> 8);
|
|
|
+ byst[7] = (byte)(d & 0x000000ff);
|
|
|
+ byte[] byrd = WriteRead(byst);
|
|
|
+ if (byrd.Length>= byst.Length)
|
|
|
+ {
|
|
|
+ bolrd = true;
|
|
|
+ }
|
|
|
+ return bolrd;
|
|
|
+ }
|
|
|
+ public bool UartSendSetting()
|
|
|
+ {
|
|
|
+ bool bolrd = false;
|
|
|
+ byte[] byst = { 0xF0, 0x0F, 0x02, 0x00, 0x00, 0x03, 0x00, 0xDB, 0x01 };
|
|
|
+ byte[] byrd = new byte[10];
|
|
|
+ byte[] byrdx = { 0xF0, 0x0F, 0x02, 0x00, 0x00, 0x03, 0x00, 0xDB, 0x01 ,0x24};
|
|
|
+ byrd=WriteRead(byst);
|
|
|
+ if (byrd.Length >= 10)
|
|
|
+ {
|
|
|
+ bolrd = true;
|
|
|
+ for (int i = 0; i < byrdx.Length; i++)
|
|
|
+ {
|
|
|
+ if (byrd[i] != byrdx[i])
|
|
|
+ bolrd = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bolrd;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool HdlcSendSetting()
|
|
|
+ {
|
|
|
+ bool bolrd = false;
|
|
|
+ byte[] byst = { 0xF0, 0x0F, 0x02, 0x00, 0x00, 0x03, 0x00, 0x0D, 0x41 };
|
|
|
+ byte[] byrd = new byte[10];
|
|
|
+ byte[] byrdx = { 0xF0, 0x0F, 0x02, 0x00, 0x00, 0x03, 0x00, 0x0D, 0x41, 0xB2 };
|
|
|
+ byrd = WriteRead(byst);
|
|
|
+ if (byrd.Length >= 10)
|
|
|
+ {
|
|
|
+ bolrd = true;
|
|
|
+ for (int i = 0; i < byrdx.Length; i++)
|
|
|
+ {
|
|
|
+ if (byrd[i] != byrdx[i])
|
|
|
+ bolrd = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bolrd;
|
|
|
+ }
|
|
|
+ public bool DataSend(byte[] data)
|
|
|
+ {
|
|
|
+ bool bolrd = false;
|
|
|
+ if (data!=null && data.Length>0)
|
|
|
+ {
|
|
|
+ byte[] sd = new byte[data.Length + 6];
|
|
|
+ sd[0] = 0xF0;
|
|
|
+ sd[1] = 0x0F;
|
|
|
+ sd[2] = 0x03;
|
|
|
+ sd[3] = 0x00;
|
|
|
+ sd[4] = 0x00;
|
|
|
+ sd[5] = (byte)data.Length;
|
|
|
+ for (int i = 0; i < data.Length; i++)
|
|
|
+ {
|
|
|
+ sd[6 + i] = data[i];
|
|
|
+ }
|
|
|
+ byte[] byrd = WriteRead(sd);
|
|
|
+ if (byrd.Length >= sd.Length)
|
|
|
+ {
|
|
|
+ bolrd = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bolrd;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|