Seite 1 von 3

COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 10:05 am
von cloidnerux
Hallo, also ich hab folgendes Problem:
Ich hab mir einen A/D-Wandler gebaut, der sendet Signale an einen Com-Port(Serielle-Schnitstelle).
Jezt will ich diese Daten auslesen, nur wie mach ich das?
Es werden immer 8-Bit Daten gesendet.
Danke im vorraus.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 11:31 am
von Xin
Das hängt stark von Deinem OS ab.

Du musst den Treiber Deiner COM-Schnittstelle ansprechen, oder Dir für ein eigenes OS einen Treiber schreiben, der auf die Adresse der COM-Schnittstelle ausliest. Ich habe das mal unter Amiga-OS gemacht, aber ich vermute, dass das für Dich eher uninteressant ist.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 11:35 am
von cloidnerux
Ähm, wie oben gesagt, Linux/Windows.
egt. sollte zumindest für Linux Funktionen zum auslesen der Com-Schnittstelle.
Auch bei google kommt nur unbrauchbares Zeug

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 11:48 am
von Xin
cloidnerux hat geschrieben:Ähm, wie oben gesagt, Linux/Windows.
Entweder... oder. Beides wird schon schwieriger. Da schaust Du Dich vielleicht mal bei portablen Libs wie QT oder Boost um.
cloidnerux hat geschrieben:egt. sollte zumindest für Linux Funktionen zum auslesen der Com-Schnittstelle.
Auch bei google kommt nur unbrauchbares Zeug
Och, so unbrauchbar ist das eigentlich nicht.
Schau beispielsweise mal hier.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 3:32 pm
von cloidnerux
JA, mal sehn was sich daraus machen lässt.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 4:45 pm
von Jside
Mit outb und inb zu Aeris hatte ich ja schonmal sowas programmiert, das lässt sich auch (zumindest unter Windows/DOS) einwandfrei anwenden, unter Linux musst du erst die Schittstelle belegen.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 5:30 pm
von cloidnerux
omg, Ich programmiere mir Aeris so, das man meinen A/D wandler Bterieben kann und daraus einen graf macht.
Also versuch ichs mal mit inb/outb unter Windows.

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 6:20 pm
von cloidnerux
In welcher Headerdatei unter Win ist outb und inb definiert?

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 6:46 pm
von Kerli
cloidnerux hat geschrieben:In welcher Headerdatei unter Win ist outb und inb definiert?
Naja. Eine allzu große Auswahl hast du ja nicht ;) Versuchs doch einmal mit 'windows.h'. Falls es dich interessiert ist hier noch der Quellcode von einem alten Programm von mir. Damit wird der Status der verschiedenen Eingänge der seriellen Schnittstelle angezeigt, und man kann die Ausgänge ein- und ausschalten:

Code: Alles auswählen

#include <windows.h>

#include <cstdlib>

#include <iostream>



using namespace std;



HANDLE g_hCom       = NULL;



HWND g_hWDialog     = 0;



HWND g_hCTS         = 0,

     g_hDSR         = 0,

     g_hRI          = 0,

     g_hDCD         = 0;



bool g_bCTS         = true,

     g_bDSR         = false,

     g_bRI          = false,

     g_bDCD         = false;



CRITICAL_SECTION    g_csInput;

HANDLE              g_hThread;



const HPEN PenRed	    = CreatePen(PS_SOLID, 1, RGB(230,50,20));

const HBRUSH BrushRed   = CreateSolidBrush(RGB(230,150,120));

                    

const HPEN PenGreen	    = CreatePen(PS_SOLID, 1, RGB(0,160,0));

const HBRUSH BrushGreen = CreateSolidBrush(RGB(0,255,0));





const int g_iGroupInTop     = 10,    g_iGroupInLeft    = 10;

const int g_iGroupOutTop    = 100,   g_iGroupOutLeft   = 10;

	



HRESULT SetDTR(HANDLE hCom, bool bState);

HRESULT SetRTS(HANDLE hCom, bool bState);

HRESULT SetTXD(HANDLE hCom, bool bState);



bool GetCTS(HANDLE hCom);

bool GetDSR(HANDLE hCom);

bool GetRI(HANDLE hCom);

bool GetDCD(HANDLE hCom);



LRESULT CALLBACK MsgProcWDialog(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

DWORD WINAPI ThreadWatchInput(void* pInitData);



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShowCmd)

{

    WNDCLASSEX wcWDialog;	//Erstellen der Fensterstruktur

		

		//Füllen der Fensterstruktur

		wcWDialog.cbSize		= sizeof(WNDCLASSEX);	//Größe der Struktur

		wcWDialog.style			= CS_HREDRAW|CS_VREDRAW;	

		wcWDialog.lpfnWndProc	= MsgProcWDialog;	//Nachrichtenbearbeitungsfunktion des Diaologfensters

		wcWDialog.cbClsExtra	= 0;

		wcWDialog.cbWndExtra	= 0;

		wcWDialog.hInstance		= hInstance;

		wcWDialog.hIcon			= LoadIcon(hInstance, IDI_WINLOGO);

		wcWDialog.hCursor		= LoadCursor(NULL, IDC_ARROW);

		wcWDialog.hbrBackground	= (HBRUSH)CreateSolidBrush(GetSysColor(COLOR_3DFACE));

		wcWDialog.lpszMenuName	= NULL;

		wcWDialog.lpszClassName	= "DialogWindowClass";

		wcWDialog.hIconSm		= LoadIcon(hInstance, IDI_WINLOGO);

	

	RegisterClassEx(&wcWDialog);		//Registrieren der Fensterstruktur



	//Dialogfenster erzeugen(liefert Handle auf Fenster zurück)

	g_hWDialog = CreateWindowEx(NULL, "DialogWindowClass", TEXT("COM - Testprogramm"), WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE, 50,50, 210,220, NULL, NULL, hInstance, NULL);

	

	UnregisterClass("DialogWindowClass", hInstance);

	//-----------------------------------------------------------------------------------------



	//ChildFenster erzeugen

	              CreateWindowEx(NULL, "button", "Intput",	WS_CHILD|WS_VISIBLE|BS_GROUPBOX,    g_iGroupInLeft,         g_iGroupInTop,          180, 80,    g_hWDialog, NULL, hInstance, NULL);

	              CreateWindowEx(NULL, "button", "Output",  WS_CHILD|WS_VISIBLE|BS_GROUPBOX,    g_iGroupOutLeft,        g_iGroupOutTop,         180, 80,    g_hWDialog, NULL, hInstance, NULL);



    g_hCTS	    = CreateWindowEx(NULL, "static", "",	    WS_CHILD|WS_VISIBLE|SS_OWNERDRAW,   g_iGroupInLeft + 20,    g_iGroupInTop + 25,     16, 16,     g_hWDialog, NULL, hInstance, NULL);

	g_hDSR	    = CreateWindowEx(NULL, "static", "",	    WS_CHILD|WS_VISIBLE|SS_OWNERDRAW,   g_iGroupInLeft + 60,    g_iGroupInTop + 25,     16, 16,     g_hWDialog, NULL, hInstance, NULL);

	g_hRI	    = CreateWindowEx(NULL, "static", "",	    WS_CHILD|WS_VISIBLE|SS_OWNERDRAW,   g_iGroupInLeft + 100,   g_iGroupInTop + 25,     16, 16,     g_hWDialog, NULL, hInstance, NULL);

	g_hDCD	    = CreateWindowEx(NULL, "static", "",	    WS_CHILD|WS_VISIBLE|SS_OWNERDRAW,   g_iGroupInLeft + 140,   g_iGroupInTop + 25,     16, 16,     g_hWDialog, NULL, hInstance, NULL);

	

                  CreateWindowEx(NULL, "static", "CTS",	    WS_CHILD|WS_VISIBLE|SS_CENTER,      g_iGroupInLeft + 12,    g_iGroupInTop + 50,     32, 16,     g_hWDialog, NULL, hInstance, NULL);

	              CreateWindowEx(NULL, "static", "DSR",	    WS_CHILD|WS_VISIBLE|SS_CENTER,      g_iGroupInLeft + 52,    g_iGroupInTop + 50,     32, 16,     g_hWDialog, NULL, hInstance, NULL);

	              CreateWindowEx(NULL, "static", "RI",	    WS_CHILD|WS_VISIBLE|SS_CENTER,      g_iGroupInLeft + 92,    g_iGroupInTop + 50,     32, 16,     g_hWDialog, NULL, hInstance, NULL);

	              CreateWindowEx(NULL, "static", "DCD",	    WS_CHILD|WS_VISIBLE|SS_CENTER,      g_iGroupInLeft + 132,   g_iGroupInTop + 50,     32, 16,     g_hWDialog, NULL, hInstance, NULL);

	

    //-----------------------------------------------------------------------------------------

    

    MSG msg;

    

    while( GetMessage(&msg, NULL, 0, 0) ) 

	{

		TranslateMessage( &msg );

		DispatchMessage( &msg );

	}

    

    TerminateThread(g_hThread, 0);

    //if(g_hCom == INVALID_HANDLE_VALUE) return 1;

    //

    //SetRTS(g_hCom, true);

    //

    //while(true)

    //{

    //    if(GetRI(g_hCom))

    //    {

    //        SetDTR(g_hCom, true);

    //        SetTXD(g_hCom, false);

    //    }

    //    else

    //    {

    //        SetDTR(g_hCom, false);

    //        SetTXD(g_hCom, true);

    //    }

    //    /*SetDTR(hCom, true);

    //    SetTXD(hCom, false);

    //    Sleep(100);

    //    SetDTR(hCom, false);

    //    SetTXD(hCom, true);

    //    Sleep(100);*/

    //}

    CloseHandle(g_hCom);

    return 0;

}



HRESULT SetDTR(HANDLE hCom, bool bState)

{

    if(bState)

    {

        EscapeCommFunction(hCom, SETDTR);

    }

    else

    {

        EscapeCommFunction(hCom, CLRDTR);

    }

    return S_OK;

}



HRESULT SetRTS(HANDLE hCom, bool bState)

{

    if(bState)

    {

        EscapeCommFunction(hCom, SETRTS);

    }

    else

    {

        EscapeCommFunction(hCom, CLRRTS);

    }

    return S_OK;

}



HRESULT SetTXD(HANDLE hCom, bool bState)

{

    if(bState)

    {

        EscapeCommFunction(hCom, SETBREAK);

    }

    else

    {

        EscapeCommFunction(hCom, CLRBREAK);

    }

    return S_OK;

}



bool GetCTS(HANDLE hCom)

{

    DWORD dwState;

    GetCommModemStatus(hCom, &dwState);

    return (dwState & MS_CTS_ON) > 0;

}



bool GetDSR(HANDLE hCom)

{

    DWORD dwState;

    GetCommModemStatus(hCom, &dwState);

    return (dwState & MS_DSR_ON) > 0;

}



bool GetRI(HANDLE hCom)

{

    DWORD dwState;

    GetCommModemStatus(hCom, &dwState);

    return (dwState & MS_RING_ON) > 0;

}



bool GetDCD(HANDLE hCom)

{

    DWORD dwState;

    GetCommModemStatus(hCom, &dwState);

    return (dwState & MS_RLSD_ON) > 0;

}





LRESULT CALLBACK MsgProcWDialog(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

	switch (msg)

	{

        case WM_CREATE:

            g_hCom = CreateFile(TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

            SetCommMask(g_hCom, EV_CTS|EV_DSR|EV_RING|EV_RLSD);

            SetDTR(g_hCom, true);



            InitializeCriticalSection(&g_csInput);



            DWORD dwThread;

            g_hThread = CreateThread(0, 0, ThreadWatchInput, 0, 0, &dwThread);



            return 0;

        case WM_DRAWITEM:

            {

                LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT) lParam;

                bool bEnabled = false;

                

                EnterCriticalSection(&g_csInput);



                    if(lpDrawItem->hwndItem == g_hCTS)      bEnabled = g_bCTS;

                    else if(lpDrawItem->hwndItem == g_hDSR) bEnabled = g_bDSR;

                    else if(lpDrawItem->hwndItem == g_hRI)  bEnabled = g_bRI;

                    else if(lpDrawItem->hwndItem == g_hDCD) bEnabled = g_bDCD;



                LeaveCriticalSection(&g_csInput);



                if(bEnabled)

                {

                    SelectObject(lpDrawItem->hDC, PenGreen);

                    SelectObject(lpDrawItem->hDC, BrushGreen);

                }

                else

                {

                    SelectObject(lpDrawItem->hDC, PenRed);

                    SelectObject(lpDrawItem->hDC, BrushRed);

                }



                Ellipse(lpDrawItem->hDC, 0, 0, 16, 16);

                

                return 0;

            }

        case WM_PAINT:			

            {

				//PAINTSTRUCT ps;

				//HDC hdc;

				//

				//hdc = BeginPaint (g_hGrpIn, &ps);

				//

				//	//-------------------

				//	//Pinsel definieren:

				//	//-------------------

				//		HPEN PenRed	= CreatePen(PS_SOLID, 1, RGB(220,30,10));



				//		SelectObject(hdc, PenRed);



    //                //x-Achse zeichnen:

				//	MoveToEx(hdc, 10, 10, 0);

				//	LineTo(hdc, 20, 20);

				//	

				//	TextOut(hdc, 30, 30, "y", 1);

				//	

			 //   EndPaint( hWnd, &ps );



				return DefWindowProc(hWnd, msg, wParam, lParam);

			}

        case WM_DESTROY:			

			PostQuitMessage(0);

			return 0;

		default:

			return DefWindowProc(hWnd, msg, wParam, lParam);

	}	

}



DWORD WINAPI ThreadWatchInput(void* pInitData)

{

    DWORD dwComState;

    MessageBox(0, "Init", "Aha", 0);

    

    do

    {

        EnterCriticalSection(&g_csInput);

        

            g_bCTS = GetCTS(g_hCom);

                

            g_bDSR = GetDSR(g_hCom);

                

            g_bRI  = GetRI(g_hCom);

                

            g_bDCD = GetDCD(g_hCom);



        LeaveCriticalSection(&g_csInput);



        InvalidateRect(g_hCTS, 0, false);

        InvalidateRect(g_hDSR, 0, false);

        InvalidateRect(g_hRI, 0, false);

        InvalidateRect(g_hDCD, 0, false);

    }while(WaitCommEvent(g_hCom, &dwComState, 0));

    return 0;

}
Achja, wie bereits erwähnt ist das Programm schon etwas älter. Der Programmierstil ist deshalb nicht unbedingt optimal :D

Re: COM-Port(Seriell) Auslesen Windows/Linux

Verfasst: Mo Feb 02, 2009 6:47 pm
von cloidnerux
windows.h funktioniert nicht.