Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Serial communication + thread related problem

Serial communication + thread related problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harsha_1234
    wrote on last edited by
    #1

    hi all, i am having one problem with threading i created one dialog based application whose work is to read the data from serial port and display it.so i created one thred for this work. means now i got two threads one is application thread and other is serial communication thread which is created in OnInitDialog. now everything is working i mean the thread got created it read some bytes from port and now it wants to show the data so need to send this data to main thread. so i am doing this with ::SendMessage() but it is giving access violation. i don't know why this is happening and currently i am not sending the data also i.e. buffer so please help me below is code (stepwise) 1)In Oninitdialog THREADPARAM *ptp = new THREADPARAM; if(ptp) { memset(ptp->array,0,30); strcpy(ptp->array,"Harshal"); ptp->appHandle = this->m_hWnd; hSerialThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) SerialCommunication, (LPVOID) NULL, 0, &dwSerialThreadID); } 2)Thread function UINT SerialCommunication(LPVOID pParam) { int write_req=0; THREADPARAM *ptp = (THREADPARAM *)pParam; while(1) { if(write_req == 2)// send write request and read it { DWORD length=0; DWORD dwRead=0; if (WriteFile(hCom, SEND_CMD, // pointer to data to write to file NOOFBYTE, // number of bytes to write &length, // pointer to number of bytes written NULL) == 0) {// write failed write_req = 0; continue; } // writefile succeeded issue readfile char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req = 0; continue; } if(dwRead > 0) { //MessageBox(NULL,buffer,"Data with packet",MB_OK); ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);// error is here } write_req = 0; } else // only read from port and increment counter { DWORD dwRead=0; char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req++; continue; } if(dwRead > 0) {// you are in thread you need to send message to // main application //char a; HWND hwnd = (HWND)AfxGetApp()->m_pMainWindow; ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0); //MessageBox(NULL,buffer,"Data withou

    T B 2 Replies Last reply
    0
    • H harsha_1234

      hi all, i am having one problem with threading i created one dialog based application whose work is to read the data from serial port and display it.so i created one thred for this work. means now i got two threads one is application thread and other is serial communication thread which is created in OnInitDialog. now everything is working i mean the thread got created it read some bytes from port and now it wants to show the data so need to send this data to main thread. so i am doing this with ::SendMessage() but it is giving access violation. i don't know why this is happening and currently i am not sending the data also i.e. buffer so please help me below is code (stepwise) 1)In Oninitdialog THREADPARAM *ptp = new THREADPARAM; if(ptp) { memset(ptp->array,0,30); strcpy(ptp->array,"Harshal"); ptp->appHandle = this->m_hWnd; hSerialThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) SerialCommunication, (LPVOID) NULL, 0, &dwSerialThreadID); } 2)Thread function UINT SerialCommunication(LPVOID pParam) { int write_req=0; THREADPARAM *ptp = (THREADPARAM *)pParam; while(1) { if(write_req == 2)// send write request and read it { DWORD length=0; DWORD dwRead=0; if (WriteFile(hCom, SEND_CMD, // pointer to data to write to file NOOFBYTE, // number of bytes to write &length, // pointer to number of bytes written NULL) == 0) {// write failed write_req = 0; continue; } // writefile succeeded issue readfile char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req = 0; continue; } if(dwRead > 0) { //MessageBox(NULL,buffer,"Data with packet",MB_OK); ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);// error is here } write_req = 0; } else // only read from port and increment counter { DWORD dwRead=0; char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req++; continue; } if(dwRead > 0) {// you are in thread you need to send message to // main application //char a; HWND hwnd = (HWND)AfxGetApp()->m_pMainWindow; ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0); //MessageBox(NULL,buffer,"Data withou

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      harsha_1234 wrote:

      ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);

      Use PostMessage() instead of SendMessage.. and look for difference between PostMessage and SendMessage in MSDN!

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

      1 Reply Last reply
      0
      • H harsha_1234

        hi all, i am having one problem with threading i created one dialog based application whose work is to read the data from serial port and display it.so i created one thred for this work. means now i got two threads one is application thread and other is serial communication thread which is created in OnInitDialog. now everything is working i mean the thread got created it read some bytes from port and now it wants to show the data so need to send this data to main thread. so i am doing this with ::SendMessage() but it is giving access violation. i don't know why this is happening and currently i am not sending the data also i.e. buffer so please help me below is code (stepwise) 1)In Oninitdialog THREADPARAM *ptp = new THREADPARAM; if(ptp) { memset(ptp->array,0,30); strcpy(ptp->array,"Harshal"); ptp->appHandle = this->m_hWnd; hSerialThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) SerialCommunication, (LPVOID) NULL, 0, &dwSerialThreadID); } 2)Thread function UINT SerialCommunication(LPVOID pParam) { int write_req=0; THREADPARAM *ptp = (THREADPARAM *)pParam; while(1) { if(write_req == 2)// send write request and read it { DWORD length=0; DWORD dwRead=0; if (WriteFile(hCom, SEND_CMD, // pointer to data to write to file NOOFBYTE, // number of bytes to write &length, // pointer to number of bytes written NULL) == 0) {// write failed write_req = 0; continue; } // writefile succeeded issue readfile char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req = 0; continue; } if(dwRead > 0) { //MessageBox(NULL,buffer,"Data with packet",MB_OK); ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);// error is here } write_req = 0; } else // only read from port and increment counter { DWORD dwRead=0; char buffer[MAX_MESSAGE]; memset(buffer,0,MAX_MESSAGE); if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead, NULL)) { write_req++; continue; } if(dwRead > 0) {// you are in thread you need to send message to // main application //char a; HWND hwnd = (HWND)AfxGetApp()->m_pMainWindow; ::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0); //MessageBox(NULL,buffer,"Data withou

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        From the 'serial port reader' thread I would place the data into a thread-safe queue of some sort and notify the 'display' thread that new data is available. The 'display' thread can pull it out of the queue and format as appropriate.

        Any sufficiently gross incompetence is nearly indistinguishable from malice.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups