COM Port ansprechen
-
tagchen!
Will mit nem C Programm den Com Port ansprechen und Befehle senden und lesen können! An dem Port soll später ne Eisenbahn angeschlossen werden!
Kenn mich zwar ein wenig mit C aus weiß aber leider nicht wie man den Port anspricht!!DANKE SCHON IM VORAUS!!
wasa
-
Wenn du mit Windows arbeitest, wende dich ans Winapi Forum
oder bei einem anderen Umgebung an das Passende Forum hier (Unix/Linux, Console)
-
Schau mal in der Bios.h nach .
MFG
Marek Swierzy
-
Hullo
Ansi C stellt leider keinen Funktionen zu verfuegung mit welchen man Ports ansprechen koennte. Da musst du auf Betriebsystem abhaengige (API) oder Compiler
abhaenge funktionen zurueck greifen.outport, inport zb fuer Borland und oder Dos
Das ganze wird aber schwieriger wenn du Ports unter Win2k oder XP ansprechen musst. Das leasst dich Win nicht auf die Port Adressen schreiben und schenkt dir ne Schutzverletzung. Du kannst aber dlls verwenden wie io.dll (einfach danach googeln) die stellen Funktzioenen zu verfuegung (SetPort) mit denen kannst dann ohne Probleme auf die Ports zugreifen.
mfg profi hasser 23
-
und wenns für konsole sein soll: schau mal ins konsolenfaq, da steht ein wunderbares programm zu diesem thema
-
@mm-motm genau aus diesen gründen der verweis in die entsprechenden spezielle Foren
- WinAPI für die ganze Windowsfamilie
- Console
- Unix/Linux
-
Ansi C stellt leider keinen Funktionen zu verfuegung mit welchen man Ports ansprechen koennte.
Diese Aussage ist FALSCH ! Hier der Beweis.
Wer mir nicht glauben will geht einfach mal in seinem COMPILER unter Hielfe und tippt da einfach mal bios.h ein./* RS-232 communications (serial I/O) Declaration: þ int bioscom(int cmd, char abyte, int port); þ unsigned _bios_serialcom(int cmd, int port, char abyte); Remarks Both bioscom and _bios_serialcom use BIOS interrupt 0x14 to perform various RS-232 communications over the I/O port given in port. Arg. ³ What It Is/Does ÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ abyte ³ OR combination of bits that specifies COM port settings ³ (ignored if cmd = 2 or 3) cmd ³ Specifies the I/O operation to perform port ³ Identifies the I/O port; 0 = COM1, 1 = COM2, etc. Return Value: For all values of cmd, both functions return a 16-bit integer. The upper 8 bits of the return value are status bits. þ If one or more status bits is set to 1, an error has occured. þ If no status bits are set to 1, the byte was received without error. The lower 8 bits of the return value depend on the value of cmd specified: Value of cmd ³ Lower 8 bits of return value ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ 0 (_COM_INIT) or ³ The lower bits are defined as shown 3 (_COM_STATUS) ³ in the preceding diagram. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ 1 (_COM_SEND) ³ ... ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ 2 (_COM_RECEIVE) ³ The byte read is in the lower bits ³ of the return value--if there is no ³ error (no upper bits are set to 1). Portability: É DOS Ñ UNIX Ñ Windows Ñ ANSI C Ñ C++ Only » º Yes ³ ³ ³ ³ º ÈÍÍÍÍÍÏÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÍÍͼ */ #include <bios.h> #include <conio.h> #define COM1 0 #define DATA_READY 0x100 #define TRUE 1 #define FALSE 0 #define SETTINGS (_COM_1200 | _COM_CHR7 | _COM_STOP1 | _COM_NOPARITY) int main(void) { unsigned in, out, status; _bios_serialcom(_COM_INIT, COM1, SETTINGS); cprintf("... _BIOS_SERIALCOM [ESC] to exit ...\r\n"); for (;;) { status = _bios_serialcom(_COM_STATUS, COM1, 0); if (status & DATA_READY) if ((out = _bios_serialcom(_COM_RECEIVE, COM1, 0) & 0x7F) != 0) putch(out); if (kbhit()) { if ((in = getch()) == '\x1B') break; _bios_serialcom(_COM_SEND, COM1, in); } } return 0; }
MFG
Marek Swierzy
-
na ja.. das ist konsole .. (wie schon im konsolenfaq beschrieben..)
aber weder conio.h noch bios.h sind standard.
-
Wer mir nicht glauben will geht einfach mal in seinem COMPILER unter <b>Hielfe</b> und tippt da einfach mal bios.h ein.
Also ich finde in meinem Compiler nirgends den Punkt "Hielfe" *SCNR
-
Hi,
ich wei? der Beitrag ist ziemlich alt, tortzdem wollte ich nach fragen ob
das Code-Beispiel von Marek Swierzy auch für WinXP funktioniert?mfg,
mrsplinter
-
nur mit tricks: http://www.uninformed.org/?v=2&a=4&t=pdf
-
ich versuch eigendlich nur die RS232-Schnittstelle anzusprechen und zwar mit C in XP halt. Ich glaub deine pdf datei geht da einbsichen zu weit.
also bin für jeden vorschlag dankbar
mfg,
mrsplinter
-
mrsplinter schrieb:
ich versuch eigendlich nur die RS232-Schnittstelle anzusprechen und zwar mit C in XP halt.
dann benutz die winapi funktionen, CreateFile, etc.
-
Ja, aber ich will C-Code nutzen.
mfg,
-
Die WinAPI ist ja auch in/für C geschrieben, also wo ist das Problem? (falls du wirklich nur mit ANSI-C arbeiten willst, wirst du hier nicht weit kommen - C kennt keine RS232-Schnittstellen, wird also auch keine Funktionen bieten, um die anzusprechen)
-
Ok, das wusste ich nicht das C keine RS232-Schnittstellen ansprechen kann. Wie sehe dann eine main aus die CreateFile benutzt. Ein Codeschnippsel würde reichen.
danke
mfg,
-
das könntest du als vorlage nehmen...
HANDLE g_hcomm; // COMM handle unsigned char g_rxframe[1500]; // Rx frame buffer ///////////////////////////////// COMM I/O functions /////////////////////////////// void COMM_Init (char *comport, DWORD baudrate) { COMMTIMEOUTS commtimeouts; DCB dcb; g_hcomm = CreateFile (comport, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); /* Is it open? */ if (g_hcomm == INVALID_HANDLE_VALUE) ERREXIT ("Cannot open COM port"); /* Adjust buffer */ if (SetupComm (g_hcomm, 10000, 10000) == FALSE) ERREXIT ("Cannot set COM buffers"); /* Set speed, flow control, etc */ GetCommState (g_hcomm, &dcb); dcb.BaudRate = baudrate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fRtsControl = 1; /* Set to 0 if RTS not needed */ dcb.fOutxCtsFlow = 1; /* Also set 0 if CTS not needed */ if (SetCommState (g_hcomm, &dcb) == FALSE) ERREXIT ("Cannot configure COMM"); /* No time-outs */ memset (&commtimeouts, 0, sizeof(commtineouts)); if (SetCommTimeouts (g_hcomm, &commtimeouts) == FALSE) ERREXIT ("Cannot set COMMTIMEOUTS"); /* Drain driver's FIFOs .... */ PurgeComm (g_hcomm, PURGE_RXABORT | PURGE_TXABORT | PURGE_RXCLEAR | PURGE_TXCLEAR); printf ("COM port initialized\n"); } /* Writes single byte */ #define COMM_Write(c) \ while (0 == TransmitCommChar (g_hcomm, (c))) \ Sleep(1); /* Reads one byte or -1 if nothing available' int COMM_Read (void) { DWORD commerr; COMSTAT comstat; DWORD bytes_read; UINT8 b; ClearCommError (g_hcomm, &commerr, &comstat); if (comstat.cbInQue == 0) return -1; ReadFile (g_hcomm, &b, 1, &bytes_read, NULL); return b; }
-
hab versucht den Code zu kompilieren mit Visual Studio 2005 und bekomme folgende Fehlermeldungen:
Fehler 1 error C2061: Syntaxfehler: Bezeichner 'g_hcomm' Fehler 2 error C2059: Syntaxfehler: ';' Fehler 3 error C2146: Syntaxfehler: Fehlendes ')' vor Bezeichner 'baudrate' rld 2.c 6 Fehler 4 error C2081: 'DWORD': Name in der formalen Parameterliste ist ungültig 2.c 6 Fehler 5 error C2061: Syntaxfehler: Bezeichner 'baudrate' 6 Fehler 6 error C2059: Syntaxfehler: ';' world 2.c 6 Fehler 7 error C2059: Syntaxfehler: ')' world 2\hello world 2\hello world 2.c 6 Fehler 8 error C2449: '{' auf Dateiebene gefunden - fehlt der Funktionskopf? 2.c 7 Fehler 9 error C2059: Syntaxfehler: '}' d:\samir\erste schritte\hello 51 Fehler 10 fatal error C1071: Unerwartetes Dateiende innerhalb eines Kommentars 2.c 73
-
du solltest vielleicht <windows.h> includen?
-
Ok danke das hätte ich wohl machen sollen, aber habe immer noch Fehlermeldungen:
Warnung 1 warning C4133: 'Funktion': Inkompatible Typen - von 'char *' zu 'LPCWSTR' 13 Warnung 2 warning C4013: 'ERREXIT' undefiniert; Annahme: extern mit Rückgabetyp int 23 Fehler 3 error C2065: 'commtineouts': nichtdeklarierter Bezeichner 41 Fehler 4 fatal error C1071: Unerwartetes Dateiende innerhalb eines Kommentars 75