‘定义变量
Type LOAD_DATA
    Max As Single         '负荷传感器量程
    Range As Integer          '线性段    
    CalData(5) As Single     '线性段标定系数   //数组
    Value As Single           '负荷值
    Code As Long            '负荷原码值
End Type
Type TIME_DATA
    CalData As Single       '时间标定系数
    Value As Single         '时间值
    Code As Long          '时间原码累加变量
End Type‘线性段放大倍数
Global Const Load_Cal(0) = 20       '第一段
Global Const Load_Cal(1) = 10       '第二段
Global Const Load_Cal(2) = 5        '第三段
Global Const Load_Cal(3) = 2        '第四段
Global Const Load_Cal(4) = 1        '第五段Global LD As LOAD_DATA       //全局变量
Global TM As TIME_DATA‘设置串口属性
Comm1.Settings = "19200,n,8,1"
Comm1.InputMode = 1
Comm1.Rthreshold=40
‘串口接收
Private Sub Comm1_OnComm()
Dim ReceiveStr As String
Select Case Comm1.CommEvent
    Case 1
    Case 2
        If Comm1.InBufferCount <> 0 Then '串口不为空则读数据
            ReceiveStr = Comm1.Input
            mscommstr = mscommstr + ReceiveStr
TimerComm=True
        End If
End Select
End Sub
‘原始数据采集
Public Sub Data_Transfor()
Dim i, j, strlenth, strlenth1 As Integer
Dim midstr1, midstr2 As String
Dim DataStr() As Byte
Dim middata() As Byte
Dim LoadPos, Range, lNumber As Integer
'loadPOS--接收字符串中负荷起始帧的位置
'Range--接收到的量程
Dim yy0, yy1, yy2, yy3, yy4, yy5 As Byte
Dim x0, x1, x2, x3, key As Byte
    midstr2 = mscommstr    '串口数据上次剩余量mscommstr
    strlenth = LenB(midstr2)
    If strlenth > 23 Then
        DataStr() = midstr2
        For i = 0 To strlenth - 1   '查找负荷第一帧数据
            x0 = DataStr(i)
            x0 = x0 And &HF0
            Select Case x0
                Case &H80, &HF0  '负荷、键值
                    LoadPos = i
                    GoTo 10
            End Select
        Next i
    End If
    mscommstr = midstr2
    Exit Sub
10: midstr1 = MidB(midstr2, LoadPos + 1, strlenth - LoadPos)
    middata() = midstr1
    strlenth1 = LenB(midstr1)
    yy0 = strlenth1 \ 4     '每四帧数据为一个量(负荷、应变、位移、大延伸、时间)
    For i = 0 To yy0 - 1
        j = 4 * i
        x0 = middata(0 + j)
        x1 = middata(1 + j)
        x2 = middata(2 + j)
        x3 = middata(3 + j)
        yy1 = x1 And &H7F       '取数据
        yy2 = x2 And &H7F       '取数据
        yy3 = x3 And &H7F       '取数据
        yy4 = x0 And &H7        '取数据
        yy5 = x0 And &H8        '取符号
        Range = x0 And &H7      '取量程
        key = x0 And &HF        '取键值
        x0 = x0 And &HF0        '取识别量
        Select Case x0
            Case &H80       '负荷
                LD.Code = yy2 * 128 + yy3
                If yy5 = &H8 Then LD.Code = LD.Code * (-1)
                If Range < 5 Then LD.Range = Range
        End Select
    Next i
    If (strlenth1 Mod 4) = 0 Then
        mscommstr = ""
    Else
        mscommstr = MidB(midstr1, yy0 * 4 + 1, strlenth1 - yy0 * 4)
    End If
End Sub
‘数据处理
Private Sub Timer1_Timer()
Dim LoadValue, LoadOldValue, newD As Single
If TimerComm Then
    Data_Transfor
    TimerComm = False
Else
    Exit Sub
End If
LD.Value = LD.Code * LD.Max * 0.275 * LD.CalData( LD.Range) / Load_Cal(LD.Range)
TM.Value = TM.Code * TM.CalData / 100# 
End Sub转成delphi 怎么写。

解决方案 »

  1.   

    我自己的Delphi 写法。我估计有问题。。
    type
      TLOAD_DATA = record
        Max : Currency;        // '负荷传感器量程
        Range : Shortint;       //     '线性段
        CalData : array[0..4] of Currency;   //    '线性段标定系数
        Value : Currency;        //     '负荷值
        Code : Integer;           //   '负荷原码值  end;
      LOAD_DATA = ^TLOAD_DATA; TTIME_DATA = record
        CalData : Currency;    //   '时间标定系数
        Value : Currency;      //   '时间值
        Code : Integer;       //   '时间原码累加变量
     end;
     TIME_DATA =^TTIME_DATA;  LD : LOAD_DATA; //全局变量定义
      ET : EXT_DATA;
      PS : POSITION_DATA;
      TM : TIME_DATA;
      Load_Cal : array [0..4] of Shortint;
      mscommstr : WideString;
      TimeComm : Boolean;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Load_Cal[0] := 20;
      Load_Cal[1] := 10;
      Load_Cal[2] := 5;
      Load_Cal[3] := 2;
      Load_Cal[4] := 1;  Comm1.Settings := '19200,n,8,1';
      Comm1.InputMode := 1;
      Comm1.Rthreshold :=40;
      Comm1.CommPort := 5;
      mscommstr := '';
      TimeComm := False;
    end;procedure TForm1.Comm1Comm(Sender: TObject);
    var
      ReceiveStr : WideString;
      a : Integer;
    begin case Comm1.CommEvent of
        1 : a := 1;
        2 : begin
            if Comm1.InBufferCount <> 0 then //'串口不为空则读数据
              begin
                ReceiveStr := Comm1.Input;
                mscommstr := mscommstr + ReceiveStr;
                TimeComm :=True;
             end;
            end;
      end;end;procedure TForm1.Data_Transfor;
    var
     i, j, strlenth, strlenth1 : ShortInt;
     midstr1, midstr2 : WideString;
     DataStr : array of Byte;
     middata : array of Byte;
     LoadPos, Range, lNumber : ShortInt;
    //'loadPOS--接收字符串中负荷起始帧的位置
    //'Range--接收到的量程
     yy0, yy1, yy2, yy3, yy4, yy5 : Byte;
     x0, x1, x2, x3, key : Byte;
     label nb;
    begin
        midstr2 := mscommstr;   // '串口数据上次剩余量mscommstr
        strlenth := Length(midstr2);
        If strlenth > 23 Then
        begin
            setlength(DataStr,strlenth);
            for i:= 0 To strlenth - 1 do
            begin
              DataStr[i] := Byte(midstr2[i]);
            end;        for i:= 0 To strlenth - 1 do //   '查找负荷第一帧数据
            begin          x0 := DataStr[i];
              x0 := x0 and $F0;
              Case x0 of
                 $3F,$F0:  // '负荷、键值
                     begin
                      LoadPos := i;
                      goto nb;
                      end;
              end;
            end;
        end;
        mscommstr := midstr2;
        Exit;
        nb: midstr1 := Copy(midstr2, LoadPos + 1, strlenth - LoadPos);
        strlenth1 := Length(midstr1)×2;
        setlength(middata,strlenth1);
            for i:= 0 To strlenth1 - 1 do
            begin
              middata[i] := Byte(midstr1[i]);
            end;
        yy0 := strlenth1 div 4;    // '每四帧数据为一个量(负荷、应变、位移、大延伸、时间)
        For i:= 0 To yy0 - 1 do
        begin
            j := 4 * i;
            x0 := middata[0 + j];
            x1 := middata[1 + j];
            x2 := middata[2 + j];
            x3 := middata[3 + j];
            yy1 := x1 And $7F;      // '取数据
            yy2 := x2 And $7F;      // '取数据
            yy3 := x3 And $7F;      // '取数据
            yy4 := x0 And $7;       // '取数据
            yy5 := x0 And $8;       // '取符号
            Range := x0 And $7;     // '取量程
            key := x0 And $F;       // '取键值
            x0 := x0 And $F0;       // '取识别量
              Case x0 of
                $80 : //       '负荷
                     begin
                    LD.Code := yy2 * 128 + yy3;
                    If yy5 = $8 then
                    begin
                     LD.Code := LD.Code * (-1);
                    end;
                    If Range < 5 Then LD.Range := Range;
                   end;
             end;
          end;
        If (strlenth1 Mod 4) = 0 Then
        begin
            mscommstr := '';
        end
        else
        begin
            mscommstr := Copy(midstr1, yy0 * 4 + 1, strlenth1 - yy0 * 4);
        end;end;procedure TForm1.TimerCommTimer(Sender: TObject);
    var
     LoadValue, LoadOldValue, newD : Currency;
    begin
      if TimeComm Then
      begin
          Data_Transfor;
          TimeComm := False;
      end
      else
      begin
        Exit;
      end;
    //  LD.Value := LD.Code * LD.Max * 0.275 * LD.CalData[ LD.Range] / Load_Cal[LD.Range];
      TM.Value := TM.Code * TM.CalData / 100;
    end;procedure TForm1.btn1Click(Sender: TObject);
    begin
      if Comm1.PortOpen then // 打开串口
      begin
         Comm1.PortOpen := False;  //关闭串口
         Comm1.DTREnable := False; // 数据终端准备好
         btn1.Caption := '接收结束'
      end
      else
      begin
        Comm1.PortOpen := True;
        Comm1.DTREnable := True;
        btn1.Caption := '正在接收'
      end;
    end;