COM1 COM2

COM1接口
  COM1与COM2接口也称串口,它是一个9针RS-232接口。它的数据的传输方式是采用串行传输,串口的最大传输速率为14.3KB/秒,通常用于传输速率较低的设备,如鼠标、外置MODEM、老式的数码相机、手写板。有些老主板上提供两个串口(9针、25针),而新主板一般是提供两个9针的串口。

COM2接口
  COM2接口的功能与COM1接口一样。不过在有些586主板上虽然也提供两个串口,但是COM1接口一般是接鼠标,COM2接口一般是接外置Modem或其他串口设备。

阅读更多
换一批

串口通信 com1com2之间通信

12-19

打开两个com:com3和com4,rncom3代码如下:rnrn#include rn#include rn#include rn#include rnrnHANDLE hComm;rnOVERLAPPED m_ov;rnCOMSTAT comstat;rnunsigned char *words;rnrnrnProcessErrorMessage(char* ErrorText)rnrn char *Temp = new char[200];rn LPVOID lpMsgBuf;rn FormatMessage(rn FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,rn NULL,rn GetLastError(),rn MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default languagern (LPTSTR) &lpMsgBuf,rn 0,rn NULLrn );rn sprintf(Temp, "WARNING: %s Failed with the following error: \n%s\nPort: %d\n", (char*)ErrorText, lpMsgBuf, "com2");rn MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP);rn LocalFree(lpMsgBuf);rn delete[] Temp;rn return true;rnrnrnbool openport(char *portname)rnrnrnhComm = CreateFile(portname,rn GENERIC_READ | GENERIC_WRITE,rn 0,rn 0,rn OPEN_EXISTING,rn FILE_FLAG_OVERLAPPED, rn 0);rnif (hComm == INVALID_HANDLE_VALUE)rnreturn FALSE;rnelsernreturn true;rnrnrnbool setupdcb(int rate_arg)rnrn DCB dcb;rn int rate= rate_arg;rn memset(&dcb,0,sizeof(dcb));rn if(!GetCommState(hComm,&dcb))rn rn ProcessErrorMessage("GetCommState()");rn return FALSE;rn rn /* -------------------------------------------------------------------- */rn // set DCB to configure the serial portrn dcb.DCBlength = sizeof(dcb);rn /* ---------- Serial Port Config ------- */rn dcb.BaudRate = rate;rn dcb.Parity = NOPARITY;rn dcb.fParity = 0;rn dcb.StopBits = ONESTOPBIT;rn dcb.ByteSize = 8;rn dcb.fOutxCtsFlow = 0;rn dcb.fOutxDsrFlow = 0;rn dcb.fDtrControl = DTR_CONTROL_DISABLE;rn dcb.fDsrSensitivity = 0;rn dcb.fRtsControl = RTS_CONTROL_DISABLE;rn dcb.fOutX = 0;rn dcb.fInX = 0;rn /* ----------------- misc parameters ----- */rn dcb.fErrorChar = 0;rn dcb.fBinary = 1;rn dcb.fNull = 0;rn dcb.fAbortOnError = 0;rn dcb.wReserved = 0;rn dcb.XonLim = 2;rn dcb.XoffLim = 4;rn dcb.XonChar = 0x13;rn dcb.XoffChar = 0x19;rn dcb.EvtChar = 0;rn /* -------------------------------------------------------------------- */rn // set DCBrn if(!SetCommState(hComm,&dcb))rn rn ProcessErrorMessage("SetCommState()");rn return false;rn rn elsern return true;rnrnrnbool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD ReadTotalconstant,DWORD WriteTotalMultiplier,DWORD WriteTotalconstant)rnrn COMMTIMEOUTS timeouts;rn timeouts.ReadIntervalTimeout=ReadInterval;rn timeouts.ReadTotalTimeoutConstant=ReadTotalconstant;rn timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier;rn timeouts.WriteTotalTimeoutConstant=WriteTotalconstant;rn timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;rn if(!SetCommTimeouts(hComm, &timeouts))rn rn ProcessErrorMessage("SetCommTimeouts()");rn return false;rn rn elsern return true;rnrnrnReceiveChar( )rnrnBOOL bRead = TRUE;rnBOOL bResult = TRUE;rnDWORD dwError = 0;rnDWORD BytesRead = 0;rnchar RXBuff;rnfor (;;)rnrn bResult = ClearCommError(hComm, &dwError, &comstat);rn if (comstat.cbInQue == 0)rn continue;rn if (bRead)rn rn bResult = ReadFile(hComm, // Handle to COMM portrn &RXBuff, // RX Buffer Pointerrn 1, // Read one bytern &BytesRead, // Stores number of bytes readrn &m_ov); // pointer to the m_ov structurern printf("%c",RXBuff);rn if (!bResult)rn rn switch (dwError = GetLastError())rn rn case ERROR_IO_PENDING:rn rn bRead = FALSE;rn break;rn rn default:rn rn break;rn rn rn rn elsern rn bRead = TRUE;rn rn // close if (bRead)rn if (!bRead)rn rn bRead = TRUE;rn bResult = GetOverlappedResult(hComm, // Handle to COMM portrn &m_ov, // Overlapped structurern &BytesRead, // Stores number of bytes readrn TRUE); // Wait flagrn rn rnreturn RXBuff;rnrnrnWriteChar(BYTE* m_szWriteBuffer,DWORD m_nToSend)rnrnBOOL bWrite = TRUE;rnBOOL bResult = TRUE;rnDWORD BytesSent = 0;rn//HANDLE m_hWriteEvent;rn//ResetEvent(m_hWriteEvent);rnif (bWrite)rnrnm_ov.Offset = 0;rnm_ov.OffsetHigh = 0;rn// Clear bufferrnbResult = WriteFile(hComm, // Handle to COMM Portrn m_szWriteBuffer, // Pointer to message buffer in calling finctionrn m_nToSend, // Length of message to sendrn &BytesSent, // Where to store the number of bytes sentrn &m_ov ); // Overlapped structurernif (!bResult)rnrnDWORD dwError = GetLastError();rnswitch (dwError)rnrncase ERROR_IO_PENDING:rnrn// continue to GetOverlappedResults()rnBytesSent = 0;rnbWrite = FALSE;rnbreak;rnrndefault:rnrn// all other error codesrnProcessErrorMessage("WriteFile()");rnrnrnrn // end if(bWrite)rnif (!bWrite)rnrnbWrite = TRUE;rnbResult = GetOverlappedResult(hComm, // Handle to COMM portrn &m_ov, // Overlapped structurern &BytesSent, // Stores number of bytes sentrn TRUE); // Wait flagrnrn// deal with the error codernif (!bResult)rnrnProcessErrorMessage("GetOverlappedResults() in WriteFile()");rnrn // end if (!bWrite)rnrn// Verify that the data size send equals what we tried to sendrnif (BytesSent != m_nToSend)rnrnprintf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)m_szWriteBuffer));rnrnreturn true;rnrnrnDWORD WINAPI MyThread1(LPVOID pParam)rnrnReceiveChar();rnreturn 0;rnrnrnDWORD WINAPI MyThread2(LPVOID pParam)rnrn while(hComm!=INVALID_HANDLE_VALUE) rn rn unsigned char d;rn d=getch();rn printf("%c",d);rn WriteChar(&d,1);rn rnreturn 0;rnrnrnrnvoid main()rnrnif(openport("com3"))rnprintf("open comport success\n");rnif(setupdcb(9600))rnprintf("setupDCB success\n");rnif(setuptimeout(0,0,0,0,0))rnprintf("setuptimeout success\n");rnPurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);rnwords=(unsigned char *)malloc(256); rnprintf("please input your data:\n");rnscanf("%s",words); rnrnWriteChar(words,20);rnprintf("received data:\n");rnReceiveChar( );rnHANDLE hThread1=CreateThread(NULL, 0, MyThread1, 0, 0, NULL); rnHANDLE hThread2=CreateThread(NULL, 0, MyThread2, 0, 0, NULL); rnCloseHandle(hThread1);rnCloseHandle(hThread2);rnsystem("pause");rnrnrnrn运行程序以后出现两个console框,在com3的"please input your data:"后面输入字符串,但是com4的console窗收不到发送的字符串,该如何解决呢?rn

没有更多推荐了,返回首页