This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

MicroController Connection

Any one know how my program should be like when i need to pass in information for example "a" and display it in a display box... through serial communication.

Parents
  • erm... i did the codings... its like this...

    
    void CSerialComm::Send(HANDLE *pComm, CString str)
    {
    	unsigned char StopByte=0x1;
    	unsigned long int n_out;
    
    	str = "0x80";
    	WriteFile(*pComm, str, str.GetLength(),&n_out , NULL);
    	WriteFile(*pComm, &StopByte, 1, &n_out, NULL);
    }
    
    void CSerialComm::Receive(HANDLE *pComm, CString str1)
    {
    	char cRxByte;
    	DWORD dwRead;
    
    	while(cRxByte!=0x1)
    	{
    		ReadFile(*pComm, &cRxByte, 1, &dwRead, NULL);
    		str1+=cRxByte;
    	}
    	MessageBox("Acknowledge Received");
    	//	MessageBox(str1);
    }
    
    void CSerialComm::initComm(HANDLE *pComm)
    {
    //	HANDLE hComm;
    
    //	hComm = CreateFile( "COM1:",
    //                    GENERIC_READ | GENERIC_WRITE,
    //                    0,
    //                    0,
    //                    OPEN_EXISTING,
    //                    FILE_FLAG_OVERLAPPED,
    //                  0);
    //	if (hComm == INVALID_HANDLE_VALUE)
    //	{
    //	}
       // error opening port; abort
    
    
    
    	*pComm = CreateFile("COM1:", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    	if (*pComm ==INVALID_HANDLE_VALUE)
    	{
    		//MessageBox("Please input value !");
    	}
    
    	//FillMemory(&dcb, sizeof(dcb), 0);
    	dcb.DCBlength = sizeof(dcb);
    	dcb.BaudRate = CBR_9600;
    	dcb.fBinary = TRUE;
    	dcb.fDtrControl = DTR_CONTROL_DISABLE;
    	dcb.fRtsControl = RTS_CONTROL_DISABLE;
    	dcb.ByteSize = 8;
    	dcb.Parity = NOPARITY;
    	dcb.StopBits = ONESTOPBIT;
    
    	if (!SetCommState(*pComm, &dcb))
    	{
    		//MessageBox("Wrong Port");
    	}
    }
    

    it can be executed without connecting the mc. but after the mc is connected, the program hangs

    can tell me wats wrong...

Reply
  • erm... i did the codings... its like this...

    
    void CSerialComm::Send(HANDLE *pComm, CString str)
    {
    	unsigned char StopByte=0x1;
    	unsigned long int n_out;
    
    	str = "0x80";
    	WriteFile(*pComm, str, str.GetLength(),&n_out , NULL);
    	WriteFile(*pComm, &StopByte, 1, &n_out, NULL);
    }
    
    void CSerialComm::Receive(HANDLE *pComm, CString str1)
    {
    	char cRxByte;
    	DWORD dwRead;
    
    	while(cRxByte!=0x1)
    	{
    		ReadFile(*pComm, &cRxByte, 1, &dwRead, NULL);
    		str1+=cRxByte;
    	}
    	MessageBox("Acknowledge Received");
    	//	MessageBox(str1);
    }
    
    void CSerialComm::initComm(HANDLE *pComm)
    {
    //	HANDLE hComm;
    
    //	hComm = CreateFile( "COM1:",
    //                    GENERIC_READ | GENERIC_WRITE,
    //                    0,
    //                    0,
    //                    OPEN_EXISTING,
    //                    FILE_FLAG_OVERLAPPED,
    //                  0);
    //	if (hComm == INVALID_HANDLE_VALUE)
    //	{
    //	}
       // error opening port; abort
    
    
    
    	*pComm = CreateFile("COM1:", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    	if (*pComm ==INVALID_HANDLE_VALUE)
    	{
    		//MessageBox("Please input value !");
    	}
    
    	//FillMemory(&dcb, sizeof(dcb), 0);
    	dcb.DCBlength = sizeof(dcb);
    	dcb.BaudRate = CBR_9600;
    	dcb.fBinary = TRUE;
    	dcb.fDtrControl = DTR_CONTROL_DISABLE;
    	dcb.fRtsControl = RTS_CONTROL_DISABLE;
    	dcb.ByteSize = 8;
    	dcb.Parity = NOPARITY;
    	dcb.StopBits = ONESTOPBIT;
    
    	if (!SetCommState(*pComm, &dcb))
    	{
    		//MessageBox("Wrong Port");
    	}
    }
    

    it can be executed without connecting the mc. but after the mc is connected, the program hangs

    can tell me wats wrong...

Children