Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Threading;
  14.  
  15. namespace RS232
  16. {
  17.  
  18.     public partial class Form1 : Form
  19.     {
  20.         private SerialPort comPort = new SerialPort();
  21.         private delegate void SerialReceivedEventDelegate(String message);
  22.         private bool isConnected = false;
  23.         public ConcurrentQueue<char> serialDataQueue = new ConcurrentQueue<char>();
  24.  
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.            
  29.             //string[] comPortNames = null;
  30.             //comPortNames = SerialPort.GetPortNames();
  31.             //foreach (string coms in comPortNames)
  32.             //{
  33.             //    comboBoxComPorts.Items.Add(coms);
  34.             //}
  35.  
  36.             comboBoxComPorts.Items.AddRange(SerialPort.GetPortNames());
  37.             comboBoxComPorts.SelectedIndex = comboBoxComPorts.Items.Count - 1;
  38.            
  39.             comboBoxBaudrate.Items.Add(57600);
  40.             comboBoxBaudrate.SelectedIndex = comboBoxBaudrate.Items.Count - 1;
  41.  
  42.             comboBoxDataBits.Items.Add(8);
  43.             comboBoxDataBits.SelectedIndex = comboBoxDataBits.Items.Count - 1;
  44.  
  45.             comboBoxParity.Items.Add(Parity.None);
  46.             comboBoxParity.SelectedIndex = comboBoxParity.Items.Count - 1;
  47.  
  48.             comboBoxStopBits.Items.Add(StopBits.One);
  49.             comboBoxStopBits.SelectedIndex = comboBoxStopBits.Items.Count - 1;
  50.  
  51.             comboBoxHandshake.Items.Add(Handshake.None);
  52.             comboBoxHandshake.SelectedIndex = comboBoxHandshake.Items.Count - 1;
  53.  
  54.             comPort.DataReceived += new SerialDataReceivedEventHandler(dataReceived);
  55.         }
  56.  
  57.         private void buttonConnect_Click(object sender, EventArgs e)
  58.         {
  59.             richTextBoxIncomingData.Clear();
  60.  
  61.             if (isConnected == false)
  62.             {
  63.                 comPort.PortName = comboBoxComPorts.Text;
  64.                 comPort.BaudRate = Convert.ToInt32(comboBoxBaudrate.Text);
  65.                 comPort.DataBits = Convert.ToInt32(comboBoxDataBits.Text);
  66.                 comPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text);
  67.                 comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.Text);
  68.                 comPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), comboBoxHandshake.Text);
  69.              
  70.                 comPort.ReadTimeout = 200;
  71.                 comPort.WriteTimeout = 200;
  72.  
  73.                 comPort.RtsEnable = false;
  74.                 comPort.DtrEnable = true;
  75.  
  76.                 comPort.Open();
  77.  
  78.                 isConnected = true;
  79.                 labelConnected.BackColor = Color.Green;
  80.                 labelConnected.Text = "CONNECTED";
  81.                 buttonConnect.Text = "DISCONNECT";
  82.             }
  83.             else if (isConnected == true)
  84.             {
  85.                 isConnected = false;
  86.                 labelConnected.Text = "DISCONNECTED";
  87.                 labelConnected.BackColor = Color.Red;
  88.                 buttonConnect.Text = "CONNECT";
  89.                 comPort.Close();
  90.             }
  91.         }
  92.  
  93.         private void dataReceived(object sender, SerialDataReceivedEventArgs e)
  94.         {
  95.  
  96.             //inputData = comPort.ReadExisting();
  97.             //hexInputData = toHex(inputData);
  98.             //if (inputData != String.Empty)
  99.             //{
  100.             //    this.BeginInvoke(new SetTextCallback(setText), new object[] { hexInputData });
  101.             //}
  102.  
  103.             int bytesAvailable = comPort.BytesToRead;
  104.             //Console.WriteLine(bytesAvailable);
  105.  
  106.             char[] recBuf = new char[bytesAvailable];
  107.  
  108.             try
  109.             {
  110.                 comPort.Read(recBuf, 0, bytesAvailable);
  111.  
  112.                 for (int index = 0; index < bytesAvailable; index++)
  113.                 {
  114.                     serialDataQueue.Enqueue(recBuf[index]);
  115.                 }
  116.             }
  117.             catch (TimeoutException) { }
  118.  
  119.         }
  120.  
  121.         //public string toHex(string inputDataText)
  122.         //{
  123.         //    string output = string.Empty;
  124.         //    char[] values = inputData.ToCharArray();
  125.         //    foreach (char v in values)
  126.         //    {
  127.         //        int val = Convert.ToInt32(v);
  128.         //        output += string.Format("0x{0:x2} ", val);
  129.         //    }
  130.  
  131.         //    return output;
  132.         //}
  133.  
  134.         //private void setText(string text)
  135.         //{
  136.         //    this.richTextBoxIncomingData.Text += text;
  137.         //}
  138.  
  139.         private void readSerialDataQueue()
  140.         {
  141.             String hexVal = String.Empty;
  142.             //stan = 0 -> empty | 1 -> begin | 2 -> length | 3 -> cmd code | 4 -> data | 5 -> checksum
  143.             int stan = 0;
  144.             Int32 begin = 0;
  145.             Int32 length = 0;
  146.             Int32 cmd = 0;
  147.             Int32 data = 0;
  148.             Int32 checksumOne = 0;
  149.             Int32 checksumTwo = 0;
  150.             List<Int32> dataList = new List<Int32>();
  151.             Int32 dataSum = 0;
  152.             Int32 suma = 0;
  153.            
  154.             try
  155.             {
  156.                 while (serialDataQueue.TryDequeue(out char ch))
  157.                 {
  158.                     int val = Convert.ToInt32(ch);
  159.                     hexVal = String.Format("{0:x2}", val);
  160.  
  161.                     //poczatek ramki 02 lub 03
  162.                     if (stan == 0 && (hexVal == "02" || hexVal == "03"))
  163.                     {
  164.                         begin = val;
  165.                         this.richTextBoxIncomingData.Text += hexVal;
  166.                         stan = 1;
  167.                         continue;
  168.                     }
  169.                     //length 0-255
  170.                     if (stan == 1)
  171.                     {
  172.                         length = val;
  173.                         this.richTextBoxIncomingData.Text += hexVal;
  174.                         stan = 2;
  175.                         continue;
  176.                     }
  177.                     //command code 0-FFh
  178.                     if (stan == 2)
  179.                     {
  180.                         cmd = val;
  181.                         this.richTextBoxIncomingData.Text += hexVal;
  182.                         stan = 3;
  183.                         continue;
  184.                     }
  185.                     //data 0-255 Bytes
  186.                     if (stan == 3)
  187.                     {
  188.                         data = val;
  189.                         //dataList.Add(Convert.ToInt32(hexVal));
  190.                         this.richTextBoxIncomingData.Text += hexVal;
  191.                         foreach(Int32 d in dataList){
  192.                             dataSum += d;
  193.                             Console.WriteLine(d + " suma: " + dataSum + " maska1: " + (dataSum & 0xFF00) + " maska2: " + (dataSum & 0x00FF));
  194.                         }
  195.  
  196.                         checksumOne = dataSum & 0xFF00;
  197.                         checksumTwo = dataSum & 0x00FF;
  198.  
  199.                         Console.WriteLine(checksumOne + " " + checksumTwo
  200.                             + " string:" + Convert.ToString(checksumOne) + " " + Convert.ToString(checksumTwo)
  201.                             + " hexVal:" + hexVal);
  202.  
  203.                         if (data == checksumOne)
  204.                         {
  205.                             stan = 4;
  206.                         }
  207.                         continue;
  208.                     }
  209.                     //checksum 2B (B - Byte)
  210.                     if (stan == 4 && val == checksumOne)
  211.                     {
  212.                         checksumOne = val;
  213.                         Console.WriteLine("checksumOne: " + checksumOne);
  214.                         this.richTextBoxIncomingData.Text += Convert.ToString(checksumOne);
  215.                         stan = 5;
  216.                         continue;
  217.                     }
  218.                     if(stan == 5 && val == checksumTwo)
  219.                     {
  220.                         checksumTwo = val;
  221.                         Console.WriteLine("checksumTwo: " + checksumTwo);
  222.                         this.richTextBoxIncomingData.Text += Convert.ToString(checksumTwo);
  223.                         stan = 0;
  224.                         break;
  225.                     }
  226.  
  227.                     if (stan != 0 || stan != 4 || stan != 5)
  228.                     {
  229.                         //dataList.Add(Convert.ToInt32(hexVal));
  230.                         dataList.Add(val);
  231.                         Console.WriteLine("Lista: " + val);
  232.                        
  233.                     }
  234.  
  235.                 }
  236.  
  237.             }
  238.             catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
  239.         }
  240.  
  241.         private void buttonReset_Click(object sender, EventArgs e)
  242.         {
  243.             richTextBoxIncomingData.Clear();
  244.             comPort.RtsEnable = true;
  245.             comPort.DtrEnable = true;
  246.  
  247.             //comPort.ReadTimeout = 200;
  248.             comPort.Write(new byte[] { 0x02, 0x00, 0x3C, 0x3C, 0x00 }, 0, 5);
  249.             readSerialDataQueue();
  250.  
  251.             comPort.RtsEnable = false;
  252.             comPort.DtrEnable = true;
  253.         }
  254.  
  255.     }
  256.  
  257. }
  258.  
  259.  
  260. //02, 09, 0900, byte do ustawienia 9 albo 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement