0

I have b1 and b2 which are all in byte, I am using serialport.write to send them separately. Can I send them at the same time? As in using one command instead of two. My existing code:

   private void SConvert_Click(object sender, EventArgs e)
    {
        byte[] b1 = null, b2 = null;
        string[] coords = textBox1.Text.Split('\n');
        for (int i = 0; i <= coords.Length - 1; i++)
        {
            if (coords[i].Length > 0)
            {
                GetValue(coords[i], out b1, out b2);
            }
            if (serialPort.IsOpen)
            {
                serialPort.Write(b1, 0, 4);
                serialPort.Write(b2, 0, 4);
            }
        }

    }
    private void GetValue(string strValue, out byte[] b1, out byte[] b2)
    {
        string S1, S2, S = strValue;
        string[] x = S.Split(',');
        string y = x[0].ToString();//{lat=-36.123333          
        string z = x[1].ToString();//lng=174.333333}        // index outside bounds of the array
        S1 = y.Substring(y.IndexOf('=') + 1);
        string z1 = z.Replace("}", "0");                    // replace } by 0 since do not know length of }
        S2 = z1.Substring(z1.IndexOf('=') + 1);
        float f1 = float.Parse(S1), f2 = float.Parse(S2);
        b1 = System.BitConverter.GetBytes(f1);
        b2 = System.BitConverter.GetBytes(f2);
    }
myway1989
  • 9
  • 4
  • 2
    by same time, you mean to merge them to one byte? or to send both sequentially in a single method call? – pushpraj Aug 22 '14 at 10:14
  • sry I did not make myself clear. I do not want to reduce the size of b1 and b2, just combine these two commands into one. Thanks for your reminding. – myway1989 Aug 22 '14 at 10:16
  • How can your code be possible? Which are the type of both `b1` and `b2`, `byte` or `byte[]`? According to [MSDN](http://msdn.microsoft.com/ko-kr/library/system.io.ports.serialport.write.aspx), they should be `byte[]`. – ikh Aug 22 '14 at 10:17
  • ..Are you trying to send `null`? – ikh Aug 22 '14 at 10:20
  • @ikh I post my full code now, cheers:) – myway1989 Aug 22 '14 at 10:22
  • @myway1989 In this case, you just need to provide a simple and meaningful example such as `byte[] b1, b2; /* init b1, b2 */ serialPort.Write(b1, 0, 4); serialPort.Write(b2, 0, 4);` Anyway, I've answered before you edit your question >o – ikh Aug 22 '14 at 10:25

2 Answers2

2

Instead of

serialPort.Write(b1, 0, 4);
serialPort.Write(b2, 0, 4);

you could do just

serialPort.Write(b1.Concat(b2).ToArray(), 0, b1.Length + b2.Length);
AlexD
  • 32,156
  • 3
  • 71
  • 65
0

I assume you'd like to send two byte arrays at once. Solution is simple: merge them, and send it.

byte[] buf = new byte[b1.Length + b2.Length];
Array.Copy(buf, 0, b1);
Array.Copy(buf, 0, b2, b1.Length, b2.Length);
serialPort.Write(buf, 0, buf.Length);

See also: Merging two arrays in .NET

Community
  • 1
  • 1
ikh
  • 10,119
  • 1
  • 31
  • 70
  • @myway1989 If you get an answer of your question, you can *upvote* good answers by clicking up-triangle beside the body of answer, or *choose* the answer which let you solve the problem by clicking the check button. – ikh Aug 22 '14 at 10:57
  • @myway1989 Hm? can't you *choose* either? – ikh Aug 23 '14 at 03:40