473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error: win32 thread to read COM port and send data for processing

Wrote win32 thread to read serial data (character at a time) from COM2 port, once it come across "\r\n', send that data to another thread to process it, it is not working, readfile gives an error, any idea?

I am using COM2 port, running 4800 speed, 8 bits, No parity, I opened COM2 directly, do I need to input speed, parity and stopbits?

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h> 
  2. #include <stdio.h> 
  3. #include <tchar.h>
  4. #include <strsafe.h>
  5.  
  6. #define BUFSIZE 512
  7.  
  8. DWORD WINAPI InstanceThread(); 
  9. VOID GetAnswerToRequest(LPTSTR); 
  10. HANDLE handle;
  11.  
  12.  
  13. int main(VOID) 
  14.  
  15.    BOOL   fConnected = FALSE; 
  16.    DWORD  dwThreadId = 0; 
  17.    HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL; 
  18.  
  19.     handle= CreateFile( "COM2", GENERIC_READ | GENERIC_WRITE,1,NULL,
  20.     OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );
  21.  
  22.  
  23.    for (;;) 
  24.    { 
  25.  
  26.          printf("creating a processing thread.\n"); 
  27.  
  28.          // Create a thread. 
  29.          hThread = CreateThread( 
  30.             NULL,              // no security attribute 
  31.             0,                 // default stack size 
  32.             InstanceThread,    // thread proc
  33.             NULL,    // thread parameter 
  34.             0,                 // not suspended 
  35.             &dwThreadId);      // returns thread ID 
  36.  
  37.          if (hThread == NULL) 
  38.          {
  39.             _tprintf(TEXT("CreateThread failed, GLE=%d.\n"), GetLastError()); 
  40.             return -1;
  41.          }
  42.          else CloseHandle(hThread); 
  43.    } 
  44.  
  45.    return 0; 
  46.  
  47.  
  48. DWORD WINAPI InstanceThread()
  49. {
  50.     char    buf[1024] = {0};
  51.     char    ch = 0;
  52.     DWORD    len = 0;
  53.     BOOL    err = FALSE;
  54.     int        slen = 0;
  55.     char    *ptr = NULL;
  56.     static char send[1024] = {0};    
  57.  
  58.     BOOL    fNoValid = FALSE;
  59.  
  60.  
  61.  
  62.     while(1)
  63.     {
  64.         len = 0;
  65.  
  66.         err = ReadFile(handle, &ch, 1, &len, NULL);
  67.  
  68.         if((err == 0)||(len == 0))
  69.         { 
  70.             Sleep(10);
  71.             continue;
  72.         }
  73.  
  74.         buf[slen] = ch;
  75.  
  76.         switch(ch)
  77.         {
  78.             case '\r' :    
  79.                 break;
  80.  
  81.             case '\n' :    
  82.  
  83.                 if(fNoValid == FALSE)
  84.                 { 
  85.                     buf[slen] = '\0';    
  86.                     memcpy(send, buf, slen+1);    
  87.                     GetAnswerToRequest(send); 
  88.  
  89.                 }
  90.                 else
  91.                 { 
  92.                     buf[0] = '\n';    
  93.                     buf[1] = '\0';        
  94.                     memcpy(send, buf, 2);
  95.                     GetAnswerToRequest(send); 
  96.                 }
  97.  
  98.                 slen = 0;
  99.                 fNoValid = FALSE;
  100.  
  101.                 break;
  102.  
  103.             default :
  104.                 if((ch >= 0x20) && (ch < 0x7E))
  105.                 {
  106.                     slen++;
  107.  
  108.                     if(slen > (ARRAYSIZE(buf)-1))
  109.                     { 
  110.                         slen = 0;
  111.                     }
  112.                 }
  113.  
  114.                 else
  115.                 { 
  116.                     fNoValid = TRUE;
  117.                 }
  118.  
  119.                 break;
  120.         }
  121.     }
  122.  
  123.  
  124.     return(0);
  125. }
  126.  
  127.  
  128. VOID GetAnswerToRequest( LPTSTR pchRequest)
  129. {
  130.     //Process the string, print the string temporarily
  131.     printf( TEXT("String received on COM2 port:\"%s\"\n"), pchRequest );
  132.  
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
Nov 2 '11 #1
1 3100
johny10151981
1,059 1GB
What part is not working??
the reading part or thread part?
Nov 3 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
5
by: Andy G | last post by:
Hi, My plan is to use Try Catch statements to catch an error and then email it to myself. I send the user to a custom error page and I would like to email myself the asp.net error. I thought...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
1
by: niclester | last post by:
Hello, I am trying to obtain a safe correlation between a managed thread and a WIn32 thread. I need to be able to gather run time statistics on particular running threads using WMI or...
1
by: nizargermany | last post by:
hello i will send data to a slave but i can't: my first code: private datasocket as cwlib.cwdatasocket datasocket= New CWDSLib.CWDataSocket datasocket.ConnectTo("URL",...
5
by: trig | last post by:
Please help! I am an ICT teacher at a secondary school and my year 12 (AS Level) group need to create a website where data can be sent from a form to a Microsoft Access database. I am trying...
7
by: Boki | last post by:
Multi-thread read/write to a single file. I have two processing threads, thread A and thread B; and I called my queue as Q. Thread A will feed data into Q by user input, the timing is random....
3
by: romcab | last post by:
Hi guys, Just want to ask how can I send data serially? I have here a sample code but I use sockets and tcpclient class but I'm wondering how to do it using serial cable.Any idea? Private...
5
sid0404
by: sid0404 | last post by:
Hi I am new to the visualstudio.net I am trying to develop an application which requires me to send data to a HTML webpage - http://patft.uspto.gov/netahtml/PTO/search-bool.html and the user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.