COM-Port ansprechen
Übersicht

TrabukhBetreff: COM-Port ansprechen |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Weiß hier (zufällig) jemand, ob man mit BB den COM-Port ansprechen kann und dadurch dann Signale senden kann? | ||
![]() |
Abrexxes |
![]() Antworten mit Zitat ![]() |
---|---|---|
https://www.blitzforum.de/viewtopic.php?t=2528 | ||
Trabukh |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Danke, da steht aber nur was über LPT. Ich brauch die serielle Schnittstelle (RS232, COM-Port, wie ihr das nennen wollt...). | ||
![]() |
Jan_Ehemaliger Admin |
![]() Antworten mit Zitat ![]() |
---|---|---|
so, die schlechteste möglichkeit:
openfile("COM1") |
||
between angels and insects |
Trabukh |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Hey, das reicht mir eigentlich schon, danke. Die AT-Befehle kenne ich ja, bzw. kann ich mir bei Bedarf noch mal angucken. | ||
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Das mit dem Lpt auf den Hardport 32h
anwenden. Müste gehen. |
||
Seit 2 Jahren kein BlitzBasic und kein Windows mehr, und stolz darauf ! |
Steve0 |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Hey Trabukh,
ich hab vor kurzem auch über Comports Zeug nachgeschaut. Meine Frage: Was sind die At-Befehle und wo find ich die? |
||
Pixelgenaue 2D SingleSurface Engine!!! |
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Jan_ hat Folgendes geschrieben: so, die schlechteste möglichkeit:
openfile("COM1") OpenFile ?????? Ich glaub nicht ![]() Bei mir funtz das jedenfals nicht ![]() |
||
Seit 2 Jahren kein BlitzBasic und kein Windows mehr, und stolz darauf ! |
![]() |
Vertex |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hi!
Ja mit OpenFile ist das so eine Sache. M$ gibt ja selber an, das man über WinAPI-CreateFile auch Devices wie z.B. LPT, USB oder auch COM ansteuern kann, jedoch ist das je nachdem mehr Glückssache, dass das auch in Blitz funktioniert. Aber weitere wichtige Sachen wie Baudrate bleiben da auf der Strecke. Wenn ihr z.B. ein Signal mit 9600 kb/s sendet, und der Empfänger nur mit 6400 kb/s arbeitet, gehen Daten verloren. Deswegen ist eine WinAPI-Variante mit SetupComm etc. wo man auch Baudrate, Timeout etc. einstellen kann, um einiges besser. Aber mal selber eine Frage: Sind die Daten analog oder digital, weil ich nur jeweils ein Pin für RxD (Receive = empfangen) und TxD(Transmit = senden) sehe. mfg olli |
||
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Digital
+12V = low -12v = High Verry easy. Klemm mal ein Osziloscope an gnd und TxD. Alle Klarheiten beseiteigt ? Ich weiß bloß nicht wie man das in BB krieg[/img] |
||
Seit 2 Jahren kein BlitzBasic und kein Windows mehr, und stolz darauf ! |
- Zuletzt bearbeitet von Sebastian am Mo, Jun 14, 2004 16:16, insgesamt 2-mal bearbeitet
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Geniale Idee:
Com1 = "alte" (in Dos leicht zu erreichende) Harware QwBasic = "alte" (Dos basierende) Sprache Ich schreibe ein Dos-Prog das alle Bytes aus dem Com1-Buffer liest und in eine Cache-Datei schreibt. Dort können die dann mit BB ausgelesn werden. Das heißt ich brauche so wie es aussieht keine Dlls oder so mehr. Leider ist diese Prog auf meine Probleme zugeschnitten. Das heißt den meisten wird dass nix helfen. |
||
Seit 2 Jahren kein BlitzBasic und kein Windows mehr, und stolz darauf ! |
![]() |
Vertex |
![]() Antworten mit Zitat ![]() |
---|---|---|
Sebastian: Ich weiss noch nicht wie das mit -12V gemeint ist, es gibt ja irgendwie keine positiven Elektronen. Ich könnte zwar, um das ganze mal zu testen ein MAX232 Pegelkonvertor dranhängen, der mir dann aus -12V und +12V einfach 0V und +5V macht aber das ist ja nicht Sinn und Zweck der Sache, oder geht es nicht anders?
mfg olli |
||
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Also ich weiß dass auch nicht genau !
Aber ich bastel schon seit 1/2 Jahre mit der Seriellen Schnitstelle. Für mein Flug-Überwachenungs-System deshalb hab ich schon oft ein Osszi am Com hängen gehabt. Vergiss -12/+12 Volt ....... Das Signal ist jedenfalls Digital. Es gibt 1 oder 0 , high oder low. Ganz im Vertrauen die Plus und die Minus hab ich auch noch nicht kapiert ...... ![]() Ich weiß nur das das so ist. |
||
Seit 2 Jahren kein BlitzBasic und kein Windows mehr, und stolz darauf ! |
![]() |
Vertex |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hi!
Ja vllt. sind die dann einfach verpolt k.A. Hier jedenfalls ersteinmal der Anfang vom übersetzen: EDIT: Ein haufen mal editiert, und jetzt apiReadFile umgeändert. Code: [AUSKLAPPEN] Const parityEnable = False ; flag if parity is enabled
Const protocol = 0 ; stores protocol type Const dataBits = 8 ; stores number if data bits Const parityBit = 0 ; stores type of parity bit Const stopBits = 0 ; stores number of stop bits Global timeouts = CreateBank(20) Const INVALID_HANDLE_VALUE = -1 Const GENERIC_READ = $80000000 Const GENERIC_WRITE = $40000000 Const OPEN_EXISTING = 3 Const CBR_110 = 110 Const CBR_300 = 300 Const CBR_600 = 600 Const CBR_1200 = 1200 Const CBR_2400 = 2400 Const CBR_4800 = 4800 Const CBR_9600 = 9600 Const CBR_14400 = 14400 Const CBR_19200 = 19200 Const CBR_38400 = 38400 Const CBR_56000 = 56000 Const CBR_57600 = 57600 Const CBR_115200 = 115200 Const DTR_CONTROL_DISABLE = 0 Const DTR_CONTROL_ENABLE = 1 Const RTS_CONTROL_DISABLE = 0 Const RTS_CONTROL_ENABLE = 1 Const EV_TXEMPTY = 4 Const SETRTS = 3 Const CLRRTS = 4 ; Example --------------------------------------------------------------------------- Print "Open COM1 with 9600 baudrate ..." hCom = openComport(1, 9600) If hCom <> 0 And hCom <> INVALID_HANDLE_VALUE Then Print " Succesfully" Else Print " Error" WaitKey : End EndIf ; Transmit: buffer = CreateBank(6) PokeByte buffer, 0, Asc("H") PokeByte buffer, 1, Asc("e") PokeByte buffer, 2, Asc("l") PokeByte buffer, 3, Asc("l") PokeByte buffer, 4, Asc("o") PokeByte buffer, 5, 0 Print "Sending "+Chr$(34)+"Hello"+Chr$(34)+" ..." If writeComPort(hCom, 6, buffer) Then Print " Succesfully" Else Print " Error" closeComport(hCom) WaitKey : End EndIf FreeBank buffer ; Receive buffer = CreateBank(1024) bytes = CreateBank(4) Print "Receiving datas ..." If readComport(hCom, bytes, 1024, buffer, 2000) Then ; 2 Second timeout Print " Succesfully" Print " Number of receiving bytes "+PeekInt(bytes, 0) ; Datas are in the bank buffer Else Print " Error" EndIf ; Close and end closeComport(hCom) WaitKey : End ; ----------------------------------------------------------------------------------- Function openComport(comport, baudRate) Local dcbBaudRate, driverHandle, dcb If comport > 255 Or comport < 0 Then Return False EndIf Select baudRate Case 110 dcbBaudRate = CBR_110 Case 300 dcbBaudRate = CBR_300 Case 600 dcbBaudRate = CBR_600 Case 1200 dcbBaudRate = CBR_1200 Case 2400 dcbBaudRate = CBR_2400 Case 4800 dcbBaudRate = CBR_4800 Case 9600 dcbBaudRate = CBR_9600 Case 14400 dcbBaudRate = CBR_14400 Case 19200 dcbBaudRate = CBR_19200 Case 38400 dcbBaudRate = CBR_38400 Case 56000 dcbBaudRate = CBR_56000 Case 57600 dcbBaudRate = CBR_57600 Case 115200 dcbBaudRate = CBR_115200 Default Return False End Select driverHandle = apiCreateFile("COM"+comport, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0) If driverHandle = INVALID_HANDLE_VALUE Then Return INVALID_HANDLE_VALUE Else apiSetupComm(driverHandle, 1024, 1024) dcb = CreateBank(80) PokeInt dcb, 00, 80 ; sizeof(DCB) PokeInt dcb, 04, dcbBaudRate ; current baud rate PokeInt dcb, 08, 1 ; binary mode, no EOF check If parityEnable = True Then PokeInt dcb, 12, 1 ; enable parity checking Else PokeInt dcb, 12, 0 ; disable parity checking EndIf If protocol = 0 Then PokeInt dcb, 24, DTR_CONTROL_ENABLE ; DTR flow control type Else PokeInt dcb, 24, DTR_CONTROL_DISABLE ; DTR flow control type EndIf PokeInt dcb, 28, False ; DSR sensitivity If protocol = 1 Then PokeInt dcb, 52, RTS_CONTROL_ENABLE ; RTS flow control Else PokeInt dcb, 52, RTS_CONTROL_DISABLE ; RTS flow control EndIf PokeInt dcb, 60, 17 ; reserved PokeByte dcb, 70, dataBits ; number of bits/byte, 4-8 PokeByte dcb, 71, parityBit ; 0-4=no, odd, even, mark, space PokeByte dcb, 72, stopBits ; 0,1,2 = 1, 1.5, 2 apiSetCommState(driverHandle, dcb) apiSetCommMask(driverHandle, EV_TXEMPTY) EndIf FreeBank dcb Return driverHandle End Function Function closeComport(driverHandle) If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then Return False Else Return apiCloseHandle(driverHandle) EndIf End Function Function writeComport(driverHandle, numBytes, buffer) Local state, temp If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then Return False EndIf apiEscapeCommFunction(driverHandle, SETRTS) temp = CreateBank(4) state = apiWriteFile(driverHandle, buffer, numBytes, temp, 0) apiEscapeCommFunction(driverHandle, CLRRTS) FreeBank temp Return state End Function Function readComport(driverHandle, bytesRead, bufferSize, buffer, tOut) Local comErrors, comStat If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then Return False EndIf PokeInt timeouts, 00, 0 PokeInt timeouts, 04, 0 PokeInt timeouts, 08, tOut PokeInt timeouts, 12, 0 apiSetCommTimeouts(driverHandle, timeouts) comErrors = CreateBank(4) comStat = CreateBank(10) apiClearCommError(driverHandle, comErrors, comStat) apiReadFile(driverHandle, buffer, bufferSize, bytesRead, 0) FreeBank comErrors FreeBank comStat Return True End Function Zitat: lib "Kernel32.dll"
apiCreateFile%(lpFileName$, dwDesiredAccess, dwShareMode, lpSecurrityAttributes, dwCreationDistribution, dwFlagsAndAttributes, hTemplateFile) : "CreateFileA" apiSetupComm%(hFile, dwInQueue, dwOutQueue) : "SetupComm" apiGetCommState%(hFile, lpDCB*) : "GetCommState" apiSetCommState%(hFile, lpCDB*) : "SetCommState" apiSetCommMask%(hFile, dwEvtMask) : "SetCommMask" apiCloseHandle%(hObject) : "CloseHandle" apiEscapeCommFunction%(hFile, dwFunc) : "EscapeCommFunction" apiWriteFile%(hFile, lpBuffer*, nNumberOfBytesToWrite, lpNumberOfBytesWritten*, lpOverlapped) : "WriteFile" apiSetCommTimeouts%(hFile, lpCommTimeouts*) : "SetCommTimeouts" apiClearCommError%(hFile, lpErrors*, lpStat*) : "ClearCommError" apiReadFile%(hFile, lpBuffer*, nNumberOfBytesToRead, lpNumberOfBytesRead*, lpOverlapped) : "ReadFile" und der Originalcode: Code: [AUSKLAPPEN] //---------------------------------------------------------------------------
// METHODEN FÜR RS232 Kommunikation //--------------------------------------------------------------------------- //-Eingebundene-Header-Dateien----------------------------------------------- #include <vcl.h> #include <math.h> #include <stdio.h> #pragma hdrstop //-Variablen--------------------------------------------------------- extern HANDLE DriverHandle; HANDLE com_status_h; // saves COM Status int portnr_c = 1; // stores COM-Port number DWORD baudrate_dw = 9600; // stores Baudrate bool open_com_b = false; // set if COM opened DWORD rec_bytes_dw = 0; // number of received bytes bool par_enable_b; // flag if parity is enabled char protocol_c = 0; // stores protokol type char databits_c = 8; // stores number if data bits char paritbit_c = 0; // stores type of parity bit char stopbits_c = 0; // stores number of stop bits extern COMMTIMEOUTS timeouts; //-Prototypen---------------------------------------------------------------- HANDLE Open_Comport (DWORD Comport, DWORD BaudRate); int Close_Comport (HANDLE DriverHandle); int Write_Comport (HANDLE DriverHandle, DWORD NumBytes, void *Buffer); int Read_Comport (HANDLE DriverHandle, DWORD *BytesRead, DWORD BufferSize, void *Buffer,DWORD tout_dw); //-Methoden------------------------------------------------------------------ // // Open_Comport // ------------- // Opens a communications port for use. // // Returns: Handle number if successful, else 0 // // Comport = Communications port to open (ie. 1, 2, 3, 4, ... 255) // BaudRate = Desired baud rate (ie, 110, 300, 600, 1200, 2400, 4800, 9600, 19200, ...) // HANDLE Open_Comport (DWORD Comport, DWORD BaudRate) { //HANDLE DriverHandle; unsigned char Com_Name[10]; DWORD DCB_Baud_Rate; DCB Our_DCB; // // Range check the Comport number // if (Comport > 255) { return INVALID_HANDLE_VALUE; } // // Make a COMPORT name from a number // sprintf (Com_Name, "COM%i", Comport); // // Convert the Baud Rate to the Com library define for the DCB // switch (BaudRate) { case 110: DCB_Baud_Rate = 110; break; case 300: DCB_Baud_Rate = CBR_300; break; case 600: DCB_Baud_Rate = CBR_600; break; case 1200: DCB_Baud_Rate = CBR_1200; break; case 2400: DCB_Baud_Rate = CBR_2400; break; case 4800: DCB_Baud_Rate = CBR_4800; break; case 9600: DCB_Baud_Rate = CBR_9600; break; case 19200: DCB_Baud_Rate = CBR_19200; break; case 38400: DCB_Baud_Rate = CBR_38400; break; case 57600: DCB_Baud_Rate = CBR_57600; break; case 115200: DCB_Baud_Rate = CBR_115200; break; default: return INVALID_HANDLE_VALUE; } // // Open a channel to the Comport - This example DOES NOT use overlapped I/O // Since the benefits of overlapped I/O are few, we suggest not using it. // DriverHandle = CreateFile (Com_Name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); // // Do we have a valid handle? (If not, the driver probably isn't loaded) // if (DriverHandle == INVALID_HANDLE_VALUE) { return (DriverHandle); } else { // // The SetupComm() function establishes the Transmit and Receive // buffer sizes. // SetupComm (DriverHandle, 1024, 1024); // // Obtain the current DCB structure. this can be saved away for restore after // the application is done using the Comport // GetCommState (DriverHandle, &Our_DCB); // // Fill in the DCB structure with our own settings. // Our_DCB.BaudRate = DCB_Baud_Rate; if (par_enable_b) { Our_DCB.fParity = 1; } else { Our_DCB.fParity = 0; } Our_DCB.fOutxCtsFlow = 0; Our_DCB.fOutxDsrFlow = 0; if (protocol_c == 0x00) { Our_DCB.fDtrControl = DTR_CONTROL_ENABLE; } else { Our_DCB.fDtrControl = DTR_CONTROL_DISABLE; } Our_DCB.fDsrSensitivity = FALSE; Our_DCB.fTXContinueOnXoff = 0; Our_DCB.fOutX = 0; Our_DCB.fInX = 0; Our_DCB.fErrorChar = 0; Our_DCB.fNull = 0; if (protocol_c == 0x01) { Our_DCB.fRtsControl = RTS_CONTROL_ENABLE; } else { Our_DCB.fRtsControl = RTS_CONTROL_DISABLE; } Our_DCB.fAbortOnError = 0; Our_DCB.ByteSize = databits_c; Our_DCB.Parity = paritbit_c; Our_DCB.StopBits = stopbits_c; // // Configure the comport with our new DCB // SetCommState (DriverHandle, &Our_DCB); // // Setup a mask that allows us to tell if the port is done transmitting // the current buffer of data // SetCommMask (DriverHandle, EV_TXEMPTY); } return DriverHandle; } //--------------------------------------------------------------------------- // // Close_Comport // ------------- // Closes a communications port previously opened // // Returns: TRUE on success, FALSE on failure // // DriverHandle = Handle returned on Open_Comport() call // int Close_Comport (HANDLE DriverHandle) { // // Sanity check on the Handle // if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE)) { return FALSE; } // // Close the communications port // CloseHandle (DriverHandle); return TRUE; } //--------------------------------------------------------------------------- // // Write_Comport // ------------- // Writes a buffer of data out the RS232 commmunications port // // Returns: TRUE on success, FALSE on failure // // DriverHandle = Handle returned on Open_RS232() call // NumBytes = Number of bytes to send // Buffer = Buffer of data to send // int Write_Comport (HANDLE DriverHandle, DWORD NumBytes, void *Buffer) { DWORD EvtMask, BytesWritten; BOOL status; // // Sanity check on the Handle // if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE)) { return FALSE; } // // Some devices may require setting RTS or DTR to specific states prior to // tranmission of data // EscapeCommFunction (DriverHandle, SETRTS); // // Output the data to the Comport // status = WriteFile (DriverHandle, Buffer, NumBytes, &BytesWritten, 0); // // If you want, use the WaitCommEvent() function to wait for the // data Transmission to complete // // WaitCommEvent(DriverHandle, &EvtMask, NULL); // // Some devices may require clearing RTS or DTR to after transmission of data // EscapeCommFunction (DriverHandle, CLRRTS); return status; } //--------------------------------------------------------------------------- // // Read_Comport // ------------- // Reads data received on the RS232 communications port // // Returns: TRUE on success, FALSE on failure // // DriverHandle = Handle returned on Open_RS232() call // BytesRead = Number of bytes read by the ReadFile() function // BufferSize = Size of Buffer in bytes // Buffer = Data buffer that holds received data // int Read_Comport (HANDLE DriverHandle, DWORD *BytesRead, DWORD BufferSize, void *Buffer,DWORD tout_dw) { COMSTAT Our_Comstat; DWORD Com_Errors, BytesToRead; // // Sanity check on the Handle // if ((DriverHandle == 0) || (DriverHandle == INVALID_HANDLE_VALUE)) { return FALSE; } // // By far the most efficient way to input Comport data is the following: // --------------------------------------------------------------------- // A) See how much data is present // B) Read exactly that amount of data // // Note: It is possible to ask for more data than is present, but the ReadFile() // function call will not return until that amount of data has been received. // Some programs will request more data and establish a timeout using the // SetCommTimeouts() function to return from the ReadFile() function when // not enough data has been receive. Although this may work, it is inefficient // and generally works like crap. For everyone's sake, use the technique above // and handle your own timeout in applications code. // timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = tout_dw; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(DriverHandle,&timeouts); // // Check the Input buffer // ClearCommError (DriverHandle, &Com_Errors, &Our_Comstat); // // Read the data, no (NULL) Overlapped I/O // ReadFile (DriverHandle, Buffer, BufferSize, BytesRead, NULL); return TRUE; } mfg olli |
||
- Zuletzt bearbeitet von Vertex am Mi, Jun 16, 2004 17:22, insgesamt 3-mal bearbeitet
![]() |
Markus2 |
![]() Antworten mit Zitat ![]() |
---|---|---|
Sebastian hat Folgendes geschrieben: ...Ganz im Vertrauen die Plus und die Minus hab ich auch noch nicht kapiert ....
Spannung ist das ausgleichbestreben von Ladungen . Du mißt also nur die differenz zur Masse . Haste mehr ist es + haste weniger dann - Die Masse ist also ein Bezugspunkt . Würdest du von -12 bis +12 V messen haste 24 Volt . Ich hoffe es stimmt so was ich sag weil lange her das ![]() |
||
![]() |
Sebastian |
![]() Antworten mit Zitat ![]() |
---|---|---|
Bingo !
Jezte raf ich das !!!! |
||
![]() |
Vertex |
![]() Antworten mit Zitat ![]() |
---|---|---|
Möp, will verstehen wer will - ich nicht! ![]() Wie werden eigentlich AT Befehle übertragen? Soviel wie ich gelesen habe, kann man sie nur nach dem Start des Modems versenden. Sind das nur Strings? http://www.hettronic.de/hettro.../56k/3.htm wens interessiert. Habe z.B. mal in den Buffer den String "ATL3"+Chr$(13)+Chr$(10)+"ATDP123456789"+Chr$(13)+Chr$(10) ATL3 steht dafür, das der Lautsprecher auf Lautstärke 3 geschalten wird, und ATDP123456789 dafür, das er per Frequenzwahl die Nummer 123456789 wählen soll. Ich sehe zwar, das das Modem TxD anzeigt, jedoch höre ich nicht, das er etwas wählen will. mfg olli |
||
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group