i have tried below code in win32 api. before create the thread i used to wait of data while loop. that time receving is work fine but when i create the thread as in below i am getting error in setCommask and waitCommask... what is the error on that...
error shows according to the my app output
Error! in Setting CommMask
Error! in Setting WaitCommEvent()
What I have tried:
#include
#include "Serial.h"
#include "resource.h"
#include
#define AMOUNT_TO_READ 512
HANDLE hComm; char ComPortName[] = "\\\\.\\COM5"; BOOL Status; char TempChar; char lpBuf[AMOUNT_TO_READ]; DWORD NoBytesRead; int i = 0;
BOOL flag;
HANDLE readerThread;
DWORD readThreadID;
void show(char*);
#include "Main.h"
HWND windowG;
bool wirtePort()
{
char lpBuffer[] = "A"; DWORD dNoOFBytestoWrite; DWORD dNoOfBytesWritten = 0;
dNoOFBytestoWrite = sizeof(lpBuffer);
Status = WriteFile(hComm, lpBuffer, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);
return 0;
}
void ErrorExit(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
bool readPort(LPVOID lpV)
{
HANDLE tCom = (HANDLE)lpV;
DWORD dwEventMask;
Status = SetCommMask(tCom, EV_RXCHAR);
if (Status == FALSE)
show("\n\n Error! in Setting CommMask");
else
show("\n\n Setting CommMask successfull");
while (true)
{
i = 0;
Status = WaitCommEvent(tCom, &dwEventMask, NULL);
if (Status == FALSE)
{
show("\n Error! in Setting WaitCommEvent()");
ErrorExit(TEXT("WaitCommEvent"));
break;
}
else
{
Status = ReadFile(tCom, lpBuf, sizeof(lpBuf), &NoBytesRead, NULL);
show("\n");
show(lpBuf);
show(" ");
wirtePort();
}
}
return 0;
}
void setupPort()
{
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
Status = GetCommState(hComm, &dcbSerialParams);
if (Status == FALSE)
show("\n Error! in GetCommState()");
dcbSerialParams.BaudRate = CBR_9600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY;
Status = SetCommState(hComm, &dcbSerialParams);
if (Status == FALSE)
{
show("\n Error! in Setting DCB Structure");
}
else
{
show("\n\n Setting DCB Structure Successfull\n");
}
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (SetCommTimeouts(hComm, &timeouts) == FALSE)
show("\n\n Error! in Setting Time Outs");
else
show("\n\n Setting Serial Port Timeouts Successfull");
}
void openPort()
{
hComm = CreateFile(ComPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE)
show("\n Error! - Port %s can't be opened\n");
else
show("\n Port %s Opened\n ");
}
void closePort()
{
CloseHandle(hComm);
}
void show(char* userMessage)
{
HWND OutputWindow = GetDlgItem(windowG, IDC_EDIT1);
if (OutputWindow)
{
SendMessageA(OutputWindow, EM_REPLACESEL, 0, (long)userMessage);
}
}
int _stdcall handler(HWND window, UINT msg, WPARAM window_pram, LPARAM long_pram)
{
windowG = window;
switch (msg)
{
case WM_COMMAND:
{
switch (window_pram)
{
case IDC_BUTTON_CONNECT:
{
show("init the connect buttion");
openPort();
setupPort();
readerThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)readPort,(LPVOID)hComm,0,&readThreadID);
closePort();
}break;
default:
break;
}
}break;
case WM_CLOSE:
{
DestroyWindow(window);
}break;
default:
return DefWindowProcA(window, msg, window_pram, long_pram);
break;
}
return 0;
}
WinMain(HINSTANCE cur_base, HINSTANCE prv_base, char* args, int display)
{
DialogBoxA(cur_base, MAKEINTRESOURCEA(IDD_DIALOG1), 0, handler);
return 1;
}
// debug information
'PMDG_MCP.exe' (Win32): Loaded 'C:\Users\SIC-002\Documents\Visual Studio 2013\Projects\PMDG_MCP\Debug\PMDG_MCP.exe'. Symbols loaded.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\win32u.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32full.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp_win.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbase.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.18362.752_none_2e6decc4278ee012\comctl32.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\TextInputFramework.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreUIComponents.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreMessaging.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\SHCore.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntmarta.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\WinTypes.dll'
'PMDG_MCP.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\WinTypes.dll'
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\iertutil.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Program Files (x86)\Internet Download Manager\idmmkb.dll'. Cannot find or open the PDB file.
'PMDG_MCP.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.