-
September 8th, 2012, 07:58 AM
#1
Why such small program don't send nothing through com-port?
But I have program, which send data with similar connection paremeters normally. What is problem?
Code:
// Temp.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Temp.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HANDLE hComPort;
BYTE Eof;
BYTE STX;
DWORD InitPort()
{
hComPort = INVALID_HANDLE_VALUE;
// открыть порт . . .
hComPort = CreateFile(L"COM3", // порт
GENERIC_READ | GENERIC_WRITE, //чтение и запись
0, // must be opened with exclusive-access
NULL, // default security // no security attributes
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, //FILE_ATTRIBUTE_NORMAL, FILE_FLAG_OVERLAPPED, //? normal file // not overlapped I/O == 0,
NULL); // no attr. template // hTemplate must be NULL for comm devices
if (hComPort == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
CloseHandle(hComPort);
return err;
}
DCB dcb;
COMMTIMEOUTS PortTimeOuts;
// настройки порта . . .
if (!GetCommState(hComPort, &dcb))
{
DWORD err = GetLastError();
CloseHandle(hComPort);
return err;
}
dcb.BaudRate = CBR_9600; // set the baud rate CBR_9600
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = TWOSTOPBITS; // one stop bit
if (!SetCommState(hComPort, &dcb))
{
DWORD err = GetLastError();
CloseHandle(hComPort);
return err;
}
SetupComm(hComPort, 1024*1024, 1024*1024);
// таймауты ввода-вывода . . .
if (!GetCommTimeouts(hComPort, &PortTimeOuts))
{
DWORD err = GetLastError();
CloseHandle(hComPort);
return err;
}
PortTimeOuts.ReadIntervalTimeout = 5;
PortTimeOuts.ReadTotalTimeoutMultiplier = 5;
PortTimeOuts.ReadTotalTimeoutConstant = 2000;
PortTimeOuts.WriteTotalTimeoutMultiplier = 0;
PortTimeOuts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(hComPort, &PortTimeOuts))
{
DWORD err = GetLastError();
CloseHandle(hComPort);
return err;
}
PurgeComm( hComPort, PURGE_RXCLEAR ); //Очистить буфер драйвера на приём
return 0;
}
void TempWrite()
{
if (InitPort())
{
//MessageBox(hwnd,L"Ошибка открытия порта", L"Ошибка", 0);
//Writing = FALSE;
return;
}
Sleep(400);
DWORD Written;
char b='a';
BYTE *arr = new BYTE[256];
arr[0] = 3;
arr[1] = '\0';
arr[2] = STX;
arr[3] = '\0';
arr[4] = 4;
arr[5] = Eof;
WriteFile(hComPort, arr, 256, &Written, NULL);
CloseHandle(hComPort);
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEMP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEMP));
TempWrite();
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEMP));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TEMP);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
-
September 8th, 2012, 09:45 AM
#2
Re: Why such small program don't send nothing through com-port?
Did you debug your "small" program?
Is the TempWrite() called? If "YES" then what does WriteFile return?
Victor Nijegorodov
-
September 8th, 2012, 10:05 AM
#3
Re: Why such small program don't send nothing through com-port?
VictorN, called and success
-
September 8th, 2012, 10:14 AM
#4
Re: Why such small program don't send nothing through com-port?
Then what is the value of Written
Victor Nijegorodov
-
September 8th, 2012, 10:15 AM
#5
Re: Why such small program don't send nothing through com-port?
-
September 8th, 2012, 10:20 AM
#6
Re: Why such small program don't send nothing through com-port?
Originally Posted by AKE
VictorN, 256
But it means that this program does send data to your com3 port!
So why do you complain?
Victor Nijegorodov
-
September 8th, 2012, 10:25 AM
#7
Re: Why such small program don't send nothing through com-port?
VictorN, program on other computer don't catch this data, inspite of similar program send normally. I think problem in my program.
-
September 8th, 2012, 10:32 AM
#8
Re: Why such small program don't send nothing through com-port?
Big sorry, I sent it on other port...
-
September 8th, 2012, 10:32 AM
#9
Re: Why such small program don't send nothing through com-port?
But first thing in such a case would be to check a cable.
The second one - test your sender program with a null-modem cable using hyperterminal, then - check Windows firewall on both computers...
Victor Nijegorodov
-
September 8th, 2012, 11:57 AM
#10
Re: Why such small program don't send nothing through com-port?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|