我是学软件的,但是学的不是很好,刚过从学校毕业,能做的只是普通的软件,现在在一家自控设备公司工作
公司主要是做一些自控设备,现在我的任务是编写一个程序,能够实现数据传输,带我做的人很坏,只给我一个协议,就让我写,连例子都不给我一个
我哪里写的出来哦,现在我急的跟热锅上的蚂蚁一样,希望有好心人能指点指点我,应该从哪里下手
小女子感激不尽

解决方案 »

  1.   

    可以使用串口通讯控件spcomm,这样应该相对简单点。
      

  2.   

    使用PCOMM.DLL非常方便,
    我可以给你个例子用
      

  3.   

    串口通讯主要完成如下功能应该就可以了
    1.得出计算机上所有串口,可以选择;
    2.打开选择的串口,如果用控件就很简单了,否则需要使用CreateFile函数
    3.监听并接收发送到串口中的数据,如果不用控件需要使用readfile函数,然后将你接收到的数据按照规定的协议进行解析,这是最重要的工作
    4.发送数据,如果不用控件需要使用writefile函数
      

  4.   

    (*********************************************************************
        PComm.pas
         -- PComm Lib unit for Delphi (32 bit version).
        History:   Date       Author         Comment
                   5/29/98    Casper         Wrote it.**********************************************************************)unit PComm;interfaceconst
      { baud rate setting }
      B50 = $0;
      B75 = $1;
      B110 = $2;
      B134 = $3;
      B150 = 44;
      B300 = $5;
      B600 = $6;
      B1200 = $7;
      B1800 = $8;
      B2400 = $9;
      B4800 = $A;
      B7200 = $B;
      B9600 = $C;
      B19200 = $D;
      B38400 = $E;
      B57600 = $F;
      B115200 = $10;
      B230400 = $11;
      B460800 = $12;
      B921600 = $13;  { data bit }
      BIT_5 = $0;
      BIT_6 = $1;
      BIT_7 = $2;
      BIT_8 = $3;  { stop bit }
      STOP_1 = $0;
      STOP_2 = $4;  { parity }
      P_EVEN = $18;
      P_ODD  = $8;
      P_SPC  = $38;
      P_MRK  = $28;
      P_NONE = $0;  { modem control setting }
      C_DTR = $1;
      C_RTS = $2;  { modem line status }
      S_CTS = $1;
      S_DSR = $2;
      S_RI  = $4;
      S_CD  = $8;  { error code }
      SIO_OK           = 0;
      SIO_BADPORT      = -1;  { No such port or port not opened }
      SIO_OUTCONTROL   = -2;  { Can't control board }
      SIO_NODATA       = -4;  { No data to read or no buffer to write }
      SIO_OPENFAIL     = -5;   { No such port or port has opened }
      SIO_RTS_BY_HW    = -6;  { Can't set because H/W flowctrl }
      SIO_BADPARM      = -7;  { Bad parameter }
      SIO_WIN32FAIL    = -8;  (* Call win32 function fail, please call }
                                 GetLastError to get the error code *)
      SIO_BOARDNOTSUPPORT  = -9;  { Board does not support this function}
      SIO_FAIL         = -10; { PComm function run result fail }
      SIO_ABORT_WRITE  = -11; { Write has blocked, and user abort write }
      SIO_WRITETIMEOUT = -12; { Write timeout has happened }  { file transfer error code }
      SIOFT_OK           = 0;
      SIOFT_BADPORT      = -1; { No such port or port not open }
      SIOFT_TIMEOUT      = -2; { Protocol timeout }
      SIOFT_ABORT        = -3; { User key abort }
      SIOFT_FUNC         = -4; { Func return abort }
      SIOFT_FOPEN        = -5; { Can not open files }
      SIOFT_CANABORT     = -6; { Ymodem CAN signal abort }
      SIOFT_PROTOCOL     = -7; { Protocol checking error abort }
      SIOFT_SKIP         = -8; { Zmodem remote skip this send file }
      SIOFT_LACKRBUF     = -9; { Zmodem Recv-Buff size must >= 2K bytes }
      SIOFT_WIN32FAIL    = -10; (* OS fail }
      GetLastError to get the error code *)
      SIOFT_BOARDNOTSUPPORT = -11; { Board does not support this function}type  IrqProc = procedure(port: Longint);stdcall;
      CallBackProc = function(len: Longint; rlen: Longint; buf: PChar; flen: Longint): Longint;stdcall;{Import routine from PComm.dll}
    function sio_open(port: Longint): Longint; stdcall;
    function sio_close(port: Longint): Longint; stdcall;
    function sio_ioctl(port, baud, mode: Longint): Longint; stdcall;
    function sio_flowctrl(port, mode: Longint): Longint; stdcall;
    function sio_flush(port, func: Longint): Longint; stdcall;
    function sio_DTR(port, mode: Longint): Longint; stdcall;
    function sio_RTS(port, mode: Longint): Longint; stdcall;
    function sio_lctrl(port, mode: Longint): Longint; stdcall;
    function sio_baud(port, speed: Longint): Longint; stdcall;
    function sio_getch(port: Longint): Longint; stdcall;
    function sio_read(port: Longint; buf: PChar; len: Longint): Longint; stdcall;
    function sio_linput(port: Longint; buf:PChar; len: Longint; term:Longint): Longint; stdcall;
    function sio_putch(port, term: Longint): Longint; stdcall;
    function sio_putb(port: Longint; buf:PChar; len: Longint): Longint; stdcall;
    function sio_write(port: Longint; buf:PChar; len: Longint): Longint; stdcall;
    function sio_putb_x(port: Longint; buf:PChar; len: Longint; tick:Longint): Longint; stdcall;
    function sio_putb_x_ex(port: Longint; buf:PChar; len: Longint; tms:Longint): Longint; stdcall;
    function sio_lstatus(port: Longint): Longint; stdcall;
    function sio_iqueue(port: Longint): Longint; stdcall;
    function sio_oqueue(port: Longint): Longint; stdcall;
    function sio_Tx_hold(port: Longint): Longint; stdcall;
    function sio_getbaud(port: Longint): Longint; stdcall;
    function sio_getmode(port: Longint): Longint; stdcall;
    function sio_getflow(port: Longint): Longint; stdcall;
    function sio_data_status(port: Longint): Longint; stdcall;
    function sio_term_irq(port: Longint; func: IrqProc; code: Byte): Longint; stdcall;
    function sio_cnt_irq(port: Longint; func: IrqProc; count: Longint): Longint; stdcall;
    function sio_modem_irq(port: Longint; func: IrqProc): Longint; stdcall;
    function sio_break_irq(port: Longint; func: IrqProc): Longint; stdcall;
    function sio_Tx_empty_irq(port: Longint; func: IrqProc): Longint; stdcall;
    function sio_break(port, time: Longint): Longint; stdcall;
    function sio_view(port: Longint; buf: PChar; len: Longint): Longint; stdcall;
    function sio_TxLowWater(port, size: Longint): Longint; stdcall;
    function sio_AbortWrite(port: Longint): Longint; stdcall;
    function sio_AbortRead(port: Longint): Longint; stdcall;
    function sio_SetWriteTimeouts(port, timeouts: Longint): Longint; stdcall;
    function sio_GetWriteTimeouts(port: Longint; var TotalTimeouts:Longint): Longint; stdcall;
    function sio_SetReadTimeouts(port, TotalTimeouts, IntervalTimeouts: Longint): Longint; stdcall;
    function sio_GetReadTimeouts(port: Longint; var TotalTimeouts, IntervalTimeouts: Longint): Longint; stdcall;
    function sio_FtASCIITx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtASCIIRx(port:Longint; fname:PChar; func:CallBackProc; key:Longint; sec:Longint): Longint; stdcall;
    function sio_FtXmodemCheckSumTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtXmodemCheckSumRx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtXmodemCRCTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtXmodemCRCRx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtXmodem1KCRCTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtXmodem1KCRCRx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtYmodemTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtYmodemRx(port:Longint; var fname:PChar;fno:LongInt;func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtZmodemTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtZmodemRx(port:Longint; var fname:PChar;fno:LongInt;func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtKermitTx(port:Longint; fname:PChar; func:CallBackProc; key:Longint): Longint; stdcall;
    function sio_FtKermitRx(port:Longint; var fname:PChar;fno:LongInt;func:CallBackProc; key:Longint): Longint; stdcall;
      

  5.   

    implementation
    function sio_open; external 'PComm.dll';
    function sio_close; external 'PComm.dll';
    function sio_ioctl; external 'PComm.dll';
    function sio_flowctrl; external 'PComm.dll';
    function sio_flush; external 'PComm.dll';
    function sio_DTR; external 'PComm.dll';
    function sio_RTS; external 'PComm.dll';
    function sio_lctrl; external 'PComm.dll';
    function sio_baud; external 'PComm.dll';
    function sio_getch; external 'PComm.dll';
    function sio_read; external 'PComm.dll';
    function sio_linput; external 'PComm.dll';
    function sio_putch; external 'PComm.dll';
    function sio_putb; external 'PComm.dll';
    function sio_write; external 'PComm.dll';
    function sio_putb_x; external 'PComm.dll';
    function sio_putb_x_ex; external 'PComm.dll';
    function sio_lstatus; external 'PComm.dll';
    function sio_iqueue; external 'PComm.dll';
    function sio_oqueue; external 'PComm.dll';
    function sio_Tx_hold; external 'PComm.dll';
    function sio_getbaud; external 'PComm.dll';
    function sio_getmode; external 'PComm.dll';
    function sio_getflow; external 'PComm.dll';
    function sio_data_status; external 'PComm.dll';
    function sio_term_irq; external 'PComm.dll';
    function sio_cnt_irq; external 'PComm.dll';
    function sio_modem_irq; external 'PComm.dll';
    function sio_break_irq; external 'PComm.dll';
    function sio_Tx_empty_irq; external 'PComm.dll';
    function sio_break; external 'PComm.dll';
    function sio_view; external 'PComm.dll';
    function sio_TxLowWater; external 'PComm.dll';
    function sio_AbortWrite; external 'PComm.dll';
    function sio_AbortRead; external 'PComm.dll';
    function sio_SetWriteTimeouts; external 'PComm.dll';
    function sio_GetWriteTimeouts; external 'PComm.dll';
    function sio_SetReadTimeouts; external 'PComm.dll';
    function sio_GetReadTimeouts; external 'PComm.dll';
    function sio_FtASCIITx; external 'PComm.dll';
    function sio_FtASCIIRx; external 'PComm.dll';
    function sio_FtXmodemCheckSumTx; external 'PComm.dll';
    function sio_FtXmodemCheckSumRx; external 'PComm.dll';
    function sio_FtXmodemCRCTx; external 'PComm.dll';
    function sio_FtXmodemCRCRx; external 'PComm.dll';
    function sio_FtXmodem1KCRCTx; external 'PComm.dll';
    function sio_FtXmodem1KCRCRx; external 'PComm.dll';
    function sio_FtYmodemTx; external 'PComm.dll';
    function sio_FtYmodemRx; external 'PComm.dll';
    function sio_FtZmodemTx; external 'PComm.dll';
    function sio_FtZmodemRx; external 'PComm.dll';
    function sio_FtKermitTx; external 'PComm.dll';
    function sio_FtKermitRx; external 'PComm.dll';end.
      

  6.   

      sio_Close(Port);//关闭串口 port 是 1,2,3……
      sio_open(Port);//打开串口
      sio_ioctl(Port,B19200,P_NONE+BIT_8+STOP_1);//设置波特率,数据位,停止位
      sio_flush(Port,2);
      while not Terminated do
      begin
        if FPause then
        begin
          Application.ProcessMessages;
          Sleep(10);
          Continue;
        end;
      try begin
        DataLen := sio_iqueue(Port);//读串口中队列数据长度
        if DataLen>0 then
        begin
          CurByte := sio_getch(port);//从串口读一个字符
      

  7.   

    我的QQ148433108  加的时候请注明  “编程”  否则我不加的哦
    真心的很急  想学会 才公布我的qq的 
    希望好心人才加
      

  8.   

    procedure TDAW50Thread.ControlLED(const CMDType: Integer);  //显示屏控制程序,依照显示屏显示协议!
    var
      buf: array [0..3] of Byte;
    begin
      buf[0] := $10;
      case CMDType of
        0: buf[1] := $7f;
        1: buf[1] := $7e;
      end;
      buf[2] := $33;
      buf[3] := $0d;
      sio_write(LEDPort,@buf,4);//串口发送数据
    end;  
      

  9.   

    没事别着急啊。。你先看看spcomm的例子。。还是比较简单的
      

  10.   

    我的QQ148433108  加的时候请注明  “编程”  否则我不加的哦 
    真心的很急  想学会 才公布我的qq的 
    希望好心人加我教教我
      

  11.   

    如果是读卡器之类的,都有开发DLL文件,不用自己写。
      

  12.   

    找一本关于delphi 串口通讯的书看看
    http://www.itstudy.net/
    上面有一些VC的资料,很有帮助
    有一本数 Delphi串口及语音传真高级编程 对初学者很有帮助,下载看看
      

  13.   

    用spcomm写还是很方便的,关键就是根据协议解析数据!!!!!
      

  14.   

    用的是SpComm控件:1: 设置好控件的相关参数,也就是端口号,波特率等;2: SpComm.StartComm;3:
     写数据:
    var
      buf:array of byte;//buf[5]是校验位,其余的都通讯协议中的数据,完成的功能是开启MF1读卡器的125K磁场
      i:integer;
      bcc:byte;
      vRec: PChar;
    begin
      setlength(buf,5);
      buf[0]:=$02; //STX
      buf[1]:=$00; //ADDR
      buf[2]:=2;   //LEN
      buf[3]:=$2A; //CMD/STU  磁场
      buf[4]:=1;   //DATA  开启125KHz
      bcc:=0;
      for i:=1 to length(buf)-1 -1 do
         bcc:=bcc xor buf[i];
      buf[5]:=bcc; //BCC
      SpComm.WriteCommData(pchar(@buf[0]),length(buf));//写数据
    end;4:
    procedure TForm1.SpCommReceiveData(Sender: TObject; Buffer: Pointer;  BufferLength: Word);//SpComm控件的接收数据事件
    var
      strRecv:string;
      i:Integer;
      rbuf:array[1..256] of byte;
      viewstr1:string;
      Channel:array[1..4] of string;
      vReceFlag: string;
    begin
      viewstr1:='';
      pubRecevice := '';
      Move(Buffer^,PChar(@rbuf)^,BufferLength);
      for i := 1 to bufferlength - 1 do//最后一位是校验位
          viewstr1:=viewstr1+inttohex(rbuf[i],2)+'';转换成十六进制数
      pubRecevice := viewstr1;
      Memo2.Lines.Text := viewstr1;end;
      

  15.   

    你好,我们的工作很类似,我们公司也是做自控的,用delphi。。我在山东济南,可以交个朋友吗?我也是新手,去年毕业,但是我不是软件专业。公司也小,自学的,以后互相学习,共同进步。好吗?我的msn是[email protected],qq是365708921.能否加你好友? 
      

  16.   

    用spcomm试试,好好学一下。
    怎么新手都从串口通信开始呢、?呵呵
      

  17.   

    刚看了下上传的通信协议,协议种类和参数真多阿,借此机会也观摩一下...
    你要实现通信传输,连的上就算ok了吧?可以选择用控件或者windowsAPI。API感觉对你挺难,用控件吧。最好是能有实现了协议的现场机给你调试。控件使用搜索很多的,楼上楼下都是。
      

  18.   

    看不到通讯协议
    cnpack控件包的RS232组件也可以做串口通讯,使用方法类似spcomm