locked
Serial communication read and write problem RRS feed

  • Question

  • i have a problem with read and write. What i am doing is, system to system serial communication using MOXA cable. In one system i have used Hyper-terminal for serial communication and another system i have used my VC++ program application. if port is opened successfull then i want to receive what ever another system(hyper terminal) is sending data and if i pressed a send Button then another system(hyper terminal) have to receive what ever i am sending. Ansd vice-verse. in this process i could not receive and send data. I don't know where the problem is. can anyone please help me to find out the problem. 

    And my code is :

    void CSerial_comportDlg::Write(CString strSend)
    {
    	BOOL	bWriteRC;
    	DWORD	iBytesWritten;
    	DWORD	dwError;
    	char	sMsg[512];
    	CString sMsgStr;
    	LPCVOID lbuf = strSend; 
    
    	m_strStatus.SetString(_T("Writing"));
    	
    	iBytesWritten = 0;
    	bWriteRC = WriteFile(hComm, lbuf, 128, &iBytesWritten, NULL);
    
    	if (!bWriteRC || iBytesWritten == 0)
    	{
    		dwError = GetLastError();
    		
    		sprintf(sMsg, "Write of length query failed: RC=%d, "
    				"Bytes Written=%d, Error=%d",
    				bWriteRC, iBytesWritten, dwError);
    		sMsgStr = sMsg;
    		AfxMessageBox(sMsgStr);		
    	} // end if
    	/*else
    	{
    		MessageBox(_T("Written Successful"));
    	}*/
    }
    
    
    // data reading from port
    void CSerial_comportDlg::Read(CString& strReceive)
    {
    	BOOL	bReadRC; 
    	DWORD	iBytesRead;
    	DWORD	dwError;
    	char	sBuffer[128];
    	char	sMsg[512];
    	CString sMsgStr; 
    
    	memset(sBuffer,0,sizeof(sBuffer));
    	bReadRC = ReadFile(hComm, &sBuffer, strlen(sBuffer), &iBytesRead, NULL);
    
    	if (bReadRC && iBytesRead > 0)
    	{
    		strReceive = sBuffer;
    		SetDlgItemText(IDC_ERECEIVE, strReceive);
    	}
    	else
    	{
    		strReceive = "Read Failed";
    		SetDlgItemText(IDC_ERECEIVE, strReceive);
    
    		dwError = GetLastError();
    		
    		sprintf(sMsg, "Read length failed: RC=%d Bytes read=%d, "
    				"Error=%d ",
    				bReadRC, iBytesRead, dwError);
    		sMsgStr = sMsg;
    		AfxMessageBox(sMsgStr);
    	} // end if
    }
    
    
    void CSerial_comportDlg::OnBnClickedBopenport()
    {
    	// TODO: Add your control notification handler code here
    
    	int port_initialize_flag = 0;
    	UpdateData(TRUE);
    
    	//GetDlgItemText(IDC_CCOM, m_strCOM);
    	m_strCOM.GetString();
    
    	m_strPort = m_strCOM;
    
    	PortInitialize();
    
    	if(comport_initialize_flag == 1)
    	{
    		MessageBox(_T("Serial port opened"));
    		m_strStatus.SetString(_T("Opened"));
    		port_initialize_flag = 1;
    	}
    
    	GetDlgItem(IDC_BOPENPORT)->EnableWindow(FALSE);
    	GetDlgItem(IDC_BCLOSEPORT)->EnableWindow(TRUE);
    
    	if(port_initialize_flag == 1)
    	{
    		m_strStatus.SetString(_T("Reading"));
    		Read(m_strReceive);  // read data from port
    	}
    	UpdateData(FALSE);
    }
    
    void CSerial_comportDlg::OnBnClickedBsenddata()
    {
    	// TODO: Add your control notification handler code here
    	UpdateData(TRUE);
    
    	GetDlgItemText(IDC_ESEND, m_strSend);
    
    	Write(m_strSend);  //write data to port
    
    	UpdateData(FALSE);
    }


    Thanks & Regards Sasi ................. Go Green

    Friday, March 6, 2015 12:47 PM

Answers

  • are you using asynchronous I/O for your serial port?  Doing serial comms generally requires this.  There are some examples out there but since you're using C++ I recommend setting up a buffered I/O scheme that handles async I/O [a worker thread being ONE way of managing this].  Otherwise you'll be subject to only getting data when you call 'ReadFile' and only writing when you call 'WriteFile', and buffers *can* fill up and you lose data.  If this is just an app for testing you probably won't see this, but if it's a serious app you probably will.

    Serial I/O in windows is most certainly NOT for beginners...

    I even worked with TAPI, some years ago, and that was even MORE difficult.  "pick an available modem" then dial a phone number, then wait for connect, then do I/O with the remote device using whatever protocol it needed, transfer files, command the device to do things, etc..

    • Marked as answer by kvg sasi Thursday, March 12, 2015 9:39 AM
    Friday, March 6, 2015 7:09 PM
  • sorry now  i checked it. working properly.

    And also i edited the code :

    bReadRC = ReadFile(hComm, &sBuffer, 127, &iBytesRead, NULL);

    now it is working for write and i added one more button for read. If i pressed read button then it is reading. but without pressing any button i want to read whenever the port is opened.


    Thanks & Regards Sasi ................. Go Green



    • Edited by kvg sasi Friday, March 6, 2015 1:39 PM
    • Marked as answer by kvg sasi Thursday, March 12, 2015 11:40 AM
    Friday, March 6, 2015 1:15 PM

All replies

  • Have you tested with two HyperTerminals to make sure you cable is wired properly?

    jdweng

    Friday, March 6, 2015 1:08 PM
  • sorry now  i checked it. working properly.

    And also i edited the code :

    bReadRC = ReadFile(hComm, &sBuffer, 127, &iBytesRead, NULL);

    now it is working for write and i added one more button for read. If i pressed read button then it is reading. but without pressing any button i want to read whenever the port is opened.


    Thanks & Regards Sasi ................. Go Green



    • Edited by kvg sasi Friday, March 6, 2015 1:39 PM
    • Marked as answer by kvg sasi Thursday, March 12, 2015 11:40 AM
    Friday, March 6, 2015 1:15 PM
  • are you using asynchronous I/O for your serial port?  Doing serial comms generally requires this.  There are some examples out there but since you're using C++ I recommend setting up a buffered I/O scheme that handles async I/O [a worker thread being ONE way of managing this].  Otherwise you'll be subject to only getting data when you call 'ReadFile' and only writing when you call 'WriteFile', and buffers *can* fill up and you lose data.  If this is just an app for testing you probably won't see this, but if it's a serious app you probably will.

    Serial I/O in windows is most certainly NOT for beginners...

    I even worked with TAPI, some years ago, and that was even MORE difficult.  "pick an available modem" then dial a phone number, then wait for connect, then do I/O with the remote device using whatever protocol it needed, transfer files, command the device to do things, etc..

    • Marked as answer by kvg sasi Thursday, March 12, 2015 9:39 AM
    Friday, March 6, 2015 7:09 PM
  •  

    Thanks a lot big bad bombastic bob .  Yes i am using Non-overlapped serial communication. I tried with while loop but it always shows error message only and i could not click any other button.

    And i read Serial communication on win32 in msdn website also. really i am not getting what to do now. Can you please help me.

      

    Thanks & Regards Sasi ................. Go Green

    Tuesday, March 10, 2015 12:06 PM
  • Thanks a lot Pavel.

    Thanks & Regards Sasi ................. Go Green

    Wednesday, March 11, 2015 5:33 AM