daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Collections.Concurrent;
  13. using System.Threading;
  14.  
  15. namespace Rs232app
  16. {
  17.     public enum States : int
  18.     {
  19.         None, Begin, Length, Command, Data, ChecksumOne, ChecksumTwo
  20.     }
  21.  
  22.     public enum Send : int
  23.     {
  24.         Reset, Dl, Phy
  25.     }
  26.  
  27.     public partial class Form1 : Form
  28.     {
  29.         private SerialPort serialPort = new SerialPort();
  30.         private delegate void SerialReceivedEventDelegate(String message);
  31.         private Boolean isConnected = false;
  32.         private ConcurrentQueue<char> serialDataQueue = new ConcurrentQueue<char>();
  33.         private Boolean isStatusReceived = false;
  34.  
  35.         public Form1()
  36.         {
  37.             InitializeComponent();
  38.  
  39.             this.MaximizeBox = false;
  40.             this.MinimizeBox = false;
  41.             this.FormBorderStyle = FormBorderStyle.FixedDialog;
  42.             this.StartPosition = FormStartPosition.CenterScreen;
  43.  
  44.             PortCombo.Items.AddRange(SerialPort.GetPortNames());
  45.             PortCombo.SelectedIndex = PortCombo.Items.Count - 1;
  46.  
  47.             BaudCombo.Items.Add(57600);
  48.             BaudCombo.SelectedIndex = BaudCombo.Items.Count - 1;
  49.  
  50.             DataCombo.Items.Add(8);
  51.             DataCombo.SelectedIndex = BaudCombo.Items.Count - 1;
  52.  
  53.             ParityCombo.Items.Add(Parity.None);
  54.             ParityCombo.SelectedIndex = BaudCombo.Items.Count - 1;
  55.  
  56.             StopCombo.Items.Add(StopBits.One);
  57.             StopCombo.SelectedIndex = BaudCombo.Items.Count - 1;
  58.  
  59.             FlowCombo.Items.Add(Handshake.None);
  60.             FlowCombo.SelectedIndex = BaudCombo.Items.Count - 1;
  61.  
  62.             serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
  63.  
  64.             ConnectButton.BackColor = Color.Red;
  65.         }
  66.  
  67.         private void ConnectButton_Click(object sender, EventArgs e)
  68.         {
  69.             OutputText.Clear();
  70.  
  71.             if (isConnected)
  72.             {
  73.                 serialPort.Close();
  74.                 ConnectButton.Text = "Connect";
  75.                 ConnectButton.BackColor = Color.Red;
  76.                 isConnected = false;
  77.                 OutputGroup.Enabled = false;
  78.                 ResetGroup.Enabled = false;
  79.                 ModemGroup.Enabled = false;
  80.             }
  81.             else
  82.             {
  83.  
  84.                 serialPort.PortName = PortCombo.Text;
  85.                 serialPort.BaudRate = Convert.ToInt32(BaudCombo.Text);
  86.                 serialPort.DataBits = Convert.ToInt32(DataCombo.Text);
  87.                 serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), ParityCombo.Text);
  88.                 serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), StopCombo.Text);
  89.                 serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), FlowCombo.Text);
  90.  
  91.                 serialPort.ReadTimeout = 200;
  92.                 serialPort.WriteTimeout = 200;
  93.  
  94.                 serialPort.RtsEnable = false;
  95.                 serialPort.DtrEnable = true;
  96.  
  97.  
  98.                 serialPort.Open();
  99.  
  100.                 ConnectButton.Text = "Disconnect";
  101.                 ConnectButton.BackColor = Color.Green;
  102.                 isConnected = true;
  103.                 OutputGroup.Enabled = true;
  104.                 ResetGroup.Enabled = true;
  105.                 ModemGroup.Enabled = true;
  106.             }
  107.         }
  108.  
  109.         private void ResetButton_Click(object sender, EventArgs e)
  110.         {
  111.             SendData(Send.Reset);
  112.         }
  113.  
  114.         private void SendData(Send send)
  115.         {
  116.             serialPort.DtrEnable = true;
  117.             serialPort.RtsEnable = true;
  118.             Thread.Sleep(15);
  119.  
  120.             if (isStatusReceived)
  121.             {
  122.                 if(send == Send.Reset)
  123.                 {
  124.                     serialPort.Write(new byte[] { 0x02, 0x00, 0x3c, 0x3c, 0x00 }, 0, 5);
  125.                 } else if(send == Send.Dl)
  126.                 {
  127.                     serialPort.Write(new byte[] { 0x02, 0x02, 0x08, 0x00, 0x01, 0x0b, 0x00 }, 0, 7);
  128.                 } else if(send == Send.Phy)
  129.                 {
  130.                     serialPort.Write(new byte[] { 0x02, 0x02, 0x08, 0x00, 0x00, 0x0a, 0x00 }, 0, 7);
  131.                 }
  132.                
  133.                 isStatusReceived = false;
  134.  
  135.             }
  136.  
  137.             serialPort.RtsEnable = false;
  138.             serialPort.DtrEnable = true;
  139.         }
  140.  
  141.         private void DataReceivedHandler(
  142.                     object sender,
  143.                     SerialDataReceivedEventArgs e)
  144.         {
  145.             int bytesAvailable = serialPort.BytesToRead;
  146.  
  147.             char[] recBuf = new char[bytesAvailable];
  148.  
  149.             try
  150.             {
  151.                 serialPort.Read(recBuf, 0, bytesAvailable);
  152.  
  153.                 for (int index = 0; index < bytesAvailable; index++)
  154.                 {
  155.                     serialDataQueue.Enqueue(recBuf[index]);
  156.                 }
  157.  
  158.             }
  159.             catch (TimeoutException) { }
  160.             ReadSearialDataQueue();
  161.         }
  162.  
  163.         private void ReadSearialDataQueue()
  164.         {
  165.             String hexValue = String.Empty;
  166.             States status = States.None;
  167.             List<UInt16> checkSumList = new List<UInt16>();
  168.             List<UInt16> checkSumListTwo = new List<UInt16>();
  169.             Frame frame = new Frame();
  170.             UInt16 dataLeng = 0;
  171.  
  172.             String frameBegin = String.Empty;
  173.             String frameLength = String.Empty;
  174.             String frameCommand = String.Empty;
  175.             String frameCheckOne = String.Empty;
  176.             String frameCheckTwo = String.Empty;
  177.  
  178.             int counter = 0;
  179.  
  180.             try
  181.             {
  182.                 while (serialDataQueue.TryDequeue(out char ch))
  183.                 {
  184.                     UInt16 V = Convert.ToUInt16(ch);
  185.                     hexValue = String.Format("{0:x2}", V);
  186.  
  187.                     if (status == States.None && hexValue == "3f")
  188.                     {
  189.                         isStatusReceived = true;
  190.                     }
  191.  
  192.                     if (status == States.None && (hexValue == "02" || hexValue == "03"))
  193.                     {
  194.                         checkSumList.Clear();
  195.                         checkSumListTwo.Clear();
  196.                         counter = 0;
  197.                         dataLeng = 0;
  198.  
  199.                         frameBegin = String.Empty;
  200.                         frameLength = String.Empty;
  201.                         frameCommand = String.Empty;
  202.                         frameCheckOne = String.Empty;
  203.                         frameCheckTwo = String.Empty;
  204.  
  205.                         status = States.Begin;
  206.                         frameBegin = hexValue;
  207.                         serialPort.Write(new byte[] { 0x06 }, 0, 1);
  208.  
  209.                         continue;
  210.                     }
  211.  
  212.                     if (status == States.Begin)
  213.                     {
  214.                         status = States.Length;
  215.                         dataLeng = V;
  216.                         frameLength = hexValue;
  217.                         checkSumList.Add(V);
  218.  
  219.                         continue;
  220.                     }
  221.  
  222.                     if (status == States.Length)
  223.                     {
  224.                         status = States.Command;
  225.                         frameCommand = hexValue;
  226.                         checkSumList.Add(V);
  227.  
  228.                         continue;
  229.                     }
  230.  
  231.                     if (status == States.Command)
  232.                     {
  233.                         if (dataLeng != 0 && dataLeng != counter)
  234.                         {
  235.                             checkSumList.Add(V);
  236.                             counter++;
  237.                         }
  238.                         else
  239.                         {
  240.                             status = States.Data;
  241.                         }
  242.                     }
  243.  
  244.                     if (status == States.Data)
  245.                     {
  246.                         checkSumListTwo.Add(V);
  247.                         status = States.ChecksumOne;
  248.                         frameCheckOne = hexValue;
  249.  
  250.                         continue;
  251.                     }
  252.  
  253.                     if (status == States.ChecksumOne)
  254.                     {
  255.                         checkSumListTwo.Add(V);
  256.                         status = States.ChecksumTwo;
  257.                         frameCheckTwo = hexValue;
  258.  
  259.                         continue;
  260.                     }
  261.  
  262.                     if (status == States.ChecksumTwo)
  263.                     {
  264.                         Int32 sum = CheckSum(checkSumList);
  265.                         Int32 sum2 = CheckSum(checkSumListTwo);
  266.  
  267.                         if (sum == sum2)
  268.                         {
  269.                             status = (int)States.None;
  270.  
  271.                             frame.Begin = frameBegin;
  272.                             frame.Length = frameLength;
  273.                             frame.Command = frameCommand;
  274.                             frame.DataList = checkSumList;
  275.                             frame.CheckSumOne = frameCheckOne;
  276.                             frame.CheckSumTwo = frameCheckTwo;
  277.  
  278.                             this.OutputText.AppendText(frame.PrintFrame());
  279.                         }
  280.                         else
  281.                         {
  282.                             status = (int)States.None;
  283.                         }
  284.                         break;
  285.                     }
  286.                 }
  287.  
  288.                 OutputText.Text += "\n\n";
  289.  
  290.             }
  291.             catch (Exception e) { e.GetBaseException(); }
  292.         }
  293.  
  294.         private Int32 CheckSum(List<UInt16> checkSum)
  295.         {
  296.             Int32 returnValue = 0;
  297.  
  298.             foreach (UInt16 value in checkSum)
  299.             {
  300.                 returnValue += value;
  301.             }
  302.  
  303.             return returnValue;
  304.         }
  305.  
  306.         private void DlRadio_CheckedChanged(object sender, EventArgs e)
  307.         {
  308.             if (DlRadio.Checked)
  309.             {
  310.                 PhyRadio.Checked = false;
  311.                 SendData(Send.Dl);
  312.             }
  313.         }
  314.  
  315.         private void PhyRadio_CheckedChanged(object sender, EventArgs e)
  316.         {
  317.             if (PhyRadio.Checked)
  318.             {
  319.                 DlRadio.Checked = false;
  320.                 SendData(Send.Phy);
  321.             }
  322.         }
  323.     }
  324. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top