Receiving serial data - gb.net

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
PeteMarsh
Posts: 13
Joined: Thursday 22nd February 2024 8:01pm

Receiving serial data - gb.net

Post by PeteMarsh »

I have been able to set up a serial port, set the baud rate etc, use send to output data, and verified on oscilloscope. All very simple in the end. I’m now looking to read a reply. With gb.net enabled I can see there is an event called read but examples of its use don’t seem to be event driven :

'******** Incoming serial data ********
PUBLIC SUB SComm _Read()
SLEEP 0.025
TRY READ #SComm , Rx, Lof(SComm )
IF ERROR THEN
Message.info(No received data!
End if

I’m used to the oncomm event in vb which is triggered when a serial port receives data - having to poll the port on the off chance that data arrives doesn’t seem right - does anyone know if there is an equivalent of oncomm? - thanks in anticipation.
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Receiving serial data - gb.net

Post by BruceSteers »

Looks like serial port inherits Stream so usual Stream methods should work.

The event should require that the object is attached either when creating or after.

hSerialPort = New SerialPort ( ) As "SComm"


or ..
hSerialPort = New SerialPort ( )
Object.Attach(hSerialPort, Me, "SComm")

then your SComm_Read() event should work.


Please post some example code or a small application showing the problem if you still need help
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Receiving serial data - gb.net

Post by vuott »

PeteMarsh wrote: Wednesday 28th February 2024 2:01amI’m used to the oncomm event in vb which is triggered when a serial port receives data
Uhmmmm...that is, are you looking for an Event that will be raised only when the serial port receives data ?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Receiving serial data - gb.net

Post by BruceSteers »

What is this line... ?
TRY READ #SComm , Rx, Lof(SComm )
If Rx is your String I think should be..

Rx = READ #SComm , Lof(SComm)



Public Sub SComm _Read()

  Dim Rx as String

  Rx = Read #SComm , Lof(SComm)
  If not Trim(Rx) Then 
    Message.info("No received data!")
    Return
  Endif 
  
' Do whatever with Rx data
  ProcessCommString(Rx)

End

If at first you don't succeed , try doing something differently.
BruceS
Post Reply