比如说串口通信中,数据位有8位,但是平时我们在发送和接收过程中得数据位是不固定得,那串口是怎么转换得那?使得下位机得发送得数据位数和上位机接收到得位数数据是一样得那?

解决方案 »

  1.   

    我是用MScomm控件作的,在DElphi7中通过,就是串口一但有数据进入就产生读取事件,且一次就读入该次的所有数据。
    //初始化
    with mscomm1 do
      begin
        commport:=1;
        settings:='9600,n,8,1';
        inbuffersize:=1024;
        outbuffersize:=1024;
        inputmode:=comInputmodebinary;
        inputLen:=0;
        Sthreshold:=0;
        Rthreshold:=1;
        inBufferCount:=0;
        OutbufferCount:=0;
        if portOpen then
        begin
          showMessage('错误,通讯端口已被打开!');
        end;
        try
          portOpen:=true;
        except
          raise exception.Create('串口通讯出错!');
        end;
        showmessage('串口通讯成功!');
    end;procedure TMainF.MSComm1Comm(Sender: TObject);
    var
      indata:olevariant;
      fields:variant;
      tem,outbuffer,realaddr,username:string;
    begin
      case mscomm1.CommEvent of
        comEvReceive:
        begin
          dataCount:=mscomm1.InBufferCount;
          indata:=mscomm1.Input;
          for i:=0 to dataCount-1 do
            tem:=tem+inttohex(indata[i],2);//tem中的数据就是串口一次收到数据的十六进制的字符串
        end;
      end;
    end;