Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Threading;
- namespace RS232
- {
- public partial class Form1 : Form
- {
- private SerialPort comPort = new SerialPort();
- private delegate void SerialReceivedEventDelegate(String message);
- private bool isConnected = false;
- public ConcurrentQueue<char> serialDataQueue = new ConcurrentQueue<char>();
- public Form1()
- {
- InitializeComponent();
- //string[] comPortNames = null;
- //comPortNames = SerialPort.GetPortNames();
- //foreach (string coms in comPortNames)
- //{
- // comboBoxComPorts.Items.Add(coms);
- //}
- comboBoxComPorts.Items.AddRange(SerialPort.GetPortNames());
- comboBoxComPorts.SelectedIndex = comboBoxComPorts.Items.Count - 1;
- comboBoxBaudrate.Items.Add(57600);
- comboBoxBaudrate.SelectedIndex = comboBoxBaudrate.Items.Count - 1;
- comboBoxDataBits.Items.Add(8);
- comboBoxDataBits.SelectedIndex = comboBoxDataBits.Items.Count - 1;
- comboBoxParity.Items.Add(Parity.None);
- comboBoxParity.SelectedIndex = comboBoxParity.Items.Count - 1;
- comboBoxStopBits.Items.Add(StopBits.One);
- comboBoxStopBits.SelectedIndex = comboBoxStopBits.Items.Count - 1;
- comboBoxHandshake.Items.Add(Handshake.None);
- comboBoxHandshake.SelectedIndex = comboBoxHandshake.Items.Count - 1;
- comPort.DataReceived += new SerialDataReceivedEventHandler(dataReceived);
- }
- private void buttonConnect_Click(object sender, EventArgs e)
- {
- richTextBoxIncomingData.Clear();
- if (isConnected == false)
- {
- comPort.PortName = comboBoxComPorts.Text;
- comPort.BaudRate = Convert.ToInt32(comboBoxBaudrate.Text);
- comPort.DataBits = Convert.ToInt32(comboBoxDataBits.Text);
- comPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text);
- comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.Text);
- comPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), comboBoxHandshake.Text);
- comPort.ReadTimeout = 200;
- comPort.WriteTimeout = 200;
- comPort.RtsEnable = false;
- comPort.DtrEnable = true;
- comPort.Open();
- isConnected = true;
- labelConnected.BackColor = Color.Green;
- labelConnected.Text = "CONNECTED";
- buttonConnect.Text = "DISCONNECT";
- }
- else if (isConnected == true)
- {
- isConnected = false;
- labelConnected.Text = "DISCONNECTED";
- labelConnected.BackColor = Color.Red;
- buttonConnect.Text = "CONNECT";
- comPort.Close();
- }
- }
- private void dataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- //inputData = comPort.ReadExisting();
- //hexInputData = toHex(inputData);
- //if (inputData != String.Empty)
- //{
- // this.BeginInvoke(new SetTextCallback(setText), new object[] { hexInputData });
- //}
- int bytesAvailable = comPort.BytesToRead;
- //Console.WriteLine(bytesAvailable);
- char[] recBuf = new char[bytesAvailable];
- try
- {
- comPort.Read(recBuf, 0, bytesAvailable);
- for (int index = 0; index < bytesAvailable; index++)
- {
- serialDataQueue.Enqueue(recBuf[index]);
- }
- }
- catch (TimeoutException) { }
- }
- //public string toHex(string inputDataText)
- //{
- // string output = string.Empty;
- // char[] values = inputData.ToCharArray();
- // foreach (char v in values)
- // {
- // int val = Convert.ToInt32(v);
- // output += string.Format("0x{0:x2} ", val);
- // }
- // return output;
- //}
- //private void setText(string text)
- //{
- // this.richTextBoxIncomingData.Text += text;
- //}
- private void readSerialDataQueue()
- {
- String hexVal = String.Empty;
- //stan = 0 -> empty | 1 -> begin | 2 -> length | 3 -> cmd code | 4 -> data | 5 -> checksum
- int stan = 0;
- Int32 begin = 0;
- Int32 length = 0;
- Int32 cmd = 0;
- Int32 data = 0;
- Int32 checksumOne = 0;
- Int32 checksumTwo = 0;
- List<Int32> dataList = new List<Int32>();
- Int32 dataSum = 0;
- Int32 suma = 0;
- try
- {
- while (serialDataQueue.TryDequeue(out char ch))
- {
- int val = Convert.ToInt32(ch);
- hexVal = String.Format("{0:x2}", val);
- //poczatek ramki 02 lub 03
- if (stan == 0 && (hexVal == "02" || hexVal == "03"))
- {
- begin = val;
- this.richTextBoxIncomingData.Text += hexVal;
- stan = 1;
- continue;
- }
- //length 0-255
- if (stan == 1)
- {
- length = val;
- this.richTextBoxIncomingData.Text += hexVal;
- stan = 2;
- continue;
- }
- //command code 0-FFh
- if (stan == 2)
- {
- cmd = val;
- this.richTextBoxIncomingData.Text += hexVal;
- stan = 3;
- continue;
- }
- //data 0-255 Bytes
- if (stan == 3)
- {
- data = val;
- //dataList.Add(Convert.ToInt32(hexVal));
- this.richTextBoxIncomingData.Text += hexVal;
- foreach(Int32 d in dataList){
- dataSum += d;
- Console.WriteLine(d + " suma: " + dataSum + " maska1: " + (dataSum & 0xFF00) + " maska2: " + (dataSum & 0x00FF));
- }
- checksumOne = dataSum & 0xFF00;
- checksumTwo = dataSum & 0x00FF;
- Console.WriteLine(checksumOne + " " + checksumTwo
- + " string:" + Convert.ToString(checksumOne) + " " + Convert.ToString(checksumTwo)
- + " hexVal:" + hexVal);
- if (data == checksumOne)
- {
- stan = 4;
- }
- continue;
- }
- //checksum 2B (B - Byte)
- if (stan == 4 && val == checksumOne)
- {
- checksumOne = val;
- Console.WriteLine("checksumOne: " + checksumOne);
- this.richTextBoxIncomingData.Text += Convert.ToString(checksumOne);
- stan = 5;
- continue;
- }
- if(stan == 5 && val == checksumTwo)
- {
- checksumTwo = val;
- Console.WriteLine("checksumTwo: " + checksumTwo);
- this.richTextBoxIncomingData.Text += Convert.ToString(checksumTwo);
- stan = 0;
- break;
- }
- if (stan != 0 || stan != 4 || stan != 5)
- {
- //dataList.Add(Convert.ToInt32(hexVal));
- dataList.Add(val);
- Console.WriteLine("Lista: " + val);
- }
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.StackTrace); }
- }
- private void buttonReset_Click(object sender, EventArgs e)
- {
- richTextBoxIncomingData.Clear();
- comPort.RtsEnable = true;
- comPort.DtrEnable = true;
- //comPort.ReadTimeout = 200;
- comPort.Write(new byte[] { 0x02, 0x00, 0x3C, 0x3C, 0x00 }, 0, 5);
- readSerialDataQueue();
- comPort.RtsEnable = false;
- comPort.DtrEnable = true;
- }
- }
- }
- //02, 09, 0900, byte do ustawienia 9 albo 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement