阅读:1210回复:6
唉!!还没解决!!!PDA通讯
HANDLE m_hComhandle;
DWORD dwWritten; COMSTAT CommStateInfo; unsigned long iErrorCode; DWORD dwRead; char pWrBuf[] = "#00?T*"; char pszData[100]; int nLen; int nRet = 0 ; CString szComPort = L"COM1:"; CString str; memset(pszData,0,100); //Open port m_hComhandle = CreateFile( szComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,//FILE_FLAG_OVERLAPPED,//|FILE_FLAG_NO_BUFFERING, NULL); if (INVALID_HANDLE_VALUE == m_hComhandle) { return; } BOOL bRet; bRet = SetupComm(m_hComhandle,32,32); if (!bRet) { CloseHandle(m_hComhandle); return; } DCB CommSettings = {0}; CommSettings.DCBlength = sizeof(DCB); bRet = GetCommState(m_hComhandle,&CommSettings); if (!bRet) { CloseHandle(m_hComhandle); return ; } COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(m_hComhandle, &timeouts)) { CloseHandle(m_hComhandle); return ; } CommSettings.DCBlength = sizeof(DCB); CommSettings.BaudRate = CBR_9600; CommSettings.ByteSize = 8; CommSettings.Parity = NOPARITY; CommSettings.StopBits = ONESTOPBIT; bRet = SetCommState(m_hComhandle,&CommSettings); if (!bRet) { CloseHandle(m_hComhandle); return; } //Write data nLen = strlen(pWrBuf); nRet = WriteFile(m_hComhandle,(void *)pWrBuf,nLen,&dwWritten,NULL); str.Format(L"write : %d",dwWritten); AfxMessageBox(str); //Read data Sleep(100); ClearCommError(m_hComhandle, &iErrorCode, &CommStateInfo); nLen = CommStateInfo.cbInQue; nRet = ReadFile(m_hComhandle,pszData,nLen,&dwRead,NULL); str.Format(L"Read : %d",dwRead); AfxMessageBox(str); //Close port CloseHand return; 上面的使我的PDA通讯程序,数据可以写下去,但读不上来。烦恼!!由于只有一个口,不能进行调试,所以中间用了些弹出筐!可以看到有数据写下去,但没数据上来。。高手兄弟请指教!! |
|
沙发#
发布于:2004-12-15 15:37
kevin_hou兄弟,你调试串口程序是在串口外部用自环连起来收发还是与其他设备的串口进行通讯?如果是前者有可能是读写同步问题。另外,串口驱动是你自己写的还是微软带的?
下面的这个帖子有关于串口通信的讨论 http://www.driverdevelop.com/forum/html_82172.html?1103095522 |
|
|
板凳#
发布于:2004-12-15 15:49
是与其他设备相连进行通讯。我在PC上也有个测试程序,可以度上述据来,改到CE 下就不行了。。谢谢1!程序是自己写的!
|
|
地板#
发布于:2004-12-15 15:56
另外,注册表里的到串口设置如下:
Irq 17 IoLen 36 DeviceArrayIndex 0 Prefix COM Order 2 Index 1 DeviceType 0 FriendName COM1上的串行电缆 |
|
地下室#
发布于:2004-12-16 10:09
有没有数据从PC端穿过来阿?你的这个程序先写后读,感觉是自环时用的啊
|
|
5楼#
发布于:2004-12-16 12:42
我用你的代码试了一下,改了个地方,它是能读出数据的
我在程序里加了一个Timer函数,每隔1妙读取一下串口,是可以读出来的 [编辑 - 12/16/04 by chang_nicolas] |
|
6楼#
发布于:2004-12-16 13:09
谢谢。问题解决了!!!!
|
|