Arduino - Serielle Kommunikation

Für allgemeine Fragen zur Programmierung mit PureBasic.
pyromane
Beiträge: 276
Registriert: 09.09.2010 14:10

Arduino - Serielle Kommunikation

Beitrag von pyromane »

Hallo,

nach langer Inaktivität melde ich mich direkt mal mit einer Frage wieder. Ich würde gerne Daten vom Arduino via Serielle Kommunikation loggen. Der Verbindungsaufbau klappt (zumindest laut OpenSerialPort) auch soweit, AvailableSerialPortInput liefert mir auch was brauchbares zurück aber wenn ich es lesen will, kommen Lesefehler + leere Zeilen. Hier mein Code Snippet:

Code: Alles auswählen

length.i = 0
*buffer = AllocateMemory(1024)


If OpenSerialPort(0, "COM8", 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 32, 32)
  
  Debug "Successfully connected"
  
  While IsSerialPort(0)
    
    length = AvailableSerialPortInput(0)
    If Not ReadSerialPortData(0, *buffer, length)
      Debug "Error while reading input data"
    Else
        Debug PeekS(*buffer, length)
    EndIf
    
  Wend
  
  Debug AvailableSerialPortInput(0)
  
Else
  Debug "Check COM-Port"
EndIf
PureBasic Version: 5.21 LTS

Ich bedanke mich schon mal für eure Hilfe.
PureBasic 5.21 LTS auf Windows 7 x64 (Intel i5 2500K, 8GB Ram, Nvidia GTX 780)
Benutzeravatar
HeX0R
Beiträge: 2959
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win10 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2
Kontaktdaten:

Re: Arduino - Serielle Kommunikation

Beitrag von HeX0R »

Interessiert Dich der Inhalt von length etwa gar nicht?
pyromane
Beiträge: 276
Registriert: 09.09.2010 14:10

Re: Arduino - Serielle Kommunikation

Beitrag von pyromane »

Habe das ganze nun ans laufen bekommen. Hier mal ein Snippet wenn irgendwer auch mal nen Arduino mit PB ansteuern will:

Code: Alles auswählen

*buffer = AllocateMemory(128)


If OpenSerialPort(0, "COM8", 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 32, 32)
  
  Debug "Successfully connected"
  
    While IsSerialPort(0)
    
    length.i = AvailableSerialPortInput(0)
    
    If length > 0
      If Not ReadSerialPortData(0, *buffer, length)
        Debug "Error while reading input data"
      Else
        result.s = PeekS(*buffer, length)
        buffer.s + result
        i = CountString(buffer, Chr(13)+Chr(10))
        If i > 0 ; check for newline
          For x.i = 1 To i
            line.s = StringField(buffer, x, Chr(13)+Chr(10))
            Debug line
          Next
          buffer = StringField(buffer, x+1, Chr(13)+Chr(10)) ; add the new line to the buffer
        EndIf
      EndIf
    EndIf
    Delay(5)
    
  Wend
  
Else
  Debug "Check serial port"
EndIf
So, hab es nochmal aktualisiert, da noch ein Bug drin war. Code gibt nur aus wenn println verwendet wird.
PureBasic 5.21 LTS auf Windows 7 x64 (Intel i5 2500K, 8GB Ram, Nvidia GTX 780)
Zefiro_flashparty
Beiträge: 6
Registriert: 21.10.2008 21:38

Re: Arduino - Serielle Kommunikation

Beitrag von Zefiro_flashparty »

someone try to connect to the arduino one, with a cnc shield?
sending to the com directly the GRBL protocol? :lurk:
I saw some programs like the universal code sender, which one can send the commands manually, to the GRBL
someone programmed something with that?
(I clarify that my idea is not to use a cnc, only the shield platform, for its advantages, to simply move the motors) now I had two steppers connected with a web camera :)
I can send commands like G0 X10 and the motor turns to position 10 of the cnc, or put g0 x1 and it returns 9 turning contrarily, in fact as I simply want to turn to the left or right, up or down, it serves me well.

my idea is to control steppers, or a robot arm,
for some simple things, read sensor data,
or send commands to some program.

I'm going to see if I can do something with that, :lamer:
funkheld
Beiträge: 636
Registriert: 31.12.2009 11:58

Re: Arduino - Serielle Kommunikation

Beitrag von funkheld »

Port abfragen und dann geht es weiter.....
Nebenbei kann man auch andere Arbeiten machen.

Gruss

Code: Alles auswählen

 If port > 0
    While AvailableSerialPortInput(port)  
      ReadSerialPortData(port, @r_byte, 1)
      text_e + Chr(r_byte)
      SetGadgetText(#Editor_0, text_e)
    Wend 
  EndIf  
Gruss
Benutzeravatar
xXRobo_CubeXx
Beiträge: 120
Registriert: 12.06.2015 16:08
Computerausstattung: Version 5.41 LTS
Wohnort: Wohnort
Kontaktdaten:

Re: Arduino - Serielle Kommunikation

Beitrag von xXRobo_CubeXx »

@funkheld
Du hast in einem Beitrag 2x Gruss gesagt. :o :mrgreen:
Version 5.41 LTS :)
Antworten