关于串口的问题(100分)

  • 主题发起人 主题发起人 hylks
  • 开始时间 开始时间
H

hylks

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把一个采集仪器上的数据显示在界面上,看了 一些关于 windows API <br>还是不太明白 ?请高手指教?<br>谢谢
 
需要<br>1.了解传输方式,比如RS232,489等等<br>2.了解通讯协议,是二进制传送还是字符串方式传送<br>3.使用控件或者API<br><br>下面是我的一个单元的代码希望对你有帮助<br><br><br>{ 实验室设备软件 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{ System Resource &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ 串口定义单元 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br>{ Copyright (c) 2005, CHR Corporation }<br>{ Date: &nbsp; &nbsp;2005-10-01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ Build: &nbsp; 2005-11-17 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ Author: &nbsp;muhx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>unit UntCommDefine;<br><br>interface<br><br>uses<br> &nbsp;Windows, Classes, SyncObjs, SysUtils, UntTypeDefine;<br><br>type<br> &nbsp;{ 串口通讯类 }<br> &nbsp;TCom = class<br> &nbsp;private<br> &nbsp; &nbsp;FCriticalSection: TCriticalSection;<br> &nbsp; &nbsp;FHandle: THandle;<br><br> &nbsp; &nbsp;FComName: string;<br> &nbsp; &nbsp;FBaud: TBaudRate;<br> &nbsp; &nbsp;FParity: TParity;<br> &nbsp; &nbsp;FStopBit: TStopBit;<br> &nbsp; &nbsp;FDataBit: TDataBit;<br> &nbsp; &nbsp;FInQueue: Integer;<br> &nbsp; &nbsp;FOutQueue: Integer;<br> &nbsp; &nbsp;FReadTimeOut: Integer;<br> &nbsp; &nbsp;FWriteTimeOut: Integer;<br> &nbsp; &nbsp;FTimeOut: COMMTIMEOUTS;<br> &nbsp; &nbsp;FEventMask: Integer;<br> &nbsp;private<br> &nbsp; &nbsp;procedure Lock;<br> &nbsp; &nbsp;procedure Unlock;<br> &nbsp; &nbsp;function HandtoClip(AAddress: Byte): Boolean;<br> &nbsp; &nbsp;function ReceiveData(APReceive: PByte; AReceiveLen: Integer): Boolean;<br> &nbsp; &nbsp;function FiltrateUnableDate: Boolean;<br> &nbsp;public<br> &nbsp; &nbsp;constructor ComCreate(AComSet: TComSet);<br> &nbsp; &nbsp;destructor Destroy; override;<br> &nbsp;public<br> &nbsp; &nbsp;procedure ChangeComSet(const AComSet: TComSet);<br> &nbsp; &nbsp;procedure ChangeParity(AParity: TParity);<br> &nbsp; &nbsp;procedure CloseCom;<br> &nbsp; &nbsp;procedure OpenCom;<br> &nbsp; &nbsp;procedure PurgeCom;<br> &nbsp; &nbsp;function Enabled: Boolean;<br> &nbsp; &nbsp;function Read(var APData; ADataLen: Integer): Boolean;<br> &nbsp; &nbsp;function Write(var APData; ADataLen: Integer): Boolean;<br> &nbsp;public<br> &nbsp; &nbsp;function SendCommand(APCommand: PCmdParam): Boolean;<br> &nbsp;public<br> &nbsp; &nbsp;property gHandle: THandle read FHandle;<br> &nbsp; &nbsp;property gBaud: TBaudRate read FBaud default br57600;<br> &nbsp; &nbsp;property gDataBit: TDataBit read FDataBit default da8;<br> &nbsp; &nbsp;property gInQueue: Integer read FInQueue default 4096;<br> &nbsp; &nbsp;property gOutQueue: Integer read FOutQueue default 2048;<br> &nbsp; &nbsp;property gParity: TParity read FParity default paSpace;<br> &nbsp; &nbsp;property gStopBits: TStopBit read FStopBit default sb10;<br> &nbsp; &nbsp;property gReadTimeOut: Integer read FReadTimeOut default 2000;<br> &nbsp; &nbsp;property gComName: string read FComName;<br> &nbsp; &nbsp;property gWriteTimeOut: Integer read FWriteTimeout default 2000;<br> &nbsp;end;<br><br> &nbsp;TComArray = array[0..NUM_UNIT - 1] of TCom;<br><br>implementation<br><br>{ 创建串口类 }<br>constructor TCom.ComCreate(AComSet: TComSet);<br>begin<br> &nbsp;inherited Create;<br> &nbsp;{ 临界区 }<br> &nbsp;FCriticalSection := TCriticalSection.Create;<br> &nbsp;{ 串口控制句柄 }<br> &nbsp;FHandle := INVALID_HANDLE_VALUE;<br> &nbsp;{ 串口参数 }<br> &nbsp;FComName := COMComID[AComSet.csComID];<br> &nbsp;FParity := AComSet.csParity;<br> &nbsp;FBaud := AComSet.csBaud;<br> &nbsp;FDataBit := AComSet.csDataBit;<br> &nbsp;FStopBit := AComSet.csStopBit;<br> &nbsp;FInQueue := AComSet.csInQueue;<br> &nbsp;FOutQueue := AComSet.csOutQueue;<br> &nbsp;FReadTimeOut := AComSet.csReadTimeOut;<br> &nbsp;FWriteTimeOut := AComSet.csWriteTimeOut;<br> &nbsp;FEventMask := EV_RXCHAR;<br> &nbsp;{ 两个字节传输间隔 }<br> &nbsp;FTimeOut.ReadIntervalTimeout := 30;<br> &nbsp;{ 每字节传输时间 }<br> &nbsp;FTimeOut.ReadTotalTimeoutMultiplier := 5;<br> &nbsp;{ 没有字节时返回时间 }<br> &nbsp;FTimeOut.ReadTotalTimeoutConstant := 80;<br> &nbsp;{ 每字节传输时间 }<br> &nbsp;FTimeOut.WriteTotalTimeoutMultiplier := 5;<br> &nbsp;{ 传输时间常量 }<br> &nbsp;FTimeOut.WriteTotalTimeoutConstant := 50;<br> &nbsp;{ 打开串口 }<br> &nbsp;OpenCom;<br>end;<br><br>{ 释放串口类 }<br>destructor TCom.Destroy;<br>begin<br> &nbsp;CloseCom;<br> &nbsp;if FCriticalSection &lt;&gt; nil then<br> &nbsp;begin<br> &nbsp; &nbsp;FCriticalSection.Free;<br> &nbsp; &nbsp;FCriticalSection := nil;<br> &nbsp;end;<br> &nbsp;inherited Destroy;<br>end;<br><br>{ 判断串口是否创建 }<br>function TCom.Enabled: Boolean;<br>begin<br> &nbsp;Result := FHandle &lt;&gt; INVALID_HANDLE_VALUE;<br>end;<br><br>{ 关闭串口 }<br>procedure TCom.CloseCom;<br>begin<br> &nbsp;if Enabled then<br> &nbsp;begin<br> &nbsp; &nbsp;Sleep(100);<br> &nbsp; &nbsp;CloseHandle(FHandle);<br> &nbsp; &nbsp;FHandle := INVALID_HANDLE_VALUE;<br> &nbsp;end;<br>end;<br><br>{ 更改串口参数 }<br>procedure TCom.ChangeComSet(const AComSet: TComSet);<br>var<br> &nbsp;tmpDCB: DCB;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;with AComSet do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FParity := csParity;<br> &nbsp; &nbsp; &nbsp;FBaud := csBaud;<br> &nbsp; &nbsp; &nbsp;FDataBit := csDataBit;<br> &nbsp; &nbsp; &nbsp;FStopBit := csStopBit;<br> &nbsp; &nbsp; &nbsp;FInQueue := csInQueue;<br> &nbsp; &nbsp; &nbsp;FOutQueue := csOutQueue;<br> &nbsp; &nbsp; &nbsp;FReadTimeOut := csReadTimeOut;<br> &nbsp; &nbsp; &nbsp;FWriteTimeOut := csWriteTimeOut;<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if Enabled then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;tmpDCB.DCBlength := SizeOf(DCB);<br> &nbsp; &nbsp; &nbsp;GetCommState(FHandle, tmpDCB);<br> &nbsp; &nbsp; &nbsp;tmpDCB.BaudRate := COMBaudRate[FBaud];<br> &nbsp; &nbsp; &nbsp;tmpDCB.Parity := COMParity[FParity];<br> &nbsp; &nbsp; &nbsp;tmpDCB.ByteSize := COMDataBit[FDataBit];<br> &nbsp; &nbsp; &nbsp;tmpDCB.StopBits := COMStopBit[FStopBit];<br> &nbsp;<br> &nbsp; &nbsp; &nbsp;if not (SetupComm(FHandle, FInQueue, FOutQueue) and SetCommState(FHandle, tmpDCB))<br> &nbsp; &nbsp; &nbsp; &nbsp;then Abort;<br> &nbsp; &nbsp; &nbsp;PurgeCom;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;on E: Exception do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CloseCom;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>{ 更改串口奇偶校验设置 }<br>procedure TCom.ChangeParity(AParity: TParity);<br>var<br> &nbsp;tmpDCB: DCB;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;FParity := AParity;<br> &nbsp; &nbsp;if Enabled then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;tmpDCB.DCBlength := SizeOf(DCB);<br> &nbsp; &nbsp; &nbsp;GetCommState(FHandle, tmpDCB);<br> &nbsp; &nbsp; &nbsp;tmpDCB.Parity := COMParity[FParity];<br><br> &nbsp; &nbsp; &nbsp;if not SetCommState(FHandle, tmpDCB) then<br> &nbsp; &nbsp; &nbsp; &nbsp;Abort;<br> &nbsp; &nbsp; &nbsp;PurgeCom;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;on E: Exception do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CloseCom;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>{ 进入临界区 }<br>{ 在对串口进行操作的时候确保没有同时进行其他操作 }<br>procedure TCom.Lock;<br>begin<br> &nbsp;FCriticalSection.Enter;<br>end;<br><br>{ 退出缓冲区 }<br>procedure TCom.Unlock;<br>begin<br> &nbsp;FCriticalSection.Leave;<br>end;<br><br>{ 创建串口 }<br>procedure TCom.OpenCom;<br>var<br> &nbsp;tmpDCB: DCB;<br>begin<br> &nbsp;if Enabled then<br> &nbsp; &nbsp;Exit;<br> &nbsp;try<br> &nbsp; &nbsp;FHandle := CreateFile(PChar(FComName), GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp;0, nil,<br> &nbsp; &nbsp; &nbsp;OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp;FILE_FLAG_OVERLAPPED,<br> &nbsp; &nbsp; &nbsp;0);<br><br> &nbsp; &nbsp;if not Enabled then<br> &nbsp; &nbsp; &nbsp;Abort<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp;{ 设置串口参数 }<br> &nbsp; &nbsp; &nbsp;tmpDCB.DCBlength := SizeOf(DCB);<br> &nbsp; &nbsp; &nbsp;GetCommState(FHandle, tmpDCB);<br> &nbsp; &nbsp; &nbsp;tmpDCB.BaudRate := COMBaudRate[FBaud];<br> &nbsp; &nbsp; &nbsp;tmpDCB.Parity := COMParity[FParity];<br> &nbsp; &nbsp; &nbsp;tmpDCB.ByteSize := COMDataBit[FDataBit];<br> &nbsp; &nbsp; &nbsp;tmpDCB.StopBits := COMStopBit[FStopBit];<br><br> &nbsp; &nbsp; &nbsp;if not (SetCommMask(FHandle, FEventMask) and<br> &nbsp; &nbsp; &nbsp; &nbsp;SetCommTimeouts(FHandle, FTimeOut) and<br> &nbsp; &nbsp; &nbsp; &nbsp;SetupComm(FHandle, FInQueue, FOutQueue) and<br> &nbsp; &nbsp; &nbsp; &nbsp;SetCommState(FHandle, tmpDCB) and<br> &nbsp; &nbsp; &nbsp; &nbsp;(FCriticalSection &lt;&gt; nil)) then<br> &nbsp; &nbsp; &nbsp; &nbsp;Abort;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;on E: Exception do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CloseCom;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>{ 清空缓冲区 }<br>procedure TCom.PurgeCom;<br>begin<br> &nbsp;if Enabled then<br> &nbsp;begin<br> &nbsp; &nbsp;PurgeComm(FHandle, PURGE_RXCLEAR);<br> &nbsp; &nbsp;PurgeComm(FHandle, PURGE_TXCLEAR);<br> &nbsp;end;<br>end;<br><br>{ 从串口读取数据 }<br>function TCom.Read(var APData; ADataLen: Integer): Boolean;<br>var<br> &nbsp;tmpOverlapped: TOverlapped;<br> &nbsp;tmpEvent: TSimpleEvent;<br> &nbsp;tmpReceive: DWord;<br>begin<br> &nbsp;Result := True;<br> &nbsp;//Lock;<br> &nbsp;tmpEvent := TSimpleEvent.Create;<br> &nbsp;try<br> &nbsp; &nbsp;tmpReceive := 0;<br> &nbsp; &nbsp;FillChar(tmpOverlapped, SizeOf(tmpOverlapped), 0);<br> &nbsp; &nbsp;tmpOverlapped.hEvent := tmpEvent.Handle;<br><br> &nbsp; &nbsp;if not ReadFile(FHandle, APData, ADataLen, DWord(tmpReceive),<br> &nbsp; &nbsp; &nbsp;@tmpOverlapped) and (GetLastError &lt;&gt; ERROR_IO_PENDING) then<br> &nbsp; &nbsp; &nbsp;Result := False;<br><br> &nbsp; &nbsp;if tmpEvent.WaitFor(FReadTimeOut) &lt;&gt; wrSignaled then<br> &nbsp; &nbsp; &nbsp;Result := False<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp;GetOverlappedResult(FHandle, tmpOverlapped, DWord(tmpReceive), False);<br> &nbsp; &nbsp; &nbsp;tmpEvent.ResetEvent;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;//Unlock;<br> &nbsp; &nbsp;tmpEvent.Free;<br> &nbsp; &nbsp;if tmpReceive &lt;&gt; DWord(ADataLen) then<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp;end;<br>end;<br><br>{ 向串口写入数据 }<br>function TCom.Write(var APData; ADataLen: Integer): Boolean;<br>var<br> &nbsp;tmpOverlapped: TOverlapped;<br> &nbsp;tmpEvent: TSimpleEvent;<br> &nbsp;tmpWrite: DWord;<br>begin<br> &nbsp;//Lock;<br> &nbsp;Result := True;<br> &nbsp;tmpEvent := TSimpleEvent.Create;<br> &nbsp;try<br> &nbsp; &nbsp;tmpWrite := 0;<br> &nbsp; &nbsp;FillChar(tmpOverlapped, SizeOf(tmpOverlapped), 0);<br> &nbsp; &nbsp;tmpOverlapped.hEvent := tmpEvent.Handle;<br> &nbsp;<br> &nbsp; &nbsp;if not WriteFile(FHandle, APData, ADataLen, DWord(tmpWrite),<br> &nbsp; &nbsp; &nbsp;@tmpOverlapped) and (GetLastError &lt;&gt; ERROR_IO_PENDING) then<br> &nbsp; &nbsp; &nbsp;Result := False;<br><br> &nbsp; &nbsp;if tmpEvent.WaitFor(FWriteTimeOut) &lt;&gt; wrSignaled then<br> &nbsp; &nbsp; &nbsp;Result := False<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp;GetOverlappedResult(FHandle, tmpOverlapped, DWord(tmpWrite), False);<br> &nbsp; &nbsp; &nbsp;tmpEvent.ResetEvent;<br> &nbsp; &nbsp; end;<br> &nbsp;finally<br> &nbsp; &nbsp;//Unlock;<br> &nbsp; &nbsp;tmpEvent.Free;<br> &nbsp; &nbsp;if tmpWrite &lt;&gt; DWord(ADataLen) then<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp;end;<br>end;<br><br>{ 发送命令 }<br>function TCom.SendCommand(APCommand: PCmdParam): Boolean;<br>var<br> &nbsp;i: Integer;<br>begin<br> &nbsp;Result := False;<br> &nbsp;Lock;<br> &nbsp;try<br> &nbsp; &nbsp;i := 0;<br> &nbsp; &nbsp;while (i &lt; 5) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;{ 地址握手 }<br> &nbsp; &nbsp; &nbsp;ChangeParity(paMark);<br> &nbsp; &nbsp; &nbsp;if not HandtoClip(APCommand^.cmdAddress) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(i);<br> &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;{ 发送命令 }<br> &nbsp; &nbsp; &nbsp;ChangeParity(paSpace);<br> &nbsp; &nbsp; &nbsp;if not Write(APCommand^.cmdCommand, SizeOf(APCommand^.cmdCommand)) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br> &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;{ 发送数据 }<br> &nbsp; &nbsp; &nbsp;if APCommand^.cmdSendLen &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if not Write(APCommand^.cmdPSend^, APCommand^.cmdSendLen) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;{ 接收数据 }<br> &nbsp; &nbsp; &nbsp;if not ReceiveData(APCommand^.cmdPReceive, APCommand^.cmpReceiveLen) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br> &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;Sleep(100);<br> &nbsp; &nbsp;PurgeCom;<br> &nbsp; &nbsp;UnLock;<br> &nbsp;end;<br>end;<br><br>{ 与MCU地址握手 }<br>{ 2005-12-30 15:40 build by muhx }<br>function TCom.HandtoClip(AAddress: Byte): Boolean;<br>var<br> &nbsp;tmpByte: Byte;<br>begin<br> &nbsp;Result := False;<br> &nbsp;{ 发送地址 }<br> &nbsp;if not Write(AAddress, SizeOf(AAddress)) then<br> &nbsp; &nbsp;Exit;<br> &nbsp;{ 过滤无效数据 }<br> &nbsp;if not FiltrateUnableDate then<br> &nbsp; &nbsp;Exit;<br> &nbsp;{ 读取数据 }<br> &nbsp;if not Read(tmpByte, SizeOf(tmpByte)) then<br> &nbsp; &nbsp;Exit;<br> &nbsp;{ 比较数据 }<br> &nbsp;if (tmpByte &lt;&gt; AAddress) then<br> &nbsp; &nbsp;Exit;<br> &nbsp;{ 若接收成功且数据正确返回True }<br> &nbsp;Result := True;<br>end;<br><br>{ 过滤无效数据 }<br>{ 2005-12-30 16:49 build by muhx }<br>function TCom.FiltrateUnableDate: Boolean;<br>var<br> &nbsp;tmpByte: Byte;<br> &nbsp;tmpIndex: Integer;<br>begin<br> &nbsp;Result := False;<br> &nbsp;{ 过滤无效数据 }<br> &nbsp;tmpIndex := 0;<br> &nbsp;repeat<br> &nbsp; &nbsp;Inc(tmpIndex);<br> &nbsp; &nbsp;if tmpIndex &gt; TIMES_FILTER_BYTE then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;if not Read(tmpByte, SizeOf(tmpByte)) then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;if tmpByte &lt;&gt; $AA then<br> &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp;if not Read(tmpByte, SizeOf(tmpByte)) then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;if tmpByte &lt;&gt; $55 then<br> &nbsp; &nbsp; &nbsp;Continue;<br> &nbsp; &nbsp;Result := True;<br> &nbsp;until<br> &nbsp; &nbsp;Result = True;<br>end;<br><br>{ 接收数据,根据通讯协议,在接收数据前有AA55两字节校验码 }<br>{ 2005-12-30 17:25 build by muhx }<br>function TCom.ReceiveData(APReceive: PByte; AReceiveLen: Integer): Boolean;<br>var<br> &nbsp;tmpRead: array[0..1023] of Byte;<br> &nbsp;tmpPByte: PByte;<br> &nbsp;i: Integer;<br>begin<br> &nbsp;Result := False;<br> &nbsp;{ 过滤无效数据 }<br> &nbsp;if not FiltrateUnableDate then<br> &nbsp; &nbsp;Exit;<br> &nbsp;if AReceiveLen &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;if Read(tmpRead, AReceiveLen) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;tmpPByte := APReceive;<br> &nbsp; &nbsp; &nbsp;for i := 0 to AReceiveLen - 1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;tmpPByte^ := tmpRead;<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(tmpPByte);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp;end<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;if not Read(tmpRead, 1) then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;if tmpRead[0] = $88 then<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;Result := True;<br>end;<br><br>end.<br><br>部分定义如下<br>{ 串行通讯定义 }<br> &nbsp;{ 串口号 }<br> &nbsp;TCOMID = (ciCOM1, ciCOM2, ciCOM3, ciCOM4, ciCOM5, ciCOM6, ciCOM7, ciCOM8, ciCOM9,<br> &nbsp; &nbsp;ciCOM10, ciCOM11, ciCOM12, ciCOM13, ciCOM14, ciCOM15, ciCOM16);<br> &nbsp;{ 波特率 }<br> &nbsp;TBaudRate = (br110, br300, br600, br1200, br2400, br4800, br9600, br14400, br19200,<br> &nbsp; &nbsp;br38400, br56000, br57600, br115200, br128000, br256000);<br> &nbsp;{ 奇偶校验 }<br> &nbsp;TParity = (paNone, paOdd, paEven, paMark, paSpace);<br> &nbsp;{ 停止位 }<br> &nbsp;TStopBit = (sb10, sb15, sb20);<br> &nbsp;{ 数据位 }<br> &nbsp;TDataBit = (da4, da5, da6, da7, da8);<br> &nbsp;{ 串口设置属性 }<br> &nbsp;TComSet = record<br> &nbsp; &nbsp;csComID: TComID;<br> &nbsp; &nbsp;csBaud: TBaudRate;<br> &nbsp; &nbsp;csParity: TParity;<br> &nbsp; &nbsp;csDataBit: TDataBit;<br> &nbsp; &nbsp;csStopBit: TStopBit;<br> &nbsp; &nbsp;csOutQueue: Integer;<br> &nbsp; &nbsp;csInQueue: Integer;<br> &nbsp; &nbsp;csReadTimeOut: Integer;<br> &nbsp; &nbsp;csWriteTimeOut: Integer;<br> &nbsp;end;<br><br> &nbsp;{ 命令结构定义 }<br> &nbsp;TCmdParam = record<br> &nbsp; &nbsp;cmdCommand: Byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//命令字<br> &nbsp; &nbsp;cmdAddress: Byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//设备地址<br> &nbsp; &nbsp;cmdSendLen: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //发送数据长度<br> &nbsp; &nbsp;cmdPSend: PByte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //发送数据指针<br> &nbsp; &nbsp;cmpReceiveLen: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//接收数据长度<br> &nbsp; &nbsp;cmdPReceive: PByte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//接收数据指针<br> &nbsp;end;<br> &nbsp;{ 命令结构指针 }<br> &nbsp;PCmdParam = ^TCmdParam;<br><br>还有其他问题可以给我的邮箱发邮件<br>muhx1981@126.com
 
谢谢你的回答<br>我是刚接触这个软件时间不长老板就让我做一个界面,<br>把从采集仪器上采集到的数据在界面上显示出来的同时,还要存在数据库里.<br>真是好难!!!
 
后退
顶部