读写串口c++ ---------(big帮助下完成)

34 篇文章 0 订阅
19 篇文章 0 订阅

作者:shangwei97

转自:http://blog.csdn.net/shangwei97/article/details/4574955


#include "com_232.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>


#ifdef NULL
#undef NULL
#endif
#define NULL 0
/*
#define MAX_BARCODE_LEN    64
#define MAX_STRING_LEN     1024
#define MAX_UI_STR_LEN           256
#define MAX_UI_STRING_LEN        256
#define MAX_FILE_READ_BUFF_SIZE  256
#define DOWNLOAD_FILE_FROM_PC_TO_CK1  0x10
#define UPLOAD_FILE_FROM_CK1_TO_PC    0x20
*/

#define VOID     void
#define CHAR     char            
#define WCHAR    short
#define BYTE     unsigned char   
#define LONG     long
#define ULONG    unsigned long
#define DOUBLE   double          
#define BOOL     long           
#define HRESULT  long           
#define HANDLE   void*
//       
#define TRUE          1
#define FALSE         0
#define MAX_CHAR      127
#define MIN_CHAR      -127
#define MAX_WCHAR     32767
#define MIN_WCHAR     -32768
#define MAX_BYTE      255
#define MIN_BYTE      0
#define MAX_LONG      2147483647
#define MIN_LONG      -2147483647
#define MAX_ULONG     4294967295
#define MIN_ULONG     0
#define MAX_DOUBLE   (1.7976931348623158e+308)
#define MIN_DOUBLE   (2.2204460492503131e-016)
#define HR_OK                  1
#define HR_FAIL                0
#define HR_FAIL_PARAMETER_ERR  -2000
#define MAX_ERR_COUNT     10
//...
#ifdef FAILED
#undef FAILED
#endif
#define FAILED(hr)              (hr<=0)
#define RESULT_OK(hr)           (hr>0)


#include <stdlib.h>
#include <stdio.h>

#define cs_delay(i_delay_ms) {;}
#define msg(x) {if(0)MessageBox(NULL,x,"",MB_OK);}


extern "C" HRESULT __declspec(dllexport) add(int x, int y)

{

return x + y;

}



HRESULT cs_open_232(const CHAR* pc_com_name,  //"/dev/ttyS2"
             LONG i_baud_rate,         //B115200
             LONG i_verify,            //N,E,O         
             LONG i_data_size,         //8,7
             LONG i_stop_bit,          //1
             LONG *pi_232_handle,
             LONG i_timeout_msc)
{
    HRESULT hr=HR_FAIL;
   
 LONG i_com_handle = 0L;
 DCB seri;
 DCB seri_temp;
 COMMTIMEOUTS tout;
    HANDLE hPort = NULL;

 hPort =CreateFile(pc_com_name,
                 GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
 if (hPort == INVALID_HANDLE_VALUE)  goto Exit;
 GetCommState(hPort, &seri);
 seri_temp = seri; 
 seri.BaudRate = i_baud_rate;
 //seri.BaudRate = CBR_9600;//CBR_38400;//CBR_9600;CBR_115200
 seri.ByteSize = i_data_size;            // Byte of the Data.
 seri.StopBits = ONESTOPBIT;   // Use one bit for stopbit.
 seri.Parity = NOPARITY;       // No parity bit
 SetCommState(hPort, &seri); 
 tout.ReadIntervalTimeout = MAXWORD;
 tout.ReadTotalTimeoutMultiplier = 0;
 tout.ReadTotalTimeoutConstant = i_timeout_msc;
 tout.WriteTotalTimeoutMultiplier = 0;
 tout.WriteTotalTimeoutConstant = i_timeout_msc;

 SetCommTimeouts(hPort, &tout);
 PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR);

    *pi_232_handle=(LONG)hPort;
    hr = HR_OK;
Exit: 
 return hr;
}
int cs_close_232(LONG pi_232_handle)
{

    if(0 <pi_232_handle)
    {
    // delete pi_232_handle;
   if(CloseHandle((void*)pi_232_handle))
   {
    pi_232_handle=0;
    return 1;
   }
   else
   {
  return 0;
   }
     
    }
 return 3;

}

CHAR pc_read[1024]; 
HRESULT  cs_read_232(CHAR **pbt_data, LONG i_232_handle)
{
    HRESULT hr=HR_FAIL;
    LONG i_read_count    = 0L;    
    LONG i_run_time_ms   = 0L;
    BOOL b_timeout_valid = FALSE;
    DWORD willReadLen = 0;
 char *temp=NULL;
 
 DWORD i_rdata_len = 0;
 DWORD dwReadError;
    // 获得读取串口的字节数
    COMSTAT cmState; 
 //CHAR ctemp[1024];
 //memset(ctemp,0x00,1024);
 ClearCommError((void*)i_232_handle,&dwReadError,&cmState);
 memset(pc_read,0x00,1024);
 for(;;)
 {
  willReadLen = cmState.cbInQue;
   
  if(0 > i_232_handle)goto Exit;  
  cs_delay(10);
  // 一次将串口返回的所有信息读取出来
    ReadFile((void*)i_232_handle,pc_read,willReadLen,(ULONG*)&i_read_count, NULL);
        // 如果读到 ">"则证明读到结尾字符
  temp = strstr(pc_read,"OK>");
  //memcpy(pc_read,ctemp,willReadLen);
     //strcat(pc_read,ctemp);
  
  i_rdata_len = strlen(pc_read);
  if(i_rdata_len == willReadLen)
  { 
   *pbt_data = pc_read;   
   break;  
  }
  //memset(ctemp,0x00,1024);
 }
    hr=HR_OK;  
Exit:
   return hr;           
}

HRESULT cs_write(const char *pt_data,LONG i_232_handle, LONG LENGTH)
{
    HRESULT hr=HR_FAIL;
 LONG i = 0L;
 LONG j = 0L;
    LONG i_buff_len = 0L;
    LONG i_send_ok_len = 0L;
    LONG i_err_code = 0L;
    //COMSTAT com_stat; 

    if(0>i_232_handle)goto Exit;
 if(NULL == pt_data)goto Exit;
 
 cs_delay(1);
 WriteFile( (void*)i_232_handle,
    pt_data,
    LENGTH,
    (ULONG*)&i_send_ok_len,
    NULL);
 if(LENGTH == i_send_ok_len)goto Exit;
   
 
 hr=HR_OK;
Exit:
    //ClearCommError((void*)i_232_handle, (ULONG*)&i_err_code, &com_stat);
    return hr;
}

 

文本框追加文本
  long lret = strlen(strTag);
  int   nLength   =   m_Edit.SendMessage(WM_GETTEXTLENGTH);  
  m_Edit.SetSel(nLength,   nLength);  
  m_Edit.ReplaceSel(strTag);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值