engr.ahayee
Member
- Joined
- Aug 10, 2021
- Messages
- 16
- Programming Experience
- Beginner
Hi everyone.
I am trying to read data from serial port and want to do some processing.
I have defined a function which should return the received data
following is the complete code
I am getting data from serial port as I mentioned in the following picture.
my question is how I return this data to testing_array?
I am trying to read data from serial port and want to do some processing.
I have defined a function which should return the received data
function:
testing_array = ReadHoldingRegisters(byte.Parse(txtSlaveAddr.Text), int.Parse(txtStartAddr.Text), int.Parse(txtLength.Text));
following is the complete code
program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace _485_Communication
{
public partial class Form1 : Form
{
public SerialPort MBPort = new SerialPort();
bool ConnectedStatus = false;
byte[] testing_array;
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
MBPort = new SerialPort(txtCommPort.Text);
MBPort.BaudRate = int.Parse(cbBaudRate.Text);
if (cbParity.Text == "None") MBPort.Parity = System.IO.Ports.Parity.None;
else if (cbParity.Text == "Even") MBPort.Parity = System.IO.Ports.Parity.Even;
else if (cbParity.Text == "Odd") MBPort.Parity = System.IO.Ports.Parity.Odd;
try
{
if (MBPort.IsOpen) MBPort.Close();
MBPort.Open();
lblStatus.Text = "Connected";
lblStatus.ForeColor = Color.Green;
ConnectedStatus = true;
}
catch (Exception ex)
{
lblStatus.Text = "Error!" + ex.ToString();
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
MBPort.Close();
lblStatus.Text = "Disconnected";
lblStatus.ForeColor = Color.Red;
ConnectedStatus = false;
}
private void btnRead_Click(object sender, EventArgs e)
{
if(ConnectedStatus)
{
try
{
testing_array = ReadHoldingRegisters(byte.Parse(txtSlaveAddr.Text), int.Parse(txtStartAddr.Text), int.Parse(txtLength.Text));
}
catch(Exception ex)
{
lblStatus.Text = "Error!" + ex.ToString();
}
}
}
public byte[] ReadHoldingRegisters(byte SlaveAddr, int StartAddr, int Length)
{
byte[] frame = new byte[8];
byte[] return_data = new byte[2];
frame[0] = SlaveAddr;
frame[1] = 3;
frame[2] = (byte)(StartAddr >> 8);
frame[3] = (byte)StartAddr;
frame[4] = (byte)(Length >> 8);
frame[5] = (byte)Length;
byte[] CRC = CalculateCRC(frame);
frame[frame.Length - 2] = CRC[0];
frame[frame.Length - 1] = CRC[1];
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
{
MBPort.Write(frame, 0, frame.Length);
Thread.Sleep(100);
if (MBPort.BytesToRead >= 5)
{
byte[] bufferReceiver = new byte[MBPort.BytesToRead];
MBPort.Read(bufferReceiver, 0, MBPort.BytesToRead);
MBPort.DiscardInBuffer();
byte[] data = new byte[bufferReceiver.Length - 5];
Array.Copy(bufferReceiver, 3, data, 0, data.Length);
}
}));
}
catch (Exception ex)
{
throw ex;
}
return return_data;
}
public byte[] CalculateCRC(byte[] data)
{
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
byte[] CRC = new byte[2];
for (int i = 0; i < (data.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^ data[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
return CRC;
}
}
}
I am getting data from serial port as I mentioned in the following picture.
my question is how I return this data to testing_array?