解决方案 »

  1.   

    sendData类型是string,他会comm.Write(sendData);通过串口发送到下位机。大侠们能不能给个列子,谢谢了
      

  2.   

    把你需要发送的数据 第一步 肯定是封装··
    而封装的规则 就是按照他给你的那个结构:
    eag:
    public calss SendData{
     public byte head = 0x24;
     public byte heandNumber;
     ......
    public byte[] data = new byte[7]; // 数据部分
    public byte[] crc = new byte[2]; // crc
    pulic byte end = 0x7e;
    pulic byte[] ToArray(){
     using(MemoryStream ms = new MemoryStream()) {
          using(BinaryWriter bw = new BinaryWriter(ms)) {
                    bw.Write(this.head);
                    .....
                    bw.Write(this.end);

    }
    }
    }发送的时候 直接SendData.ToArray() 发送byte数据过去啊··
      

  3.   

    Quote: 引用 4 楼 yuekunge 的回复:

    把你需要发送的数据 第一步 肯定是封装··
    而封装的规则 就是按照他给你的那个结构:
    eag:
    public calss SendData{
     public byte head = 0x24;
     public byte heandNumber;
     ......
    public byte[] data = new byte[7]; // 数据部分
    public byte[] crc = new byte[2]; // crc
    pulic byte end = 0x7e;
    pulic byte[] ToArray(){
     using(MemoryStream ms = new MemoryStream()) {
          using(BinaryWriter bw = new BinaryWriter(ms)) {
                    bw.Write(this.head);
                    .....
                    bw.Write(this.end);

    }
    }
    }
    大侠上面这段我是很清楚的,问题就在你写的下面这段ToArray
    public class SendData
            {
                public const byte head = 0x24;
                public byte headNum;
                public byte comHaedNum;
                public byte length;
                public byte comLength;
                public byte commandWord;
                public byte[] data = new byte[7];
                public byte[] crc = new byte[2];
                public const byte end = 0x7e;
            }
    请大侠再指点下~谢谢了~
      

  4.   


    你发送的时候,每个字节什么值,直接给这个类里面的字段赋值,然后 ToArray这个,发送就行了··
      

  5.   


    你发送的时候,每个字节什么值,直接给这个类里面的字段赋值,然后 ToArray这个,发送就行了··大侠,谢谢你这样指导我,我是个新手
    using(MemoryStream ms = new MemoryStream()) 
          using(BinaryWriter bw = new BinaryWriter(ms))
    我不知道是干嘛的...是库函数么?我也不知道为什么要调用他们两个...求大侠赐教 
      

  6.   

    定义一个16字节的数组,把相应的内容写入对应的字节,发送就行了。
    1.数据块是7个字节,参数sendData是字符串,可以用System.Text.Encoding.Unicode.GetBytes函数转成字节数组,补齐成7字节。
    2.如果sendData字符串很长,超过7字节怎么办?难道要分成多个数据包吗,这样下位机就需加组包了。
    3.CRC校验网上就能找到源码