Go to Content



Library

Home > Library

Serial communication between a bada app and a PC app

By bada Master | 3667 views | Jan 18, 2012

If you connect a bada device and a PC with a USB cable, a serial port is created for communicating between the bada device and the PC. Users can communicate between a PC application and a bada application and control them using the serial port since bada 1.2.
This document describes how to run the Echo application of a bada device on a PC application and to exchange data between the bada application and the PC application using the Echo application. Although the Echo application is a very simple application that runs on a bada device that receives data from a PC and sends it back to the PC again, the data reception module of the PC application is implemented using a Thread because the PC application may receive unexpected data from the device.

1. bada Application
  • 1) Configuring the application
    To run a bada application via serial communication, you have to add the Serial Launch Condition option to the bada application.
    You can change the setting by selecting Properties > bada Build > Application Information and changing the Launch Conditions settings. The Launch Conditions in the Application Information is disabled by default. To enable this item, the APP_OPERATION privilege is required in the Manifest.

    To enable the Launch Conditions of the Application Information, add APP_OPERATION to the Privilege of the application Manifest.


    The Launch Conditions field after adding the APP_OPERATION privilege to the Manifest

    If you click the Add button next to the Launch Conditions field, a window appears where you can add a condition as shown by the figure below. Click the combo box below the Condition, select Serial from the combo box and enter a command. (Although "AccessaryApp" has been entered here as an example, users can enter any command.)


    The screen after a launch condition is added

  • 2) For the serial communication of a bada application, the Osp::Io::SerialPort and Osp::Io::ISerialPortEventLister event listeners are required. Create a SerialPort and connect to the listener when starting an application.
    // Header
    class Form1 :
    	public Osp::Ui::Controls::Form,
    	public Osp::Io::ISerialPortEventListener,
    .
    .
    .
    	Osp::Io::SerialPort* __pSerialPort;
    // ISerialEventListener
    	virtual void  OnSerialPortDataReceivedN	(Osp::Base::ByteBuffer &byteBuffer);
    	virtual void  OnSerialPortErrorOccured (result r);
    }
    
    // OnInitailizing
    __pSerialPort = new SerialPort();
    __pSerialPort->Construct(*this);
    
  • 3) In the SerialEventListener, write the code for the data transmission and reception.
    void
    Form1::OnSerialPortDataReceivedN (Osp::Base::ByteBuffer &byteBuffer)
    {
    // Display the received data on the pre-created label.
    	String* str = new String((const char*)byteBuffer.GetPointer());
    	__pLabel->SetText(L"Receive Data : " + *str);
    	__pLabel->RequestRedraw();
    
    // Transmit the received data as they are through serial communication.
    	__pSerialPort->Write(byteBuffer);
    
    }
    
2. PC Serial Communications Program

This section describes the PC application that communicates serially with a bada device. The descriptions in this section are based on an application using MFC (Source code development in Visual Studio 2010).

  • 1) Checking the serial port of the device
    If you connect a bada device to a PC, you can confirm that the serial port is connected in the Device Manager on the PC as shown by the figure below. Since the port number is subject to change depending on the used USB ports and the device type, you have to check the port as follows.


    The connected serial port information in the Device Manager

    Although it seems that only one port is connected as shown by the figure above, if you select run as > bada Target Application in the bada SDK, you can confirm that two serial ports are used in the Console as shown below.


    The serial ports that are used when the Target is executed in the bada SDK

    In this example, the serial port that we will use is not COM5 that was displayed in the Device Manager but COM6.

  • 2) The following is the implemented PC program. It supports the serial port number setting, message input and reception log output.

  • 3) Enter the serial port number in the Set Serial Port field (Enter "6" because the port number confirmed in Step 1 was COM6) and click the Connect button to connect to the port.
    // Here, Port Open:m_sPortName is COM6.
    m_hComm = CreateFile(m_sPortName, GENERIC_READ | GENERIC_WRITE,0,NULL,	OPEN_EXISTING,	FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL);
    
    // Set the event and the input and output sizes of the port to be used and initialize the port buffer.
    SetCommMask(m_hComm,EV_RXCHAR);
    SetupComm( m_hComm,  m_intInQueue,m_intOutQueue);	
    PurgeComm( m_hComm,  PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);
    
    // Set the TimeOut for the read and write time for the buffer
    timeouts.ReadIntervalTimeout		= 0xFFFFFFFF;
    timeouts.ReadTotalTimeoutMultiplier = 0;
    timeouts.ReadTotalTimeoutConstant	= 0;
    timeouts.WriteTotalTimeoutMultiplier= 2*CBR_115200 / dwBaud; //9600
    timeouts.WriteTotalTimeoutConstant	= 0;
    
    SetCommTimeouts( m_hComm, &timeouts);
    
    // Set the port.
    dcb.DCBlength = sizeof(DCB);	
    GetCommState( m_hComm, &dcb);	
    	
    dcb.BaudRate		= dwBaud;   //9600	
    dcb.ByteSize		= 8;		
    dcb.Parity			= 0;		
    dcb.StopBits		= 1;		
    
    SetCommState(m_hComm, &dcb);
    

    For the DCB, enter the bada serial port information displayed in the Device Manager.

  • 4) Use an OVERLAPPED function to send and receive data from the serial port and configure it as follows.
    OVERLAPPED m_osWrite, m_osRead;
    
    m_osWrite.Offset = 0;
    m_osWrite.OffsetHigh = 0;
    m_osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    
    m_osRead.Offset = 0;
    m_osRead.OffsetHigh = 0;
    m_osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    
  • 5) The bada application and the PC program receive the data transmitted from the PC as is. However, because unexpected data may be received due to a status change of the device (the screen being locked or unlocked) before running the Echo application that connects to the device, the receiving module should be implemented using a Thread. When data is received, the data size is checked, the data is received and then the size is displayed on the screen.
    HANDLE		m_hReadThread
    m_hReadThread = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ReadThreadFunc,this,0,	&dwThreadID );
    
    // The ReadThreadFunc function is implemented using a Thread. 
    DWORD	ReadThreadFunc(SerialMgr* pSerialMgr)
    {
    DWORD		dwEvent;
    BOOL		bOk = TRUE;		 
    BYTE		buff[MAXBUFFSIZE];	
    DWORD		dwBufferCounter;		
    if (! SetCommMask( pSerialMgr->m_hComm, EV_RXCHAR)) bOk = FALSE;
    if (!bOk)  return FALSE;	
    
    while (pSerialMgr->m_bPortOpen){
    dwEvent = 0;
    // Wait for a reception event.
    WaitCommEvent(pSerialMgr->m_hComm, &dwEvent, NULL);
    if((dwEvent & EV_RXCHAR) == EV_RXCHAR){ 
    do{
    // If there is received data, the serial port is read. 
    dwBufferCounter = pSerialMgr->ReadData(buff);
    // This function outputs the reception log in the log window.
    pSerialMgr->SendDataToDialog( buff, (BYTE)dwBufferCounter )   
    }while (dwBufferCounter);
    }
    }
    
    pSerialMgr->m_hReadThread = NULL;
    return TRUE;
    }
    
    // This is the function that is called from ReadThreadFunc to read the serial port.
    DWORD SerialMgr::ReadData(BYTE *pBuff)
    {
    DWORD	dwRead;
    DWORD	dwErrorFlags;
    COMSTAT	comstat;
    // Although this function detects errors in the COM port, it can be used to identify the size of the received data in the current reception buffer.
    ClearCommError( m_hComm, &dwErrorFlags, &comstat);
    dwRead = comstat.cbInQue;
    // If the size of the received data is greater than 0, the data is received.
    if (dwRead > 0){
    ReadFile( m_hComm, pBuff, MAXBUFFSIZE, &dwRead, &m_osRead);
    }
    return dwRead;
    }
    
  • 6) If the port is properly connected and configured through Steps 3 to 5, enter the "AT+OSPSERIALOPEN" command in the SendMSG window to start communicating serially with the bada device.
     //"Send" Button  ClickEventListener
    void CSerialTobadaDlg::OnBnClickedButton2()
    {
    if(m_SerialPort.m_bPortOpen)
    {
    // Fetch the text entered in the message input window and convert it into BYTE.
    CString strMsg; 
    m_EditMSG.GetWindowText(strMsg);
    strMsg +=_T( "\r\n" );
    .
    .
    .
    	// Write the converted data gWriteData.
    m_SerialPort.WriteData(gWriteData);
    	m_EditSend.ReplaceSel(strMsg);
    
    }
    }
    
    
    DWORD SerialMgr::WriteData(BYTE *pBuff)
    {
    DWORD	dwWritten;
    // Send the data over the serial port.
    WriteFile(m_hComm, pBuff, strlen((char *)pBuff), &dwWritten, &m_osWrite);
    
    return dwWritten;
    }
    
  • 7) Send the "Osp:Req=’AccessaryApp" command so that the application for which the Serial Launch Condition is "AccessaryApp"is executed.
    (Please note that the bada application’s Launch Conditions have been sent to "Serial=’AccessaryApp" in Step 1.)
3. Running the application and Echo communication on the bada device

If you complete Steps 1 and 2 above, the following popup window appears. If you click the Launch button, the AccessaryApp is launched. If you launch the app on the bada device and send the message in the PC program, the device receives and displays the data on the Label and then the device sends the received data to the PC. The PC program receives the data from the device and outputs it in the reception log window.


The screen of the PC program after sending the message

File Attachments : SerialCom.zip

Did this document help you? Yes | It's good,but ... | Not Helpful