I have one scale app. I want to pass the value to another form place but I am getting an error. I just graduated, I don't know much, unfortunately, can you help me?
My weighing app:
namespace Tartı
{
public partial class Tarti : Form
{
public SerialPort _serialPort = null;
public Tarti()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
_serialPort = new SerialPort("COM4", 9600, Parity.None, 8);
_serialPort.StopBits = StopBits.One;
_serialPort.ReadTimeout = 1000;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_serialPort.Open();
}
public string weight = "";
private delegate void Closure();
private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
string str = System.Text.Encoding.UTF8.GetString(data);
double number;
if (Double.TryParse(str, out number))
{
txtWeight.Text = string.Format("{0:0.000}", str);
}
else
{
var doubleArray = Regex.Split(str, @"[^0-9\.]+")
.Where(c => c != "." && c.Trim() != "");
string[] str1 = ((System.Collections.IEnumerable)doubleArray)
.Cast<object>()
.Select(x => x.ToString())
.ToArray();
if (str1 != null && str1.Length > 0)
{
txtWeight.Text = string.Format("{0:0.000}", str1[0]);
txtWeight.ForeColor = Color.Red;
weight = txtWeight.Text;
_serialPort.Close();
}
}
}
private void Tarti_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
Other form application data needs to come to this application:
private void AgirlikAl_Click(object sender, EventArgs e)
{
Tarti v = new Tarti();
v.Hide();
string a = v.weight;
string[] split = a.Split('.');
PackingReal.Text = split[0].ToString();
PackingDecimal.Text = split[1].ToString();
}
It gives the following message as an error: Additional information: Index was outside the bounds of the array. How can i fix this?