Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[WINAPI][C++] Program komunikujący się po RS232

Ostatnio zmodyfikowano 2014-08-31 12:09
Autor Wiadomość
1aam2am1
» 2014-08-31 10:58:22
1) Readfile zatrzymuje program ale w funkcji read_color_map_COM masz tam takiecos jak
C/C++
COMSTAT statsread; //status
ClearCommError( hCom, NULL, & statsread ); //sprawdza status
if( statsread.cbInQue != 0 ) {
    ReadFile( hCom, & read_char_COM, 1, & RS_read, 0 );
    [ \c pp ]
    Kt ó ry temu zapobiega
P-116444
krzysztof.kolski
Temat założony przez niniejszego użytkownika
» 2014-08-31 12:09:33
Dzięki, pomogło.

Ostatecznie zrobiłem tak że odbieram tą daną w timerze, jak ktoś potrafi lepiej to napisać to nie mam nic przeciwko, w każdym razie doszedłem do tego kodu i już mi działa

C/C++
#include "stdafx.h"
#include <windows.h>

const WORD ID_TIMER = 3001;

#define IDC_MAIN_BUTTON 101 // Button identifier
#define IDC_MAIN_EDIT 102 // Edit box identifier
#define IDC_MAIN_EDIT2 103 // Edit box identifier
#define IDC_MAIN_EDIT3 104 // Edit box identifier
#define IDC_MAIN_EDIT4 105 // Edit box identifier
HWND hEdit, hEdit2, hEdit3, hEdit4;
DCB dcb;
HANDLE hNumPort;
char lpBuffor_read[ 33 ], lpBuffor_write[ 33 ];
DWORD RS_ile;
char tabw[ 4 ], tabr;
LPSTR buf;
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
int dlugos;
COMSTAT statsread; //status

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd )
{
    WNDCLASSEX wClass;
    ZeroMemory( & wClass, sizeof( WNDCLASSEX ) );
    wClass.cbClsExtra = NULL;
    wClass.cbSize = sizeof( WNDCLASSEX );
    wClass.cbWndExtra = NULL;
    wClass.hbrBackground =( HBRUSH ) COLOR_WINDOW;
    wClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wClass.hIcon = NULL;
    wClass.hIconSm = NULL;
    wClass.hInstance = hInst;
    wClass.lpfnWndProc =( WNDPROC ) WinProc;
    wClass.lpszClassName = "Window Class";
    wClass.lpszMenuName = NULL;
    wClass.style = CS_HREDRAW | CS_VREDRAW;
   
    if( !RegisterClassEx( & wClass ) )
    {
        int nResult = GetLastError();
        MessageBox( NULL,
        "Window class creation failed\r\n",
        "Window Class Failed",
        MB_ICONERROR );
    }
   
    HWND hWnd = CreateWindowEx( NULL,
    "Window Class",
    "NOWE RS232 TEST (bajt 1,bajt 2,bajt 3,bajt 4)l2",
    WS_OVERLAPPEDWINDOW,
    200,
    200,
    640,
    480,
    NULL,
    NULL,
    hInst,
    NULL );
   
    if( !hWnd )
    {
        int nResult = GetLastError();
       
        MessageBox( NULL,
        "Window creation failed\r\n",
        "Window Creation Failed",
        MB_ICONERROR );
    }
   
    ShowWindow( hWnd, nShowCmd );
   
    MSG msg;
    ZeroMemory( & msg, sizeof( MSG ) );
   
    while( GetMessage( & msg, NULL, 0, 0 ) )
    {
       
        TranslateMessage( & msg );
        DispatchMessage( & msg );
    }
   
    return 0;
}

LRESULT CALLBACK WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
{ case WM_TIMER:
        ClearCommError( hNumPort, NULL, & statsread ); //sprawdza status
        if( statsread.cbInQue != 0 ) {
            if( ReadFile( hNumPort, & tabr, 1, & RS_ile, 0 ) ) { char buf[ 256 ]; wsprintf( buf, "Odebrano znak o kodzie %d", tabr ); MessageBox( NULL, buf, "Inf", MB_ICONINFORMATION ); }
        }
        break;
    case WM_CREATE:
        { if( SetTimer( hWnd, ID_TIMER, 1, NULL ) == 0 )
                 MessageBox( hWnd, "Nie można utworzyć timera!", "Kurde", MB_ICONSTOP );
            // Create an edit box
            hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 50, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT, GetModuleHandle( NULL ), NULL );
            hEdit2 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 150, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT2, GetModuleHandle( NULL ), NULL );
            hEdit3 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 250, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT3, GetModuleHandle( NULL ), NULL );
            hEdit4 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 350, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT4, GetModuleHandle( NULL ), NULL );
           
            HGDIOBJ hfDefault = GetStockObject( DEFAULT_GUI_FONT );
            SendMessage( hEdit, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
            SendMessage( hEdit2, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
            SendMessage( hEdit3, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
            SendMessage( hEdit4, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
           
            // Create a push button
            HWND hWndButton = CreateWindowEx( NULL,
            "BUTTON",
            "Wyślij",
            WS_TABSTOP | WS_VISIBLE |
            WS_CHILD | BS_DEFPUSHBUTTON,
            50,
            220,
            100,
            24,
            hWnd,
            ( HMENU ) IDC_MAIN_BUTTON,
            GetModuleHandle( NULL ),
            NULL );
            SendMessage( hWndButton,
            WM_SETFONT,
            ( WPARAM ) hfDefault,
            MAKELPARAM( FALSE, 0 ) );
           
           
            hNumPort = CreateFile( "COM1", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
            dcb.DCBlength = sizeof( dcb );
           
            dcb.BaudRate = CBR_9600;
            dcb.fParity = FALSE;
            dcb.Parity = NOPARITY;
            dcb.StopBits = ONESTOPBIT;
            dcb.ByteSize = 8;
           
            dcb.fDtrControl = DTR_CONTROL_ENABLE;
            dcb.fRtsControl = DTR_CONTROL_ENABLE;
           
            dcb.fOutxCtsFlow = FALSE;
            dcb.fOutxDsrFlow = FALSE;
            dcb.fDsrSensitivity = FALSE;
            dcb.fAbortOnError = FALSE;
            dcb.fOutX = FALSE;
            dcb.fInX = FALSE;
            dcb.fErrorChar = FALSE;
            dcb.fNull = FALSE;
           
            if( SetCommState( hNumPort, & dcb ) ) MessageBox( NULL, "Udało się połączyć z portem COM1", "Inf", MB_ICONINFORMATION );
            else MessageBox( NULL, "Nie udało się połączyć z portem COM1", "Błąd", MB_ICONERROR );
           
        }
        break;
       
    case WM_COMMAND:
        switch( LOWORD( wParam ) )
        {
        case IDC_MAIN_BUTTON:
            {
                dlugos = GetWindowTextLength( hEdit ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit, buf, dlugos + 1 ); tabw[ 0 ] = atoi( buf ) % 256;
                dlugos = GetWindowTextLength( hEdit2 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit2, buf, dlugos + 1 ); tabw[ 1 ] = atoi( buf ) % 256;
                dlugos = GetWindowTextLength( hEdit3 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit3, buf, dlugos + 1 ); tabw[ 2 ] = atoi( buf ) % 256;
                dlugos = GetWindowTextLength( hEdit4 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit4, buf, dlugos + 1 ); tabw[ 3 ] = atoi( buf ) % 256;
                WriteFile( hNumPort, tabw, 4, & RS_ile, 0 );
               
               
                MessageBox( NULL,
                "Danej zostały wysłane",
                "Information",
                MB_ICONINFORMATION );
            }
            break;
        }
        break;
       
    case WM_DESTROY:
        { CloseHandle( hNumPort );
            PostQuitMessage( 0 );
            return 0;
        }
        break;
    }
   
    return DefWindowProc( hWnd, msg, wParam, lParam );
}
[ \c pp ]
P-116449
1 2 « 3 »
Poprzednia strona Strona 3 z 3