急求COM口通讯控件
最好附源代码及例子。
在线求救中。
如果确实好,还可加分。  

解决方案 »

  1.   

    对的,SPCOMM
    非常不错,用了一年多,比较经典
      

  2.   

    我用过SPCOMM,很棒!到网上找找!
      

  3.   

    用WinApi的CreateFile建立与串口的通讯很简单的,干吗要控件?这是用CBC控制串口的资料Com端口控制小结
    一、打开Com端口1、使用打开文件的方法CreateFile打开Com端口示例代码:
    char *cComNo = "COM1";  //Or COM2
    DCB dcbCom; hCom = CreateFile
    (
                cComNo,          //pointer to name of the file
    GENERIC_READ | GENERIC_WRITE, //access (read-write) mode
    0, //Share Mode
    NULL, //pointer to security attributes
                OPEN_EXISTING, //how to create
                0, //file attributes
                0 //handle to file with attributes //to copy
         );    if(hCom == INVALID_HANDLE_VALUE)
    {
    //打开通信端口错误!!
    }相关资料:
    The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object: 
    1、files 
    2、pipes 
    3、mailslots 
    4、communications resources 
    5、disk devices (Windows NT only)
    6、consoles 
    7、directories (open only)HANDLE CreateFile(
        LPCTSTR lpFileName, // pointer to name of the file 
        DWORD dwDesiredAccess, // access (read-write) mode 
        DWORD dwShareMode, // share mode 
     LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security //attributes 
        DWORD dwCreationDistribution, // how to create 
        DWORD dwFlagsAndAttributes, // file attributes 
        HANDLE hTemplateFile  // handle to file with attributes to copy  
       );
     
    二、设置Com端口状态步骤1、取得Com当前状态 GetCommState
    2、修改Com当前状态参数
    3、设置Com端口 SetCommState
    4、若3成功则清空通信端口输入/输出缓冲区(不是非常必要吧?)示例代码://取得端口原始参数
    GetCommState(hCom, &dcbCom);//赋予新的参数
    dcbCom.BaudRate = CBR_9600;
    dcbCom.ByteSize = 8;
    dcbCom.Parity = NOPARITY;
    dcbCom.StopBits = ONESTOPBIT;
    //设置通信端口
    if(!SetCommState(hCom,&dcbCom))
    {
    //通信端口设置错误!!
    //CloseHandle(hCom);
    }//清空输入输出缓冲区
    PurgeComm(hCom,PURGE_TXCLEAR | PURGE_TXCLEAR);相关资料:
    1、The GetCommState function fills in a device-control block (a DCB structure) with the current control settings for a specified communications device. BOOL GetCommState(
        HANDLE hFile, // handle of communications device
        LPDCB lpDCB // address of device-control block structure
       ); 2、The SetCommState function configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues. BOOL SetCommState(
        HANDLE hFile, // handle of communications device
        LPDCB lpDCB // address of device-control block structure
       );3、The PurgeComm function can discard all characters from the output or input buffer of a specified communications resource. It can also terminate pending read or write operations on the resource. BOOL PurgeComm(
        HANDLE hFile, // handle of communications resource 
        DWORD dwFlags // action to perform
       );三、从Com端口接收数据1、获得端口错误信息并得到端口状态(通讯信息)
    2、分别判断返回错误信息和端口状态是否合法,若非法如缓冲区不匹配等则进行适当处理并退出
    3、使用ReadFile读取通讯端口数据到缓冲区示例代码:char inBuf[1024]; //输入数据缓冲区
    DWORD  nBytesRead; //读取子节数
    DWORD  dwError; //端口错误信息 COMSTAT cs; //端口状态信息 //获得端口错误标志及通讯信息,并清空设备错误标志
    ClearCommError(hCom,&dwError,&cs);

    //根据端口错误标志以及通讯信息判断是否合法
    //当然还应该有许多其它方面的判断
    if( cs.cbInQue > sizeof(inBuf) ) //队列中的数据长度在端口状态信息中
    {
    //对方传送数据量太大,临时空间不足。
    PurgeComm(hCom,PURGE_RXCLEAR); //清空输入缓冲区
    return;
    } //读取文件,即从通讯端口读取数据
    ReadFile(hCom, inBuf, cs.cbInQue, &nBytesRead, NULL);相关资料:
    1、The ClearCommError function retrieves information about a communications error and reports the current status of a communications device. The function is called when a communications error occurs, and it clears the device's error flag to enable additional input and output (I/O) operations. Return Values:
    If the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.BOOL ClearCommError(
        HANDLE hFile, // handle to communications device
        LPDWORD lpErrors, // pointer to variable to receive error codes
        LPCOMSTAT lpStat // pointer to buffer for communications status  
       );2、The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation. BOOL ReadFile(
        HANDLE hFile, // handle of file to read 
        LPVOID lpBuffer, // address of buffer that receives data  
        DWORD nNumberOfBytesToRead, // number of bytes to read 
        LPDWORD lpNumberOfBytesRead, // address of number of bytes read 
        LPOVERLAPPED lpOverlapped  // address of structure for data 
       ); Return Values:
    If the function succeeds, the return value is nonzero.四、向Com端口发送数据1、确保端口已经打开(应该在别的代码段实现)
    2、初始化与发送数据相关的参数
    3、使用WriteFile发送数据
    4、根据返回值或返回参数作进一步处理示例代码:
    char *cSendData; //发送数据
    String strTemp;
    unsigned long lBytesWritten;
    unsigned long lStrLen; //欲发送数据长度// if(hCom == 0)
    // {
    //      mInfo->Lines->Add("端口还没有打开,请先打开端口!");
    //      return;
    // } //设置相关数据、参数
    strTemp = “要发送的数据在这里”;
    cSendData = strTemp.c_str();
    lStrLen = strTemp.Length(); WriteFile(hCom, cSendData, lStrLen, &lBytesWritten, NULL); if (lBytesWritten > 0)
    {
    //成功发送lBytesWritten个字节的数据
    }
    else
    {
    //"发送数据失败!";
    if (hCom == 0)
    }相关资料:
    The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished. BOOL WriteFile(
        HANDLE hFile, // handle to file to write to 
        LPCVOID lpBuffer, // pointer to data to write to file 
        DWORD nNumberOfBytesToWrite, // number of bytes to write 
        LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written 
        LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped I/O
       );Return Values:
    If the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.五、最重要的一点
    不管是使用Rs-232/485串口还是使用USB端口进行通讯,要解决的关键问题是搞清与设备通讯所使用的协议,只有正确使用协议,才能说与设备建立起了对话,才能使用设备。
      

  4.   

    http://218.56.11.178:8020/web/index.aspx->下载基地->例程-精品例程->九品御厨作品-串口通讯控制器          ->控件-硬件控制->TurboPower Async Professional
      

  5.   

    我要的是mscomm控件,谢谢大家的关心等拿到mscomm不会亏待大家的
      

  6.   

    delphi中常用串口控件简介:
    mscomm:微软的东西,是VB中带的一个ActiveX控件,使用简单,性能一般,由于是ActivX控件,打包时需要注册好多信息,在Delphi中使用,建议使用VCL控件,编译程序时直接编入程序中,再不需任何其它处理。
    spcomm:比较好的vcl控件,算是比较专业的,解剖了一下,功能比较完善。
    TurboPower:公认的专业通讯vcl控件。可以到其站点下载,开放源码了。
    我在制作串口通讯软件时三种都用过,最终全部使用TurboPower!所以也推荐大家使用它。
    写了个例子,基本的串口通讯都可以实现,可提供参考:
    下载基地-》文件名称:串口通讯控制器
    版权声明:以下本文只允许在本站观看,不得以任何媒体方式进行传播。
    发表意见请到留言版。TurboPower串口通讯实际应用:
    在串口通讯时有字符和十六进制两种数据传输方式,不论使用哪种方式,只要能正确收到数据就是目的,至于收到数据后如何处理,就要根据具体的情况来定了。1.接收数据的方法:
    轮询和中断(利用windows消息激发事件)。
    1)轮询:每间隔一定的时间查询一下串口接收缓存中有无数据,有就读出来。这种方法是很毫资源的,即没事找事。
    2)中断:在控件中有OnTrigger事件,当串口收到数据后,即触发此事件,无数据时什么都不做,在这个事件中接收数据就比较科学了。
    所以,提倡使用控件中的OnTrigger事件接收数据。2.通讯协议的制定:
    接收数据的一般处理方法,最基本的思路就是通过协议进行分析,所以协议的制定是至关重要的:
    1)首先要确定指令的起始点,从大量的数据流中将指令分离出来,没有起始标志的话,结果就可想而知了,一串无效的费数据!
    2)然后就是指令结束识别点,可以利用指令的长度(如果长度一定或有表示长度的数据)或结束标志来确定,当然还可以利用下一条指令的指令头。
    3)既然头尾都明确了,指令的截取想来不是什么问题了吧!但还有一种情况就是数据错误是的容错,如何容错呢,最简单的办法:发现不符合格式的指令,就将其抛掉或特殊处理(如要求重发)一下!
    4)有效数据中如果增加一些校验,通讯将会更加可靠!
    例:#(指令头)**(指令功能)0123456789(有效数据)**(有效数据校验和)%(指令尾)
    注:**代表变动值。3.接收数据的分析技巧:
    通讯协议制定好后,一切将以通讯协议为中心。一套协议中的所有指令可能长度都是统一的,也有可能是长短不同的,并且在OnTrigger事件中实际反应速度及快,可能一条指令数据还没有完全收齐就已经触发了此事件,即收到了半截指令,并且有可能继续收取的数据中除了下半截指令外,还有下一条指令的前半截,如何处理?
    我在做这种处理时是利用全局变量,将串口收到的所有数据都收到该串中,然后按指令格式进行截取,发现不合法指令做一下特殊处理(如要求重发)或抛弃。
    如收到的数据串为:
    #**0000012000**%#**0000000343#**000000540560**%#**0002200000**%
    分段截为:
    #**0000012000**%
    #**0000000343
    #**000000540560**%
    #**0002200000**%
    四条指令,其中:#**0000000343不完整,检测到后进行抛弃处理。调试技巧篇:
    对于已了解协议的支持串口产品,要想进行编程控制,可以使用“串口通讯控制器”进行调试,以摸清具体实现数据,可按如下步骤进行:
    1.确定硬件连接无误,这是首要条件,如果错误将没有成功的可能;
    连线必须正确,必要时可以使用计算机自带的多个端口相互进行测试,已保证硬件的连接无误。串口通讯线有9针和25针,多用9针,其中最重要的是2(RXD)、3(TXD)、5(GND)线,对应关系如下:
    9针 25针
    2 -- 3
    3 -- 2
    5 -- 72.确定通讯参数正确,如:波特率、奇偶校验位、数据位、停止位等,以及收发的是十六进制还是字符串:3.以上确保正确,则使用“串口通讯控制器”,按协议输入数据进行收发控制了。
    注意:有的仪器需要进行初始化,即先发一段激活指令,然后才能进入工作状态,这种设置主要是为了实现利用硬件为软件加密,即类似加密狗,需要有激活方法才行,不过该类方法使用较少。原创作者:JPYC,望业界专家多多指正!控件及例程源码请到:http://www.kaer.cn/default.aspx->下载基地