Seite 1 von 1

Arduino - Serielle Kommunikation

Verfasst: 11.01.2014 12:25
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.

Re: Arduino - Serielle Kommunikation

Verfasst: 11.01.2014 19:51
von HeX0R
Interessiert Dich der Inhalt von length etwa gar nicht?

Re: Arduino - Serielle Kommunikation

Verfasst: 11.01.2014 20:29
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.

Re: Arduino - Serielle Kommunikation

Verfasst: 18.12.2017 19:57
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:

Re: Arduino - Serielle Kommunikation

Verfasst: 19.12.2017 09:19
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

Re: Arduino - Serielle Kommunikation

Verfasst: 19.12.2017 09:59
von xXRobo_CubeXx
@funkheld
Du hast in einem Beitrag 2x Gruss gesagt. :o :mrgreen: