[Gambas-user] Serial I/O with byte data

nando_f at ...951... nando_f at ...951...
Mon May 15 13:33:50 CEST 2017


My reply to the comment:

> Gambas Strings are not null-terminated and can
> contain any sequence of bytes, which includes non-printable characters
> (which I think is what you meant by "unreadable" characters).

You should always consider String as an array of bytes.
Just because humans can't read them or because they cannot be displayed,
means nothing to the computer.  It only means something to the human.

While the computer is processing and doing work, we don't need to see it.

I use string exclusively for binary data...especially serial port stuff.
Strings are far easier than a byte array.

If you insist on using a byte array, consider a large fixed-size array.
For example: b[10000].
Resizing a dynamic array every time is cpu work.

-Fernando


--
Open WebMail Project (http://openwebmail.org)


---------- Original Message -----------
From: Mike Crean <mike.crean at ...2897...>
To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Sent: Mon, 15 May 2017 10:46:01 +0000 (UTC)
Subject: Re: [Gambas-user] Serial I/O with byte data

> If you are talking about a byte the likes of that used in modbusrtu you will 
> need to do some high low bit manipulation on it. RegardsMike
> 
>     On Monday, 15 May 2017, 13:19, Alexie <ualex73 at ...626...> wrote:
> 
>  Following should work for you:
> 
> Public Sub XXX_Read()
> 
>   Dim iLen As Integer
>   Dim aData As Byte[]
> 
>   iLen = Lof(Last)
>   aData = New Byte[]
>   aData.Resize(iLen)
>   Try aData.Read(Last, 0, iLen)
> 
> End
> 
> You need to get the length (lof) first, then resize array ... only then you
> can read it.
> 
> 2017-05-14 21:56 GMT+02:00 Tobias Boege <taboege at ...626...>:
> 
> > On Sun, 14 May 2017, alexchernoff wrote:
> > > Good day all,
> > >
> > > Anyone know how to read data received on serial port into an array of
> > bytes
> > > instead of string?
> > >
> >
> > I think that's not possible.
> >
> > > String gets unreadable binary characters, so how can I
> > > put them into byte[] or so?
> > >
> >
> > This shouldn't be a problem. Gambas Strings are not null-terminated and can
> > contain any sequence of bytes, which includes non-printable characters
> > (which
> > I think is what you meant by "unreadable" characters).
> >
> > If you insist on a Byte[], read it into a String and use
> > Byte[].FromString(),
> > but there is no way to go around the String, AFAICT (and that is because
> > there is no need to, as Strings can handle binary data just fine).
> >
> > Regards,
> > Tobi
> >
> > --
> > "There's an old saying: Don't change anything... ever!" -- Mr. Monk
> >
> > ------------------------------------------------------------
> > ------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
> 
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list