Re: strange problem with serial communication
From: Maverick (Maverick_at_maverick.com)
Date: 12/27/04
- Next message: Maverick: "Re: strange problem with serial communication"
- Previous message: Joe: "Re: output.c error in multithreaded program"
- In reply to: Crystal: "Re: strange problem with serial communication"
- Next in thread: Scott McPhillips [MVP]: "Re: strange problem with serial communication"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 27 Dec 2004 11:14:11 +0800
Solved! Thanks!
maverick
"Crystal" <linwingyee@hotmail.com> дÈëÓʼþ
news:OHHEM5Z6EHA.1392@tk2msftngp13.phx.gbl...
> Maybe Set the timeout as follow.. .. to see whether it is better.
>
> CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
> CommTimeOuts.ReadTotalTimeoutMultiplier = 100;
> CommTimeOuts.ReadTotalTimeoutConstant = 100;
> CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
> CommTimeOuts.WriteTotalTimeoutConstant = 0;
>
> "Maverick" <maverick@newsfan.net> wrote in message
> news:#9vv$JZ6EHA.540@TK2MSFTNGP12.phx.gbl...
> > Hi, all
> >
> > I met a big trouble. I am using MFC and multi-thread to write a serial
> port
> > communication program. Each time I send a string, I alwayse get part of
> it.
> > For example , I send a string of 120 characters, what I receive is a
> string
> > which contains only top 8 or 16 or 24 or 32 characters. I cannot receive
> the
> > same string at all. I've tried different baud rate, including
19200,9600,
> > but all failed. Below is part of my code.
> > /////////////////////////////////////////////////////
> > BOOL CSerial::SendData( const char *buffer, int size )
> > {
> > DWORD dwNumByteWritten;
> > if(!WriteFile(m_hIDComDev,buffer,size,&dwNumByteWritten,NULL))
> > {
> > DWORD dwError = GetLastError();
> > return FALSE;
> > }
> > return TRUE;
> > }
> > /////////////////////////////////////////////////////
> >
> > int CSerial::ReadData( char *data )
> > {
> > char Byte;
> > DWORD dwCommModemStatus,dwBytesTransferred;
> > int len = 0;
> > int buf_len;
> > unsigned long r_len = 0;
> >
> >
> > SetCommMask (m_hIDComDev, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD );
> >
> > if (m_hIDComDev!= INVALID_HANDLE_VALUE)
> > {
> > WaitCommEvent (m_hIDComDev, &dwCommModemStatus, 0);
> >
> >
> > GetCommMask (m_hIDComDev, EV_RXCHAR | EV_CTS | EV_DSR );
> >
> > if (dwCommModemStatus & EV_RXCHAR)
> > {
> > // Loop for waiting for the data.
> > do
> > {
> > // Read the data from the serial port.
> > ReadFile (m_hIDComDev,
> > &Byte,
> > 1,
> > &dwBytesTransferred,
> > 0
> > );
> >
> > // Display the data read.
> > if (dwBytesTransferred == 1)
> > {
> > data[len] = Byte;
> > data[len+1] = 0;
> > len++;
> > }
> > }
> > while (dwBytesTransferred == 1);
> > }
> > }
> >
> > return len;
> > }
> > /////////////////////////////////////////////////////
> > BOOL CSerial::Open( int nPort, int nBaud )
> > {
> > TCHAR szPort[15];
> > DCB dcb;
> >
> > wsprintf( szPort, _T("COM%d:"), nPort );
> > m_hIDComDev = CreateFile( szPort,
> > GENERIC_READ | GENERIC_WRITE,
> > 0,
> > NULL,
> > OPEN_EXISTING,
> > 0,
> > NULL
> > );
> >
> > if( m_hIDComDev == INVALID_HANDLE_VALUE)
> > {
> > DWORD dwError=GetLastError();
> > return( FALSE );
> > }
> > else
> > {
> > m_bOpened = TRUE;
> > }
> >
> > // Change the CommTimeOuts structure settings.
> > COMMTIMEOUTS CommTimeOuts;
> > GetCommTimeouts (szPort, &CommTimeOuts);
> >
> > CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
> > CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
> > CommTimeOuts.ReadTotalTimeoutConstant = 0;
> > CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
> > CommTimeOuts.WriteTotalTimeoutConstant = 0;
> >
> > if(!SetCommTimeouts( m_hIDComDev, &CommTimeOuts ))
> > {
> > DWORD dwError=GetLastError();
> > return FALSE;
> > }
> >
> > dcb.DCBlength = sizeof( DCB );
> > GetCommState( m_hIDComDev, &dcb );
> > dcb.BaudRate = nBaud;
> >
> > dcb.fBinary = TRUE; // Binary mode; no EOF check
> > dcb.fParity = TRUE; // Enable parity checking
> > dcb.fOutxCtsFlow = FALSE; // No CTS output flow control
> > dcb.fOutxDsrFlow = FALSE; // No DSR output flow control
> > dcb.fDtrControl = DTR_CONTROL_ENABLE;
> > // DTR flow control type
> > dcb.fDsrSensitivity = FALSE; // DSR sensitivity
> > dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx
> > dcb.fOutX = FALSE; // No XON/XOFF out flow control
> > dcb.fInX = TRUE; // No XON/XOFF in flow control
> > dcb.fErrorChar = FALSE; // Disable error replacement
> > dcb.fNull = FALSE; // Disable null stripping
> > dcb.fRtsControl = RTS_CONTROL_ENABLE;
> > // RTS flow control
> > dcb.fAbortOnError = FALSE; // Do not abort reads/writes on
> > // error
> > dcb.ByteSize = 8; // Number of bits/byte, 4-8
> > dcb.Parity = 0; // 0-4=no,odd,even,mark,space
> > dcb.StopBits = ONE5STOPBITS; // 0,1,2 = 1, 1.5, 2
> >
> >
> > if( !SetCommState( m_hIDComDev, &dcb )||
> > !SetupComm( m_hIDComDev, 1024, 1024 )/*||
> > m_OverlappedRead.hEvent == NULL ||
> > m_OverlappedWrite.hEvent == NULL*/ )
> > {
> > DWORD dwError = GetLastError();
> > CloseHandle( m_hIDComDev );
> > return( FALSE );
> > }
> >
> > m_bOpened = TRUE;
> >
> > return( m_bOpened );
> > }
> > /////////////////////////////////////////////////////
> > void PortReadThread(CScanDlg *dlg)
> > {
> > dlg->GetDlgItem(IDC_BUTTON_READ)->EnableWindow(FALSE);
> > dlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
> > if (Serial.Open(dlg->m_COM+1,9600) )//Open the port
> > {
> > char data[1024];
> > #ifdef _UNICODE
> > Serial.ReadData(data);
> > USES_CONVERSION;
> > dlg->m_szContent = A2W(data);
> > #else
> > Serial.ReadData(data);
> > dlg->m_szContent = data;
> > #endif
> > Serial.PortClose();
> > }
> > else
> > {
> > AfxMessageBox(_T("Open Port error."));
> > }
> >
> > dlg->GetDlgItem(IDC_BUTTON_READ)->EnableWindow(TRUE);
> > dlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
> > dlg->GetDlgItem(IDC_EDIT_CONTENT)->SetWindowText(dlg->m_szContent);
> > }
> >
>
////////////////////////////////////////////////////////////////////////////
> > void CScanDlg::OnButtonRead()
> > {
> > UpdateData(TRUE);
> >
> > HANDLE hReadThread;
> > DWORD dwThreadID;
> >
> > // Create a reading thread.
> > if (hReadThread = CreateThread
> > (NULL,0,(LPTHREAD_START_ROUTINE)PortReadThread, this, 0, &dwThreadID))
> > {
> > CloseHandle (hReadThread);
> > }
> > else
> > {
> > ::MessageBox (NULL, _T("Cannot create a reading thread."),
_T("Error"),
> > MB_OK);
> > }
> >
> > UpdateData(FALSE);
> > }
> > ///////////////////////////////////////////////////////////////////////
> >
> > I am using a X86 based board, running CE.net 4.2.
> >
> > Maverick
> >
> >
>
>
- Next message: Maverick: "Re: strange problem with serial communication"
- Previous message: Joe: "Re: output.c error in multithreaded program"
- In reply to: Crystal: "Re: strange problem with serial communication"
- Next in thread: Scott McPhillips [MVP]: "Re: strange problem with serial communication"
- Messages sorted by: [ date ] [ thread ]