13 个解决方案
#1
C#里是有这种接口类
C++你GOOGLE找找看吧
C++你GOOGLE找找看吧
#2
boost.asio似乎有串口
#3
HANDLE file = CreateFile("COM1", ....)
然后看这个 http://msdn.microsoft.com/en-us/library/aa363196(v=VS.85).aspx搞清楚怎么设串口参数,还有超时乱七八糟的
然后ReadFile(file, ...) WriteFile(file, ...)
然后看这个 http://msdn.microsoft.com/en-us/library/aa363196(v=VS.85).aspx搞清楚怎么设串口参数,还有超时乱七八糟的
然后ReadFile(file, ...) WriteFile(file, ...)
#4
+1
#5
打开串口CREATEFILE()
楼主看看
DWORD dwThreadID;
if(hFTTPCom != INVALID_HANDLE_VALUE) //串口已经被打开,不能再次打开
{
return FALSE;
}
CString strCom;
strCom.Format(_T("\\\\.\\%s"), pComm);//add by xiaolin.
//hFTTPCom = ::CreateFile(pComm,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );
hFTTPCom = ::CreateFile(strCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );//modified by xiaolin.
if(hFTTPCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("COM1 open fail!"));
return FALSE;
}
//AfxMessageBox(_T("COM1 open SUCCESS!"));
ASSERT(hFTTPCom!=INVALID_HANDLE_VALUE); //检测打开串口操作是否成功
SetCommMask(hFTTPCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm( hFTTPCom, 1024,512) ; //设置输入、输出缓冲区的大小
PurgeComm( hFTTPCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区
COMMTIMEOUTS CommTimeOuts ; //定义超时结构,并填写该结构
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = -1;
CommTimeOuts.ReadTotalTimeoutMultiplier = -1;
CommTimeOuts.WriteTotalTimeoutConstant = -1;
CommTimeOuts.WriteTotalTimeoutMultiplier = -1;
SetCommTimeouts( hFTTPCom, &CommTimeOuts ) ;//设置读写操作所允许的超时
DCB dcb ; // 定义数据控制块结构
GetCommState(hFTTPCom, &dcb ); //读串口原来的参数设置
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
dcb.fBinary = TRUE ;
dcb.fParity = FALSE;
SetCommState(hFTTPCom, &dcb ) ; //串口参数配置
hMutexComTx = CreateMutex(NULL,FALSE,NULL);
memset(&olWrite,0,sizeof(OVERLAPPED));
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hCommWatchThread = ::CreateThread( (LPSECURITY_ATTRIBUTES) NULL, //安全属性
0, //初始化线程栈的大小,缺省为与主线程大小相同
(LPTHREAD_START_ROUTINE)CommWatchProc, //线程的全局函数
hSendWnd, //此处传入了主框架的句柄
0, &dwThreadID );
ASSERT(hCommWatchThread!=NULL);
SetThreadPriority(hFTTPOLLThread,THREAD_PRIORITY_HIGHEST);
hMainWnd = hSendWnd;
return TRUE;
楼主看看
DWORD dwThreadID;
if(hFTTPCom != INVALID_HANDLE_VALUE) //串口已经被打开,不能再次打开
{
return FALSE;
}
CString strCom;
strCom.Format(_T("\\\\.\\%s"), pComm);//add by xiaolin.
//hFTTPCom = ::CreateFile(pComm,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );
hFTTPCom = ::CreateFile(strCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );//modified by xiaolin.
if(hFTTPCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("COM1 open fail!"));
return FALSE;
}
//AfxMessageBox(_T("COM1 open SUCCESS!"));
ASSERT(hFTTPCom!=INVALID_HANDLE_VALUE); //检测打开串口操作是否成功
SetCommMask(hFTTPCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm( hFTTPCom, 1024,512) ; //设置输入、输出缓冲区的大小
PurgeComm( hFTTPCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区
COMMTIMEOUTS CommTimeOuts ; //定义超时结构,并填写该结构
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = -1;
CommTimeOuts.ReadTotalTimeoutMultiplier = -1;
CommTimeOuts.WriteTotalTimeoutConstant = -1;
CommTimeOuts.WriteTotalTimeoutMultiplier = -1;
SetCommTimeouts( hFTTPCom, &CommTimeOuts ) ;//设置读写操作所允许的超时
DCB dcb ; // 定义数据控制块结构
GetCommState(hFTTPCom, &dcb ); //读串口原来的参数设置
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
dcb.fBinary = TRUE ;
dcb.fParity = FALSE;
SetCommState(hFTTPCom, &dcb ) ; //串口参数配置
hMutexComTx = CreateMutex(NULL,FALSE,NULL);
memset(&olWrite,0,sizeof(OVERLAPPED));
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hCommWatchThread = ::CreateThread( (LPSECURITY_ATTRIBUTES) NULL, //安全属性
0, //初始化线程栈的大小,缺省为与主线程大小相同
(LPTHREAD_START_ROUTINE)CommWatchProc, //线程的全局函数
hSendWnd, //此处传入了主框架的句柄
0, &dwThreadID );
ASSERT(hCommWatchThread!=NULL);
SetThreadPriority(hFTTPOLLThread,THREAD_PRIORITY_HIGHEST);
hMainWnd = hSendWnd;
return TRUE;
#6
学习了,现在正做这一块。
#8
#9
呃 呵呵……有点蒙……仔细看看~~
谢谢各位!
谢谢各位!
#10
就是通过API读取!
#11
cat /dev/ttyS0
#12
楼主是做游戏服务器吗 ?我最近也有这样的需要,准备利用串口出来请求~~
#13
5楼好身手啊,感谢
#1
C#里是有这种接口类
C++你GOOGLE找找看吧
C++你GOOGLE找找看吧
#2
boost.asio似乎有串口
#3
HANDLE file = CreateFile("COM1", ....)
然后看这个 http://msdn.microsoft.com/en-us/library/aa363196(v=VS.85).aspx搞清楚怎么设串口参数,还有超时乱七八糟的
然后ReadFile(file, ...) WriteFile(file, ...)
然后看这个 http://msdn.microsoft.com/en-us/library/aa363196(v=VS.85).aspx搞清楚怎么设串口参数,还有超时乱七八糟的
然后ReadFile(file, ...) WriteFile(file, ...)
#4
+1
#5
打开串口CREATEFILE()
楼主看看
DWORD dwThreadID;
if(hFTTPCom != INVALID_HANDLE_VALUE) //串口已经被打开,不能再次打开
{
return FALSE;
}
CString strCom;
strCom.Format(_T("\\\\.\\%s"), pComm);//add by xiaolin.
//hFTTPCom = ::CreateFile(pComm,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );
hFTTPCom = ::CreateFile(strCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );//modified by xiaolin.
if(hFTTPCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("COM1 open fail!"));
return FALSE;
}
//AfxMessageBox(_T("COM1 open SUCCESS!"));
ASSERT(hFTTPCom!=INVALID_HANDLE_VALUE); //检测打开串口操作是否成功
SetCommMask(hFTTPCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm( hFTTPCom, 1024,512) ; //设置输入、输出缓冲区的大小
PurgeComm( hFTTPCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区
COMMTIMEOUTS CommTimeOuts ; //定义超时结构,并填写该结构
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = -1;
CommTimeOuts.ReadTotalTimeoutMultiplier = -1;
CommTimeOuts.WriteTotalTimeoutConstant = -1;
CommTimeOuts.WriteTotalTimeoutMultiplier = -1;
SetCommTimeouts( hFTTPCom, &CommTimeOuts ) ;//设置读写操作所允许的超时
DCB dcb ; // 定义数据控制块结构
GetCommState(hFTTPCom, &dcb ); //读串口原来的参数设置
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
dcb.fBinary = TRUE ;
dcb.fParity = FALSE;
SetCommState(hFTTPCom, &dcb ) ; //串口参数配置
hMutexComTx = CreateMutex(NULL,FALSE,NULL);
memset(&olWrite,0,sizeof(OVERLAPPED));
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hCommWatchThread = ::CreateThread( (LPSECURITY_ATTRIBUTES) NULL, //安全属性
0, //初始化线程栈的大小,缺省为与主线程大小相同
(LPTHREAD_START_ROUTINE)CommWatchProc, //线程的全局函数
hSendWnd, //此处传入了主框架的句柄
0, &dwThreadID );
ASSERT(hCommWatchThread!=NULL);
SetThreadPriority(hFTTPOLLThread,THREAD_PRIORITY_HIGHEST);
hMainWnd = hSendWnd;
return TRUE;
楼主看看
DWORD dwThreadID;
if(hFTTPCom != INVALID_HANDLE_VALUE) //串口已经被打开,不能再次打开
{
return FALSE;
}
CString strCom;
strCom.Format(_T("\\\\.\\%s"), pComm);//add by xiaolin.
//hFTTPCom = ::CreateFile(pComm,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );
hFTTPCom = ::CreateFile(strCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL );//modified by xiaolin.
if(hFTTPCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox(_T("COM1 open fail!"));
return FALSE;
}
//AfxMessageBox(_T("COM1 open SUCCESS!"));
ASSERT(hFTTPCom!=INVALID_HANDLE_VALUE); //检测打开串口操作是否成功
SetCommMask(hFTTPCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm( hFTTPCom, 1024,512) ; //设置输入、输出缓冲区的大小
PurgeComm( hFTTPCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区
COMMTIMEOUTS CommTimeOuts ; //定义超时结构,并填写该结构
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = -1;
CommTimeOuts.ReadTotalTimeoutMultiplier = -1;
CommTimeOuts.WriteTotalTimeoutConstant = -1;
CommTimeOuts.WriteTotalTimeoutMultiplier = -1;
SetCommTimeouts( hFTTPCom, &CommTimeOuts ) ;//设置读写操作所允许的超时
DCB dcb ; // 定义数据控制块结构
GetCommState(hFTTPCom, &dcb ); //读串口原来的参数设置
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
dcb.fBinary = TRUE ;
dcb.fParity = FALSE;
SetCommState(hFTTPCom, &dcb ) ; //串口参数配置
hMutexComTx = CreateMutex(NULL,FALSE,NULL);
memset(&olWrite,0,sizeof(OVERLAPPED));
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hCommWatchThread = ::CreateThread( (LPSECURITY_ATTRIBUTES) NULL, //安全属性
0, //初始化线程栈的大小,缺省为与主线程大小相同
(LPTHREAD_START_ROUTINE)CommWatchProc, //线程的全局函数
hSendWnd, //此处传入了主框架的句柄
0, &dwThreadID );
ASSERT(hCommWatchThread!=NULL);
SetThreadPriority(hFTTPOLLThread,THREAD_PRIORITY_HIGHEST);
hMainWnd = hSendWnd;
return TRUE;
#6
学习了,现在正做这一块。
#7
#8
#9
呃 呵呵……有点蒙……仔细看看~~
谢谢各位!
谢谢各位!
#10
就是通过API读取!
#11
cat /dev/ttyS0
#12
楼主是做游戏服务器吗 ?我最近也有这样的需要,准备利用串口出来请求~~
#13
5楼好身手啊,感谢