Quantcast

Serial I/O with byte data

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Serial I/O with byte data

alexchernoff
Dear all,

I am receiving data over a serial port which is sent by a byte[].write function (an array of bytes)

I get the SerialPort1_read() event raised where I try to read data, RxBuffer is a string var

Try Read #SerialPort1, RxBuffer, Lof(SerialPort1)

as soon as I do, the whole program stops, some other timers in other modules also stop to fire. In fact when running the program in Gambas again, it does not execute even the startup, so have to restart the IDE.

what am I doing wrong?

thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

Mike Crean
Hi Alex, have a look at this code for some ideas how I have serial comms working.
' Gambas class file

Private Sport As SerialPort
Public Pp As Byte
Public Po As Byte
Public Rxs As String

Public Sub Form_Open()
  Me.Load
  Me.Center
End

Public Sub Button2_Click()
  Sport = New SerialPort As "Sport"
  Po = 1
  
  If TextBox3.Text = "N" Or TextBox3.Text = "n" Then
    pp = 0
  End If
  
  With Sport
    .PortName = TextBox1.Text
    .Speed = TextBox2.Text
    .Parity = Pp
    .DataBits = TextBox4.Text
    .StopBits = TextBox5.Text
    .FlowControl = Val(TextBox6.text)
  Try
    .Open()
    Label9.Text = TextBox1.Text & ":" & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & "  OPEN"
    Label10.Background = &H00FF00&  
    If Error Then
      Message(" Port " & TextBox1.Text & " not found -- TERMINATING PROGRAM")
      Quit
    Endif
  End With

End

Public Sub Sport_Read()
  Dim Rx As String
  Read #Sport, Rx, Lof(Sport)
  Rxs = Rxs & Rx
  If Rx = Chr$(13) Then
    TextArea1.Text = TextArea1.Text & Rxs
    Rxs = ""
  End If
End

Public Sub Button1_Click()
  If Po = 0 Then
    Message("Open the PORT")
  Else
    Print #Sport, Textbox7.Text; Chr$(13)
  End If
End

Public Sub Button3_Click()
  Sport.Close
  Label10.Background = &HFF0000&
  Label9.Text = "CLOSED"
  Po = 0
End

Public Sub Button4_Click()
  TextArea1.Clear
End

Public Sub Button5_Click()
  EndIt()  
End

Public Sub Form_Close()
  EndIt()
End

Public Sub EndIt()
  Try
    Sport.Close
    Quit
  If Error Then
    Quit  
  Endif
End


RegardsMike
 

    On Tuesday, 9 May 2017, 0:12, alexchernoff <[hidden email]> wrote:
 

 Dear all,

I am receiving data over a serial port which is sent by a byte[].write
function (an array of bytes)

I get the SerialPort1_read() event raised where I try to read data, RxBuffer
is a string var

*Try Read #SerialPort1, RxBuffer, Lof(SerialPort1)*

as soon as I do, the whole program stops, some other timers in other modules
also stop to fire. In fact when running the program in Gambas again, it does
not execute even the startup, so have to restart the IDE.

what am I doing wrong?

thanks!




--
View this message in context: http://gambas.8142.n7.nabble.com/Serial-I-O-with-byte-data-tp58933.html
Sent from the gambas-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user

CommsLayout800-300x212.jpg (21K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

alexchernoff
Thanks Mike,

what I have is pretty much the same, it's this line that causes everything to stop.

Read #Sport, Rx, Lof(Sport)

A very similar program written in Xojo on the same system using same serial port works just fine. In Gambas, things just stop executing as soon as I do a Read on that port...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

nando-7
I have used Gambas serial port for 12 years now without problems.

If there is nothing in the serial port queue, then that line might block.

I use this:

. . .
L=Lof(Sport)                     'how much is there now?
if L>1 then L=L-1                'leave at least one byte for next time around.
if L>0 then Read #Sport, Rx, L   'get all less one byte
Rx_all = Rx_all + Rx             'combine this piece into the whole receive string.
Rx=""                            'just in case it loops around, don't re-read Rx.
. . .


Most importantly, please do not think that just one READ will
retrieve a whole line or chunk of data from your serial port.
It will get only part of it because your computer reads bytes much faster
and empties the receiver buffer much faster than data send over RS232.

-Fernando

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


---------- Original Message -----------
From: alexchernoff <[hidden email]>
To: [hidden email]
Sent: Mon, 8 May 2017 22:45:11 -0700 (MST)
Subject: Re: [Gambas-user] Serial I/O with byte data

> Thanks Mike,
>
> what I have is pretty much the same, it's this line that causes everything
> to stop.
>
> *Read #Sport, Rx, Lof(Sport) *
>
> A very similar program written in Xojo on the same system using same serial
> port works just fine. In Gambas, things just stop executing as soon as I do
> a Read on that port...
>
> --
> View this message in context: http://gambas.8142.n7.nabble.com/Serial-I-O-with-
> byte-data-tp58933p58939.html Sent from the gambas-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

T Lee Davidson
In reply to this post by alexchernoff
On 05/08/2017 12:13 PM, alexchernoff wrote:

> Dear all,
>
> I am receiving data over a serial port which is sent by a byte[].write
> function (an array of bytes)
>
> I get the SerialPort1_read() event raised where I try to read data, RxBuffer
> is a string var
>
> *Try Read #SerialPort1, RxBuffer, Lof(SerialPort1)*
>
> as soon as I do, the whole program stops, some other timers in other modules
> also stop to fire. In fact when running the program in Gambas again, it does
> not execute even the startup, so have to restart the IDE.
>
> what am I doing wrong?
>
> thanks!

If you are attempting to read the data _into_ RxBuffer, it appears your syntax is incorrect.

According to http://gambaswiki.org/wiki/lang/read , the syntax for the READ statement is:
Variable = READ [ # Stream , ] Length

Perhaps try:
Try RxBuffer = Read #SerialPort1, Lof(SerialPort1)


---
Lee


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

alexchernoff
Good day all,

Anyone know how to read data received on serial port into an array of bytes instead of string?  String gets unreadable binary characters, so how can I  put them into byte[] or so?

Thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

Tobias Boege-2
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

Alexie-2
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 <[hidden email]>:

> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

Mike Crean
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 <[hidden email]> 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 <[hidden email]>:

> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

nando-7
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 <[hidden email]>
To: mailing list for gambas users <[hidden email]>
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 <[hidden email]> 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 <[hidden email]>:
>
> > 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
> > [hidden email]
> > 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
> [hidden email]
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

alexchernoff
In reply to this post by Alexie-2
Thanks, it works (both with .tostring and reading one by one...

But I get values I don't know how to convert, e.g. where I expect a decimal 83 or hex 53 I get byte value of 253 ... Or instead of Decimal 15 I get 225 :(

cheers!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

alexchernoff
Hmm eureka, it's 02 53 = 0x53
Or 02 25 = 0x25...

Where could that 02 be from?

Cheers!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

zxMarce
&H02 is ASCII for STX, a special char that for some protocols mean "start transmission".
Some protocols frame their data payload between chars STX (&H02) and ETX(&H03).
These also normally use some checksum mechanism either before or right after ETX.

Of course it could also be a misleading UTF8 header of some kind resulting from raw data to string conversion, but that I cannot tell for sure.

You need a good protocol manual and check low-level comms frames.

Regards,
zxMarce.

On May 15, 2017, 10:42, at 10:42, alexchernoff <[hidden email]> wrote:

>Hmm eureka, it's 02 53 = 0x53
>Or 02 25 = 0x25...
>
>Where could that 02 be from?
>
>Cheers!
>
>
>
>--
>View this message in context:
>http://gambas.8142.n7.nabble.com/Serial-I-O-with-byte-data-tp58933p58986.html
>Sent from the gambas-user mailing list archive at Nabble.com.
>
>------------------------------------------------------------------------------
>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
>[hidden email]
>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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

zxMarce
In reply to this post by alexchernoff
Alex,

Without knowing the protocol/device you're communicating with, I don't
think anyone will be able to give substantial help.
But, after years of interfacing very diverse hardware via RS-232 (fiscal
printers, cash-hanlding hardware, GPS, you name it!), I can tell you
that it's not always easy, and docs sometimes lack info or are plain wrong.
Unfortunately the bulk of my experience with hardware is in VB6...
Maybe you're looking at some kind of protocol wrapper bytes... Can't
really say without the detail.

On 15/05/17 08:52, alexchernoff wrote:
> Thanks, it works (both with .tostring and reading one by one...
> But I get values I don't know how to convert, e.g. where I expect a
> decimal 83 or hex 53 I get byte value of 253 ... Or instead of Decimal
> 15 I get 225 :(
> cheers!


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

alexchernoff
it works fine - it was a strange serial port hardware issue. All the "Read" methods suggested above work, Thanks to all!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Serial I/O with byte data

Mike Crean
In reply to this post by zxMarce
Hi, as I said before most likely Modbus RTU or similar protocol. If so you will need to do some BIT bashing and probably
some CRC conversion. You will need to know the frame size among other things.

Public Sub HbLb()
  Dim Xz2 As Long
  Hb = Fix(DataS / 256)
  Xz2 = Fix(256 * Hb)
  Lb = DataS - Xz2
End
RegardsMike
 

    On Monday, 15 May 2017, 23:29, ML <[hidden email]> wrote:
 

 Alex,

Without knowing the protocol/device you're communicating with, I don't
think anyone will be able to give substantial help.
But, after years of interfacing very diverse hardware via RS-232 (fiscal
printers, cash-hanlding hardware, GPS, you name it!), I can tell you
that it's not always easy, and docs sometimes lack info or are plain wrong.
Unfortunately the bulk of my experience with hardware is in VB6...
Maybe you're looking at some kind of protocol wrapper bytes... Can't
really say without the detail.

On 15/05/17 08:52, alexchernoff wrote:
> Thanks, it works (both with .tostring and reading one by one...
> But I get values I don't know how to convert, e.g. where I expect a
> decimal 83 or hex 53 I get byte value of 253 ... Or instead of Decimal
> 15 I get 225 :(
> cheers!


------------------------------------------------------------------------------
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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Loading...