Wie sende ich was an COMPORT x?

Anfängerfragen zum Programmieren mit PureBasic.
Aspirant
Beiträge: 101
Registriert: 19.10.2006 12:19

Wie sende ich was an COMPORT x?

Beitrag von Aspirant »

Hallo,
Habe zwar was im Code Archive gefunden und auch im Forum aber finde das ist alles viel zu aufwending ich den es mmus doch auch einfacher gehen.

ich möchte einfach nur ein "a" an com1 senden mehr nicht
das mus mit 3 bis 4 zeilen möglich sein oder?

MFG Aspi
Ich würde mich ja gerne geistig mit dir duellieren, aber wie ich sehe bist du unbewaffnet.
================================
WinXP Pro, W2k3, Eisfair
-----------------------------
PB 4.40 auf WinXPpro kauf
Benutzeravatar
HeX0R
Beiträge: 3042
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win11 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2 + 3
Kontaktdaten:

Beitrag von HeX0R »

3-4 Zeilen sind es nicht, aber kurz ist es allemal:
http://www.purebasic.fr/german/viewtopi ... 7065#78436

[Edit]
Ist allerdings noch PB3.94 sehe ich gerade, keine Ahnung ob das mit den 4.0er Filebefehlen noch so geht, ansonsten die API-Pendants nehmen.
Benutzeravatar
HeX0R
Beiträge: 3042
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win11 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2 + 3
Kontaktdaten:

Beitrag von HeX0R »

Das hier benutze ich selbst, geht auch unter PB4.0 allerdings eben nur Windows:

Code: Alles auswählen

EnableExplicit
#V24_MODE_BINARY        	= $0001 ;binary mode MUST BE TRUE
#V24_MODE_PARITY_CHECK  	= $0002 ;Parity chacking is performed and errors are reported
#V24_MODE_MONITOR_CTS   	= $0004 ;If this member is TRUE, the DSR (data-set-ready) signal is monitored
      														;For output flow control. If this member is TRUE And DSR is turned off,
      														;output is suspended Until DSR is sent again
#V24_MODE_MONITOR_DSR   	= $0008 ;If this member is TRUE, the DSR (data-set-ready) signal is monitored
      														;For output flow control. If this member is TRUE And DSR is turned off,
     															;output is suspended Until DSR is sent again
#V24_MODE_DTR_ON        	= $0010 ;Enables the DTR line when the device is opened and leaves it on.
#V24_MODE_DTR_HANDSHAKE 	= $0020 ;Enables DTR handshaking. If handshaking is enabled, it is an error for the application
      														;To adjust the line by using the EscapeCommFunction function
#V24_MODE_NEED_DSR     	  = $0040 ;If this member is TRUE, the communications driver is sensitive to the state of the DSR signal.
      														;The driver ignores any bytes received, unless the DSR modem input line is high.
#V24_MODE_CONT_AFTER_XOFF = $0080 ;If this member is TRUE, transmission continues after the input buffer has come within
      														;XoffLim bytes of being full And the driver has transmitted the XoffChar character To stop
      														;receiving bytes. If this member is FALSE, transmission does not Continue Until the input buffer
      														;is within XonLim bytes of being empty And the driver has transmitted the XonChar character To resume reception.
#V24_MODE_XONOFF_SEND	    = $0100 ;Indicates whether XON/XOFF flow control is used during transmission. If this member is TRUE,
	      													;transmission stops when the XoffChar character is received And starts again when the XonChar character is received. 
#V24_MODE_XONOFF_RECV   	=	$0200 ;Indicates whether XON/XOFF flow control is used during reception.
      														;If this member is TRUE, the XoffChar character is sent when the input buffer
      														;comes within XoffLim bytes of being full, And the XonChar character is sent
      														;when the input buffer comes within XonLim bytes of being empty.
#V24_MODE_ACTIVE_PARITY 	= $0400 ;Indicates whether bytes received with parity errors are replaced with the character specified by the ErrorChar member.
      														;If this member is TRUE And the fParity member is TRUE, replacement occurs.
#V24_MODE_DISCARD_NULL  	= $0800 ;If this member is TRUE, null bytes are discarded when received.
#V24_MODE_RTS_MODE_BIT_01 = $1000 ;00 = Disables the RTS line when the device is opened and leaves it disabled.
#V24_MODE_RTS_MODE_BIT_10 = $2000 ;01 = Enables the RTS line when the device is opened and leaves it on.
      														;10 = Enables RTS handshaking. The driver raises the RTS line when the "type-ahead" (input) buffer is less than
      														;one-half full And lowers the RTS line when the buffer is more than
      														;three-quarters full. If handshaking is enabled, it is an error
      														;for the application To adjust the line by using the EscapeCommFunction function.
      														;11 = Specifies that the RTS line will be high if bytes are available for transmission.
     															;After all buffered bytes have been sent, the RTS line will be low.
         													;       !!Windows Me/98/95:  This value is not supported.!!
#V24_MODE_STOP_ON_ERROR   = $4000 ;If this member is TRUE, the driver terminates all read and write operations with an error status if an error occurs.
         													;The driver will not accept any further communications operations
         													;Until the application has acknowledged the error by calling the ClearCommError function.


#V24_MODE_DEFAULT         = #V24_MODE_RTS_MODE_BIT_01 | #V24_MODE_CONT_AFTER_XOFF | #V24_MODE_BINARY | #V24_MODE_PARITY_CHECK

Procedure HandleError(Result.l, Text$)

  If Result = 0
    MessageRequester("Error", Text$, #MB_ICONERROR)  
  EndIf
  
EndProcedure

ProcedureDLL.l Open_Com(Com.l, BaudRate.l, Parity.l, StopBits.l, ByteSize.l, Buffer_Size_IN.l, Buffer_Size_OUT.l, Flags.l = #V24_MODE_DEFAULT, TimeOUT.l = 0)
	Protected hCom, ct.COMMTIMEOUTS, TimeOut_Const.l, dcb.DCB, PortConfig.DCB, ComError.l
	
  hCom = CreateFile_("\\.\COM" + Str(Com), #GENERIC_READ |#GENERIC_WRITE ,0,0, #OPEN_EXISTING ,#FILE_FLAG_OVERLAPPED ,0)
  If hCom <> #INVALID_HANDLE_VALUE  
      
    If GetCommState_(hCom, @PortConfig)
    	;If WindowID And WindowEvent
    		SetCommMask_(hCom, #EV_RXCHAR )
    	;EndIf
      HandleError( SetupComm_(hCom, Buffer_Size_IN, Buffer_Size_OUT), "SetupComm()" )
      TimeOut_Const = 1
      
      If TimeOUT = 0
      	TimeOUT = 200
      EndIf
      
      ct\ReadIntervalTimeout         = #MAXDWORD
      ct\ReadTotalTimeoutMultiplier  = TimeOut
      ct\ReadTotalTimeoutConstant    = TimeOut_Const
      ct\WriteTotalTimeoutMultiplier = TimeOut
      ct\WriteTotalTimeoutConstant   = TimeOut_Const
      
      HandleError( SetCommTimeouts_(hCom, ct), "SetCommTimeouts()" )
    
      HandleError( GetCommState_(hCom, @dcb), "GetCommState()" )
      
      If Flags = 0
      	Flags = #V24_MODE_DEFAULT
      EndIf
      
      dcb\BaudRate  = BaudRate
      dcb\Parity    = Parity
      dcb\StopBits  = StopBits
      dcb\ByteSize  = ByteSize  
      dcb\fbits     = Flags
      
      HandleError( SetCommState_(hCom, @dcb), "SetCommState()" )
      ClearCommError_(hCom, @ComError, #Null)
      HandleError( PurgeComm_(hCom, #PURGE_TXCLEAR | #PURGE_RXCLEAR), "PurgeComm()")

    EndIf
  EndIf
  ProcedureReturn hCom
EndProcedure

ProcedureDLL.l Close_Com(Com.l)

	If Com
	  CloseHandle_(Com)
  EndIf
  
EndProcedure

ProcedureDLL.l Read_Com(Com.l, Anzahl, *Buffer)
	Protected CountBuffer.l, Result.l, o.OVERLAPPED
	
	If Com
		ReadFile_(Com, *Buffer, Anzahl, @CountBuffer, @o)
		GetOverlappedResult_(Com, @o, @Result, #True)
	EndIf
	ProcedureReturn Result
	
EndProcedure

ProcedureDLL Write_Com(Com.l, String.s)
	Protected CountBuffer.l, o.OVERLAPPED, Result.l

	If Com
		WriteFile_(Com, @String, Len(String), @CountBuffer, @o)
		GetOverlappedResult_(Com, @o, @Result.l, #True)
	EndIf
	
	ProcedureReturn Result
	
EndProcedure
Wie immer nahezu nix kommentiert, es lebe Learning by Doing (bzw. by Reading) :mrgreen:
Aspirant
Beiträge: 101
Registriert: 19.10.2006 12:19

Beitrag von Aspirant »

He das ist absolut nicht böse gemeint aber ist das nicht etwas viel Code für ein simples :

copy "A" > com1

ok können vieleicht auch 2 oder 3 Zeichen werden aber ich möchte echt Port auf - Zeichen senden - Port zu

Kein handschake kein weiteres Prüfen ob daten endfangen wurden alles absolut egal.

ich habe mir mal die MV-Lieb an gesehen doch da haut noch nicht so ganz hin was ich will naja werde mich Dinstag noch mal damit aus ein ander setzen. wenn ich das Kassen modul habe

Aber bedanken tue ich mich trozdem vielleicht hat ja noch jemand was einfacheres.
Ich würde mich ja gerne geistig mit dir duellieren, aber wie ich sehe bist du unbewaffnet.
================================
WinXP Pro, W2k3, Eisfair
-----------------------------
PB 4.40 auf WinXPpro kauf
Benutzeravatar
NicTheQuick
Ein Admin
Beiträge: 8809
Registriert: 29.08.2004 20:20
Computerausstattung: Ryzen 7 5800X, 64 GB DDR4-3200
Ubuntu 24.04.2 LTS
GeForce RTX 3080 Ti
Wohnort: Saarbrücken

Beitrag von NicTheQuick »

Warum ist dir das zu kompliziert? Das, was der Code macht, macht
normalerweise eben "copy". Und ein simples Aufrufen einer Procedure sollte
nicht zu aufwendig sein. Da ist eine DLL komlizierter.
Georg
Beiträge: 29
Registriert: 17.06.2005 19:04

Beitrag von Georg »

Code: Alles auswählen

CreateFile(0, "Com1:")
WriteString(0, "A")
CloseFile(0)
kurz genug?
Georg
Antworten