请教:我想把数据通过串口发送给下位机,现在有很多数据要同时发下去,不知道如何发送,如何能转换成一个byte类型的数组
我想发的数据有:int a = 123;
int b = 456;
int c = 1238546;
int d = 98742;
int e = 345;我该怎么做才能同时把这些发送出去?

解决方案 »

  1.   

                int[] datas = new int[]{123,456,1238546,98742,345};
                int intlength = 4;
                Byte[] bytes = new byte[intlength*5];
                int counter=0;
                foreach (int data in datas)
                {
                    byte[] intBytes = BitConverter.GetBytes(data);
                    bytes[counter] = intBytes[0];
                    counter++;
                    bytes[counter] = intBytes[1];
                    counter++;
                    bytes[counter] = intBytes[2];
                    counter++;
                    bytes[counter] = intBytes[3];
                    counter++;            }            serialPort1.Write(bytes, 0, bytes.Length);
      

  2.   

    你将数据通过Arraylist数组,然后用ArrayList中好像有一个ToByteArray之类方法去转换发送出去,希望能帮到你
      

  3.   

    需要例子的话给我留言,我给你发一个例子给你看,就是串口收发数据的,不过得等到晚上了;
    其实很简单的,现在又现成的控件;
    发送内容你可以直接作为16进制数据;
    也可以字符串的形式,利用ASCII.getbytes就可以转换为字节数据;