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? -
#include <windows.h>
-
#include <stdio.h>
-
#include <tchar.h>
-
#include <strsafe.h>
-
-
#define BUFSIZE 512
-
-
DWORD WINAPI InstanceThread();
-
VOID GetAnswerToRequest(LPTSTR);
-
HANDLE handle;
-
-
-
int main(VOID)
-
-
{
-
BOOL fConnected = FALSE;
-
DWORD dwThreadId = 0;
-
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
-
-
handle= CreateFile( "COM2", GENERIC_READ | GENERIC_WRITE,1,NULL,
-
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );
-
-
-
for (;;)
-
{
-
-
printf("creating a processing thread.\n");
-
-
// Create a thread.
-
hThread = CreateThread(
-
NULL, // no security attribute
-
0, // default stack size
-
InstanceThread, // thread proc
-
NULL, // thread parameter
-
0, // not suspended
-
&dwThreadId); // returns thread ID
-
-
if (hThread == NULL)
-
{
-
_tprintf(TEXT("CreateThread failed, GLE=%d.\n"), GetLastError());
-
return -1;
-
}
-
else CloseHandle(hThread);
-
}
-
-
return 0;
-
}
-
-
-
DWORD WINAPI InstanceThread()
-
{
-
char buf[1024] = {0};
-
char ch = 0;
-
DWORD len = 0;
-
BOOL err = FALSE;
-
int slen = 0;
-
char *ptr = NULL;
-
static char send[1024] = {0};
-
-
BOOL fNoValid = FALSE;
-
-
-
-
while(1)
-
{
-
len = 0;
-
-
err = ReadFile(handle, &ch, 1, &len, NULL);
-
-
if((err == 0)||(len == 0))
-
{
-
Sleep(10);
-
continue;
-
}
-
-
buf[slen] = ch;
-
-
switch(ch)
-
{
-
case '\r' :
-
break;
-
-
case '\n' :
-
-
if(fNoValid == FALSE)
-
{
-
buf[slen] = '\0';
-
memcpy(send, buf, slen+1);
-
GetAnswerToRequest(send);
-
-
}
-
else
-
{
-
buf[0] = '\n';
-
buf[1] = '\0';
-
memcpy(send, buf, 2);
-
GetAnswerToRequest(send);
-
}
-
-
slen = 0;
-
fNoValid = FALSE;
-
-
break;
-
-
default :
-
if((ch >= 0x20) && (ch < 0x7E))
-
{
-
slen++;
-
-
if(slen > (ARRAYSIZE(buf)-1))
-
{
-
slen = 0;
-
}
-
}
-
-
else
-
{
-
fNoValid = TRUE;
-
}
-
-
break;
-
}
-
}
-
-
-
return(0);
-
}
-
-
-
VOID GetAnswerToRequest( LPTSTR pchRequest)
-
{
-
//Process the string, print the string temporarily
-
printf( TEXT("String received on COM2 port:\"%s\"\n"), pchRequest );
-
-
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
1 3100
What part is not working??
the reading part or thread part?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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",...
|
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...
|
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....
|
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...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |