B4R Question String.split into an array..how?

Discussion in 'B4R Questions' started by jimseng, Jul 10, 2018 at 3:44 PM.

  1. jimseng

    jimseng Member Licensed User

    I am receiving a comma separated string eg "100,150,175,200" from a PC via USB:
    I am trying to get it into an array such as:
    dim i(3) as int
    dim count as int
    Dim c As String = (JoinStrings(Array As String(BC.StringFromBytes(Buffer))))
    from the tutorial I can split it around the comma:
    For Each s() As Byte In bcx.Split(c,",")
    count = count + 1
    i(count) = s
    R.AnalogWrite(i(count))
    G.AnalogWrite(i(count))
    B.AnalogWrite(i(count))
    W.AnalogWrite(i(count))


    next

    This doesn't work (Cannot cast type). There must be a more elegant way of doing this.
    Hopefully it is obvious what I am trying to do-write the values of the string to 4 pwm outputs.

    How might I acheive this.

    Many thanks
     
  2. mark35at

    mark35at Well-Known Member Licensed User

    Firstly, before DonManfred or Erel gets to you, please use the code tags (left from the disk) when posting. It makes your code easier to read.

    What exactly are you getting from the USB port, a string or bytes?
     
  3. mark35at

    mark35at Well-Known Member Licensed User

    I think you were lloking here maybe? Link At this code:

    Code:
    Dim b() As Byte = " abc,def,ghijkl "
       
    Log("Substring: ", bc.SubString(b, 3)) 'Substring: c,def,ghijkl
       For Each s() As Byte In bc.Split(b, ",")
         
    Log("Split: ", s)
         
    'Split: abc
         'Split: def
         'Split: ghijkl
       Next
    In this case "s" is an array and you can call each member s(0), s(1) etc.
     
  4. tigrot

    tigrot Well-Known Member Licensed User

    Use type string carefully expecially on low Ram devices. This structure uses lot of ram space.
     
  5. jimseng

    jimseng Member Licensed User

    Wow I have been at this four hours and really don't understand.

    So I am sending from a terminal something like 100,200,220,254 (Arduino serial port monitor)

    All I want to do is split the string around the comma and get an integer value from it that I can write to an analogue pin
    (eg
    Code:
    R.AnalogWrite(254)
    )
    log(s) returns the values "correctly"
    Code:
    log(s1)
    returns something like 49. If done in a loop it returns 48 48 51 53

    if I send a string such as 1,2,3,4 I get "Out of bounds error. Array length = 1, Index = 1"

    I would have thought this would be so simple but I haven't found an example that helps me, and I have been searching for hours. I am new to this. (Can you tell?)
     
  6. mark35at

    mark35at Well-Known Member Licensed User

    Not a problem being new, we were all new sometime. Can you post your whole code or zip it and post?

    Where are you sending from and where is is going?
     
  7. jimseng

    jimseng Member Licensed User

    Currently I am using the Arduino IDE serial port monitor to send a simple string. Next I will be using a VB.net app but I don't know where this is going. It is just a bit of fun but it might involve raspberry pi/bluetooth. I guess I should be sending bytes instead of strings but I am trying to keep it generic for now.
    I am investigating RGBW LEDS for a home lighting project using the PWM outputs from the Arduino.

    No animals will be harmed in the process of developing this project.
     

    Attached Files:

  8. tigrot

    tigrot Well-Known Member Licensed User

    You write log(s(1)).
    Try with S(0)...
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Don't. Start with this: https://www.b4x.com/android/forum/threads/tool-external-serial-connector.65724/#content

    You cannot directly convert arrays of bytes to numbers. You must first convert it to string:
    Code:
    Dim bc As ByteConverter
    [code]
    For Each s() As Byte In bcx.Split(c,",")
    count = count + 
    1
    i(count) = bc.StringFromBytes(s)
    Make sure to set #StackBufferSize to 600 or more.

    Strings are the least suitable format for communication with microcontrollers. Your best option is to use B4RSerializator.
     
    Johan Hormaza likes this.
  10. jimseng

    jimseng Member Licensed User

    I have been looking at the B4RSerializator but not got anywhere. I get "Out of bounds error. Array length = 150, Index = 65535 " when sending 4 bytes from my .net app.
    I have tried the b4j example, which doesn't really seem any different from hyperterminal or some other serial comms. I must be approaching this badly but I see no examples that come close to what I'm after.
     
  11. jimseng

    jimseng Member Licensed User

    I had a quick dabble within the arduino IDE and perhaps the attached sketch would demonstrate what I am trying to do. I would rather stick with B4R as it is more familiar to me. This Arduino sketch works well when sending bytes from vb.net (rather than strings). If I could achieve this in B4R I would be much happier.
     

    Attached Files:

  12. Erel

    Erel Administrator Staff Member Licensed User

  13. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Try this code, it worked perfectly for me.
    Code:
    Sub ReceiveFileName (Buffer() As Byte)
        
    Dim J As Byte = 0
        
    Dim Anio, mes, hora, Dia, minutos As Object
        
    Log("Bufer File: ",Buffer)
        
    Log("Trim: ", bc.Trim(Buffer)) 
        
    For Each s() As Byte In bc.Split(Buffer, "/")
            j = j + 
    1

            
    Select J
                
    Case 1
                    Anio = bc.stringfrombytes(s)
                
    Case 2
                    mes = bc.stringfrombytes(s)
                
    Case 3
                    Dia = bc.stringfrombytes(s)
                
    Case 4
                    hora = bc.stringfrombytes(s)
                
    Case 5
                    minutos = bc.stringfrombytes(s)
            
    End Select
            
    Log("Split: ", s, "Posicion: ", J)
        
    Next
        
    Log("Año: ",Ano," Mes: ",mes," Dia: ",Dia," Hora: ",hora," Min: ",minutos)
        DateTimes(Anio,mes,Dia,hora,minutos)
        void_loop.Enabled = 
    True
    End Sub

    Sub DateTimes(Anio As Object, mes As Object, Dia As Object, Horas As Object, Minutos As Object)
    .
    .
    .
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice