免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1028 | 回复: 0
打印 上一主题 下一主题

帮忙修改串口程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-23 18:08 |只看该作者 |倒序浏览
我刚刚接触到编程。这是一个具有接受和发送数据功能的串口程序,有错误,我不会改,恳请各位高手帮我更正一下  谢谢! 急用!
#include "stdafx.h"
#include "stdio.h"
#include "resource.h"

#define MAX_LOADSTRING 100
#define BUFFER_SIZE 500
#define MSG(szMsg) MessageBox (NULL, szMsg , TEXT("Error", MB_OK)

       
HANDLE m_hCom;

BOOL  OpenSerial(LPCTSTR lpszDevice);
DWORD WriteDataSerial(LPCVOID pdata, DWORD  len);
DWORD ReadDataSerial (TCHAR ReceiveBuf[BUFFER_SIZE]);
void   CloseSerial();



int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
         
{   LONG  LastError=ERROR_SUCCESS;
    LastError=OpenSerial("COM1";
          if(LastError!=ERROR_SUCCESS)
          {
         MessageBox (NULL, TEXT("OpenSerial:COM1 failed",TEXT("Error", MB_OK);
             return FALSE;
          }
   LastError=WriteDataSerial("Hello World";
      if(LastError!=ERROR_SUCCESS)
          {
                  MessageBox(NULL, TEXT("WriteDataSerial:send data failed",TEXT("Error", MB_OK);
          return FALSE;
          }
   LastError=ReadDataSerial (TCHAR ReceiveBuf[BUFFER_SIZE]);
        DWORD dwBytes;                           
            const char *Byte = "Hello World";        
            ReadFile(m_hCom, &Byte,11, &dwBytes, 0);  
        if( LastError!=ERROR_SUCCESS)  
                {
                        MessageBox(NULL, TEXT("ReadDataSerial:read data failed",TEXT("Error", MB_OK);
                    return FALSE;
                }
    CloseSerial();
     return 0;  
}
BOOL OpenSerial(LPCTSTR lpszPortNum)
{
        DCB  dcb;        
              BOOL bSuccess;
       
        m_hCom = CreateFile(lpszPortNum,           
                            GENERIC_READ|GENERIC_WRITE,
                            FILE_SHARE_READ,      
                            NULL,               
                            OPEN_EXISTING,        
                            0,                     
                            NULL);                 


        if ( m_hCom == INVALID_HANDLE_VALUE )
        {
               
                MessageBox (NULL, TEXT("OpenSerial(): CreateFile() failed, invalid handle value!", TEXT("Error"), MB_OK);
                       
                return FALSE;
        }
       

        bSuccess = GetCommState(m_hCom, &dcb);

        if ( ! bSuccess )
        {

                MessageBox (NULL, TEXT("OpenSerial(): GetCommState() failed!"), TEXT("Error"), MB_OK);
                CloseSerial();

                return FALSE;
        }

       
       
       

        dcb.BaudRate = CBR_9600 ;
        dcb.ByteSize = 8 ;
        dcb.Parity   = NOPARITY ;
        dcb.StopBits = ONESTOPBIT ;
       
        bSuccess = SetCommState(m_hCom, &dcb);

        if ( ! bSuccess )
        {
               
                MessageBox (NULL, TEXT("OpenSerial(): SetCommState() failed!"), TEXT("Error"), MB_OK);
               
                CloseSerial();

                return FALSE ;
        }


          
       COMMTIMEOUTS  CommTimeouts;
       GetCommTimeouts(m_hCom, &CommTimeouts);
       CommTimeouts.ReadIntervalTimeout = MAXDWORD;  
       CommTimeouts.ReadTotalTimeoutMultiplier = 10;  
       CommTimeouts.ReadTotalTimeoutConstant = 10;   
       CommTimeouts.WriteTotalTimeoutMultiplier = 50;  
       CommTimeouts.WriteTotalTimeoutConstant = 100;   
       if (!SetCommTimeouts(m_hCom, &CommTimeouts))
       {
              return FALSE;
       }                 

        return TRUE ;
}


DWORD WriteDataSerial(LPCVOID pdata, DWORD   len)
{
        BOOL  bSuccess ;
        DWORD written = 0 ;

        if ( len < 1 )
                return(0);

        bSuccess = WriteFile(m_hCom,   
                             pdata,   
                             len,      
                             &written,
                             NULL);   


        if ( len != written )
        {

           MessageBox (NULL, TEXT("WriteDataSerial(): WriteFile() failed\n"), TEXT("Error"), MB_OK);  

           return(0);
        }


        return written;
}
DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE])
{
      
              
       HANDLE   hPort = m_hCom;
       BYTE     Byte;
       int    iCounter = 0;
       DWORD   dwBytes;
        
      
       SetCommMask (hPort, EV_RXCHAR|EV_TXEMPTY);  
          
             
      
           if (m_hCom!= INVALID_HANDLE_VALUE)
       {
                 
              DWORD   dwCommStatus;
              DWORD   dwlength;

              WaitCommEvent(hPort, &dwCommStatus, 0);
             
              
              
              do
              {
                     ReadFile(hPort, &Byte, dwlength, &dwBytes, 0);                        
                     if(dwBytes == dwlength)
                     {
                            ReceiveBuf[iCounter++] = Byte;
                                        
                                                       

                            if(iCounter == BUFFER_SIZE)
                            {
                                 
                                   MessageBox (NULL, TEXT("Error ReadDataSerial(): buffer full!\n"), TEXT("Error"), MB_OK);  
                                   return 0;
                            }
                     }                          
              } while (dwBytes == dwlength);

                          
              iCounter = 0;         
       }

       
        return 1;
}
void CloseSerial(void)
{
        if ( m_hCom >; 0 )
        {
                CloseHandle(m_hCom);
        }

        m_hCom = 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP