0

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?

burak
  • 51
  • 5
  • Is this a Raspberry Pi project hence the serial comms? If not, serial communications should be avoided in contemporary apps due to the complexity in establishing a simple link (both parties have to use the same _baud, bits, stop, flow control_ and know this **ahead of time**). Consider using TCP, named pipes, REST, WCF etc instead –  Jul 01 '21 at 09:57
  • The process of transferring data from two windows forms applications from one application to another. I have a weighing application, I need to print the value from there to the appropriate places in the other form. – burak Jul 01 '21 at 10:28
  • where exactly does the error occur? – MakePeaceGreatAgain Jul 01 '21 at 10:39
  • The error is right here: string a = v.weight; can't get the value in weight – burak Jul 01 '21 at 11:39

1 Answers1

0

Check your "weight" varible value, it comes allways string.Empty, becouse you are creating always new instance of Tarti class(form) here:

Tarti v = new Tarti();
v.Hide();
string a = v.weight;

string[] split = a.Split('.'); - array elements count in this case - zero, for this you are getting "Index was outside the bounds of the array" error

Mansur Kurtov
  • 726
  • 1
  • 4
  • 12
  • Yes, you're right Sir, the value is empty, but let me explain, I print the data I got from the scale application on the screen in the other form, and as the value changes, a new form should open so that it can get the new value. https://i.ibb.co/KGTP1Nz/Screenshot-1.png When I say get data here, it has to go to that form all the time. How do you think I can fix this, I'm new to coding, I couldn't find my problem elsewhere. – burak Jul 01 '21 at 10:26
  • i created for you sample project, (https://github.com/MansurKurtov/MyWindowsFormsApp1) – Mansur Kurtov Jul 01 '21 at 11:00
  • Sir, there is pressure on me to do this job quickly, I uploaded it to the drive, I would be very happy if you could take care of it.https://drive.google.com/drive/folders/1Nzj8gkEHMyEMDYs1JppfHvFjDMGTWcqd?usp=sharing – burak Jul 01 '21 at 11:16
  • this link not accessable for me – Mansur Kurtov Jul 01 '21 at 11:25
  • I fixed it Sir i am sorry. https://drive.google.com/drive/folders/1Nzj8gkEHMyEMDYs1JppfHvFjDMGTWcqd?usp=sharing – burak Jul 01 '21 at 11:37
  • write me to my gmail pls] – Mansur Kurtov Jul 01 '21 at 12:07
  • Sorry i was still trying to fix it i didn't see it Sir – burak Jul 01 '21 at 12:10
  • Updated By Mansur Kurtov: Changes were made in 2 places. The first is on the data sender side. Second, where data is taken Tarti v = new Tarti(); v.ShowDialog(); if (v.DialogResult == DialogResult.OK) { string weight = v.weight; string[] weightSplit = weight.Split('.'); if (weightSplit.Length == 0) return; PackingReal.Text = weightSplit[0].ToString(); PackingDecimal.Text = weightSplit[1].ToString(); – burak Jul 01 '21 at 13:26