c#串口write 用c++ 怎么解决
发布网友
发布时间:2022-04-12 14:24
我来回答
共2个回答
懂视网
时间:2022-04-12 18:45
SQL SELECT * FROM v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod PL/SQL Release 10.2.0.3.0
- Production SQL select name,parameter1,paramet
SQL> SELECT * FROM v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
- ProdPL/SQL Release 10.2.0.3.0 - Production
SQL>SELECT name,parameter1,parameter2,parameter3,wait_class from v$event_name
2 where name in ('free buffer waits','db file parallel write','write complete waits');
NAME
PARAMETER1 PARAMETER2 PARAMETER3 WAIT_CLASS
------------------------- ---------- ---------- ---------- ------------------------------
free buffer waits
file#
block#
set-id#
Configurationwrite complete waits
file#
block#
Configurationdb file parallel write
requests interrupt timeout System I/O
在Oracle 10g中,db file parallel write 被归属为系统I/0等待类,如果你检查到了系统出现了大量的
db file parallel write事件,表明你的I/O有问题。这时你可以检查
db file parallel write ,write complete waits,free buffer waits的平均等待时间
SQL> select event,time_waited,average_wait from v$system_event where event in
2 ('db file parallel write','write complete waits','free buffer waits');
EVENT
TIME_WAITED AVERAGE_WAIT
---------------------------------------------------------------- ----------- ------------
db file parallel write 284 .67
我这里由于是测试机器,没有出现 write complete waits,free buffer waits 等待事件
AVERAGE_WAIT的值应该小于1(百分之一秒),如果AVERAGE_WAIT大于10(十分之一秒),表明非常缓慢的I/O,出现了db file parallel write 时常也会伴随write complete waits等待,如果是非常繁忙的OLTP系统,还可能出现free buffer waits,因为缓慢的I/0势必影响前台进程。db file parallel write只属于DBWR进程独有,不要被名字迷惑了,不要误解为该事件与并行DML有关。
热心网友
时间:2022-04-12 15:53
c#用 API写串口 看这个文档
http://wenku.baidu.com/view/8c60db89a1116c175f0e7cd184254b35eefd1aef
用C++写
#include "ComControl.h"
CComControl *pObjComControl;
CComControl::CComControl(void)
{
InitCom();
}
CComControl::~CComControl(void)
{
ResumeThread(m_hThread);
SetEvent(m_hExit);
WaitForSingleObject(m_hThread,INFINITE);
m_hThread = NULL;
ClosePort();
CloseHandle(m_ovRead.hEvent);
CloseHandle(m_ovWrite.hEvent);
CloseHandle(m_hExit);
DeleteCriticalSection(&m_csWmThreshold);
}
UINT ComThreadProc(LPVOID para)
{
CComControl* pCom = (CComControl*)para;
DWORD dwEventMask;
OVERLAPPED eov;
DWORD dwErrWord;
COMSTAT comstat;
memset(&eov,0,sizeof(OVERLAPPED));
eov.hEvent = CreateEvent(NULL,false,false,NULL);
HANDLE hWaits[2];
DWORD dwWait;
hWaits[0] = eov.hEvent;
hWaits[1] = pCom->m_hExit;
while(true)
{
dwEventMask = 0;
WaitCommEvent(pCom->m_hCom,&dwEventMask,&eov);
dwWait = WaitForMultipleObjects(2,hWaits,false,INFINITE);
if (dwWait == WAIT_OBJECT_0+1)
break ;
if (pCom->m_bCanInterrupt == FALSE) continue;
ClearCommError(pCom->m_hCom,&dwErrWord,&comstat);
pCom->m_cbInQue = comstat.cbInQue;
pCom->m_cbOutQue = comstat.cbOutQue;
if(pCom->m_cbInQue == 0) continue;
if (dwEventMask & EV_ERR)
{
//emit com err;
CloseHandle(eov.hEvent);
}
if (dwEventMask & EV_TXEMPTY)
{
}
if (dwEventMask & EV_RXCHAR)
{
pCom->m_bCanInterrupt = false;
emit pCom->signalDataComming();
}
}
CloseHandle(eov.hEvent);
return 0;
}
void CComControl::InitCom()
{
InitializeCriticalSection(&m_csWmThreshold);
memset(m_szPortName,0,sizeof(m_szPortName));
memset(congjiaddr,0,10);
memset(&m_ovRead,0,sizeof(OVERLAPPED));
memset(&m_ovWrite,0,sizeof(OVERLAPPED));
m_ovRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); //注意true
m_ovWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);//see above
m_nThresHold = 0 ;
ChangeMode = false;// 默认为 noparity modbus下
m_bCanInterrupt = TRUE;
m_hExit = CreateEvent(NULL,FALSE,FALSE,NULL);
m_hCom = INVALID_HANDLE_VALUE;
m_cbInQue = 0;
m_cbOutQue = 0 ;
m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ComThreadProc,this,CREATE_SUSPENDED,0);
}
bool CComControl::OpenPort()
{
if (m_hCom != INVALID_HANDLE_VALUE)
{
ClosePort();
}
m_hCom = CreateFile(m_szPortName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if (m_hCom == INVALID_HANDLE_VALUE)
{
int errcode = GetLastError();
return false;
}
DCB dcb;
memset(&dcb, 0, sizeof(dcb));//
dcb.DCBlength= sizeof(dcb);// 以字节数指定当前的波特率
dcb.fBinary= true;// 必须是true
dcb.BaudRate= m_lBaudRate;// 当前的波特率
dcb.Parity= NOPARITY;// modbus 645有些区别 主要设置参数页面的 一个勾选
if(this->ChangeMode == false)
dcb.Parity= NOPARITY;// 奇偶校验方法,NOPARITY / ODDPARITY / EVENPARITY / MARKPARITY
else
dcb.Parity= EVENPARITY;
dcb.ByteSize= 8;// 指定端口当前使用的数据位数
dcb.StopBits= ONESTOPBIT;// 指定端口当前使用的停止位数 ONESTOPBIT / ONE5STOPBITS / TWOSTOPBITS
dcb.fDtrControl = DTR_CONTROL_ENABLE;// DTR_CONTROL_DISABLE置DTR为OFF,DTR_CONTROL_ENABLE置DTR为ON,DTR_CONTROL_HANDSHAKE允许DTR"握手"
dcb.fRtsControl = RTS_CONTROL_ENABLE;// RTS置OFF
dcb.fAbortOnError = true;// 有错误发生时中止读、写操作
dcb.fDsrSensitivity = false;// 若为TRUE,则DSR为OFF时接收的字节被忽略
dcb.wReserved1= 0;// 没有使用,必须为零
COMMTIMEOUTS timeouts; //定义超时时间
timeouts.ReadIntervalTimeout= 10*9600 / m_lBaudRate + 100;// 9600:110
timeouts.ReadTotalTimeoutConstant= 5000;
timeouts.ReadTotalTimeoutMultiplier = 10*9600 / m_lBaudRate;// 9600:10
timeouts.WriteTotalTimeoutConstant= 0;
timeouts.WriteTotalTimeoutMultiplier= 2*9600 / m_lBaudRate;// 9600:2
if(!SetCommState(m_hCom, &dcb) ||
!SetupComm(m_hCom, 200, 200) ||
!SetCommTimeouts(m_hCom, &timeouts) ||
!SetCommMask(m_hCom, EV_RXCHAR|EV_TXEMPTY|EV_ERR) ||
!PurgeComm(m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR )
)
{
ClosePort();
return false;
}
ResumeThread(m_hThread);
return true;
}
void CComControl::ClosePort()
{
if (m_hCom != INVALID_HANDLE_VALUE)
{
if (m_hThread)
SuspendThread(m_hThread);
PurgeComm(m_hCom,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(m_hCom);
m_hCom = INVALID_HANDLE_VALUE;
//update status
}
}
void CComControl::SetParam(const char* szPortname,long lBaudRate,int nParity)
{
if (pObjComControl->PortOpened())
{
if (m_lBaudRate == lBaudRate)
{
if(strstr(m_szPortName,szPortname))
{
return ; //打开过重复的
}
}
}
//以下记录上一次 打开的波特率 以及串口号
m_lBaudRate = lBaudRate;
memset(m_szPortName,0,20); //清
//strcpy_s(m_szPortName,"\\\\.\\");
strcat_s(m_szPortName,szPortname); // \\.\com1
if (m_hCom!= INVALID_HANDLE_VALUE)
{
ClosePort();
Sleep(500);
OpenPort();
}
}
void CComControl::ClearInOut()
{
if (m_hCom == INVALID_HANDLE_VALUE)
return ;
//PurgeComm(m_hCom,PURGE_TXABORT | PURGE_TXCLEAR);
PurgeComm(m_hCom,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
ResetBytes();
}
bool CComControl::IsWindowsNT()
{
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx(&osv);
return (osv.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
void CComControl::ResetBytes()
{
DWORD dwErrWord;
COMSTAT comstat;
ClearCommError(m_hCom,&dwErrWord,&comstat);
m_cbInQue = comstat.cbInQue;
m_cbOutQue = comstat.cbOutQue;
}
bool CComControl::Send(BYTE* pBuff,DWORD dwBytesSend)
{
if (!PortOpened()) return false;
BOOL wRes;
DWORD dwBytesW;
wRes = WriteFile(m_hCom,pBuff,dwBytesSend,&dwBytesW,&m_ovWrite);
if (!wRes)
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(m_ovWrite.hEvent,1000);
wRes = GetOverlappedResult(m_hCom,&m_ovWrite,&dwBytesW,false);
if (!wRes || dwBytesW !=dwBytesSend)
{
ClearInOut();
return false;
}
}
else
{
ClearInOut();
return false;
}
}
return true;
}
bool CComControl::Receive(BYTE* pBuff,DWORD dwBytesRead)
{
DWORD dwBytesReaded = 0;
return Receive(pBuff, dwBytesRead, dwBytesReaded);
return true;
}
bool CComControl::Receive(BYTE* pBuff,DWORD dwBytesRead,DWORD& dwBytesReaded)
{
dwBytesReaded = 0;
if(!PortOpened())return false;
BOOL rRes=true;
if(m_hCom == INVALID_HANDLE_VALUE)return false;
m_bCanInterrupt = FALSE; //不能被打断裂
if( !ReadFile( m_hCom,//handle of file to read
pBuff,//address of buffer that receives data
dwBytesRead,//number of bytes to read
&dwBytesReaded,//address of number of bytes read
&m_ovRead//address of structure for data
)
)
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(m_ovRead.hEvent, 1000);//this block the calling thread,if err or timeout it return
rRes = GetOverlappedResult(m_hCom, &m_ovRead, &dwBytesReaded, false);
/* 以前的代码 感觉 这里处理有点问题
if(!rRes || dwBytesReaded != dwBytesRead)
{// an error occurred, i think it most was timeout also it return when line error
m_bCanInterrupt = TRUE;
rRes = false;
}
*/
if(rRes<=0 && dwBytesReaded <=0 )
{// an error occurred, i think it most was timeout also it return when line error
m_bCanInterrupt = TRUE;
rRes = false;
}
else
{ // some other error occurred
m_bCanInterrupt = TRUE;
rRes = true;
}
}
}
//ResetBytes();
ClearInOut();
m_bCanInterrupt = TRUE;
if(rRes)
return true;
else
return false;
return true;
}
void CComControl::SetComOwner(HWND hOwner)
{
this->m_hOwner = hOwner;
}