// serial.h
#ifndef __serial_h__
#define __serial_h__
#define fc_dtrdsr 0x01
#define fc_rtscts 0x02
#define fc_xonxoff 0x04
#define ascii_bel 0x07
#define ascii_bs 0x08
#define ascii_lf 0x0a
#define ascii_cr 0x0d
#define ascii_xon 0x11
#define ascii_xoff 0x13
class cserial
public:
cserial();
~cserial();
bool open( int nport = 2, int nbaud = 9600 );
bool close( void );
int readdata( void *, int );
int senddata( const char *, int );
int readdatawaiting( void );
bool isopened( void )
protected:
bool writecommbyte( unsigned char );
handle m_hidcomdev;
bool m_bopened;
#endif
// serial.cpp
#include "stdafx.h"
#include "serial.h"
cserial::cserial()
m_hidcomdev = null;
m_bopened = false;
cserial::~cserial()
close();
bool cserial::open( int nport, int nbaud )
if( m_bopened ) return( true );
char szport[15];
char szcomparams[50];
dcb dcb;
wsprintf( szport, "com%d", nport );
if( m_hidcomdev == null ) return( false );
commtimeouts commtimeouts;
commtimeouts.readintervaltimeout = 0xffffffff;
commtimeouts.readtotaltimeoutmultiplier = 0;
commtimeouts.readtotaltimeoutconstant = 0;
commtimeouts.writetotaltimeoutmultiplier = 0;
commtimeouts.writetotaltimeoutconstant = 5000;
setcommtimeouts( m_hidcomdev, &commtimeouts );
wsprintf( szcomparams, "com%d:%d,n,8,1", nport, nbaud );
dcb.dcblength = sizeof( dcb );
getcommstate( m_hidcomdev, &dcb );
dcb.baudrate = nbaud;
dcb.bytesize = 8;
unsigned char ucset;
ucset = (unsigned char) ( ( fc_rtscts & fc_dtrdsr ) != 0 );
ucset = (unsigned char) ( ( fc_rtscts & fc_rtscts ) != 0 );
ucset = (unsigned char) ( ( fc_rtscts & fc_xonxoff ) != 0 );
if( !setcommstate( m_hidcomdev, &dcb ) ||
!setupcomm( m_hidcomdev, 10000, 10000 ) ||
dword dwerror = getlasterror();
closehandle( m_hidcomdev );
return( false );
m_bopened = true;
return( m_bopened );
bool cserial::close( void )
if( !m_bopened || m_hidcomdev == null ) return( true );
closehandle( m_hidcomdev );
m_bopened = false;
m_hidcomdev = null;
return( true );
bool cserial::writecommbyte( unsigned char ucbyte )
bool bwritestat;
dword dwbyteswritten;
if( !bwritestat && ( getlasterror() == error_io_pending ) ){
else{
return( true );
int cserial::senddata( const char *buffer, int size )
if( !m_bopened || m_hidcomdev == null ) return( 0 );
dword dwbyteswritten = 0;
int i;
for( i=0; iwritecommbyte( buffer[i] );
dwbyteswritten++;
return( (int) dwbyteswritten );
int cserial::readdatawaiting( void )
if( !m_bopened || m_hidcomdev == null ) return( 0 );
dword dwerrorflags;
comstat comstat;
clearcommerror( m_hidcomdev, &dwerrorflags, &comstat );
return( (int) comstat.cbinque );
int cserial::readdata( void *buffer, int limit )
if( !m_bopened || m_hidcomdev == null ) return( 0 );
bool breadstatus;
dword dwbytesread, dwerrorflags;
comstat comstat;
clearcommerror( m_hidcomdev, &dwerrorflags, &comstat );
if( !comstat.cbinque ) return( 0 );
dwbytesread = (dword) comstat.cbinque;
if( limit < (int) dwbytesread ) dwbytesread = (dword) limit;
if( !breadstatus ){
if( getlasterror() == error_io_pending ){
return( (int) dwbytesread );
return( 0 );
return( (int) dwbytesread );
C 非同步資料接收串列埠操作類
使用c 呼叫傳統32位api實現串列埠操作,整個結構特別的簡單。接收資料只需要定義資料接收事件即可。using system using system.runtime.interopservices namespace ibms.tool.io this.recvdata recvdata 返回接收...
C 非同步資料接收串列埠操作類
使用c 呼叫傳統32位api實現串列埠操作,整個結構特別的簡單。接收資料只需要定義資料接收事件即可。上傳源 我不會,需要源 的請與我 dyj057 gmail.聯絡。你也可以教我怎麼上傳源 using system using system.runtime.interopservices names...
非同步fifo的讀寫
這裡不討論非同步fifo是如何實現的,而是在實現fifo的前提下,對fifo的讀寫。如下 接收到的資料位00.可能是時序約束問題,在之前我確實沒有約束。改了解碼的編寫後就好了,在此,沒有解碼必須是1個1的解碼,不然就會出錯。但是,位址不夠,我要如何改,現在還不確定。但是解碼必須是1 個 1.控制三態...