[Gambas-user] using events of serial ports for reading how_?

T Lee Davidson t.lee.davidson at gmail.com
Mon Aug 6 19:40:42 CEST 2018


You will have problems if you try to utilize both an event triggered serial port read and a manual read.

The SerialPort.Read event is triggered when there is data available to be read. If you read that data in the event handler, the
port is cleared and there will be no data available to a manual read.

If you want to manually read the port, then you should not read the data from it in the SerialPort.Read event handler. Perhaps
you do not even need to implement that event handler.

I could be incorrect since I haven't verified this. But, it seems logical to me that Stream.Blocking would be False by default
otherwise an attempt to read the port would hang the application until data is available and that should not be a default behavior.

I can think of two ways to implement manual reading.

1. Do not implement a SerialPort.Read event handler, ensure that Stream.Blocking = False, and simply read the port manually
understanding that there may be no data available. In this case READ would appear to do nothing, but the application would not hang.

2. Use the SerialPort.Read event handler to set a flag indicating that data is available and read that flag in your Sub rp()
function. For example:

Private PortDataAvailable As Boolean

Public Sub SComm_Read()
	PortDataAvailable = True
End

Public Sub rp()
	If myDataAvailableBoolean then
		' Do the port reading stuff
		' Read port blah blah blah
		' All data has been read
		PortDataAvailable = False
	Else
		Print "No data available"
		Return
	Endif
End


___
Lee


On 08/06/2018 12:05 PM, PICCORO McKAY Lenz wrote:
> OK hans, and tobias, yes the code in my first mail last week was bad,
> now i understant some problems when i read agains the printert
> but i need and i want a manual read funtion "also": so i have now:
> 
> the read event for the port, as documents suggest "is triggered"
> when data "can be read" so if there's some data to read or available
> the event will be executed by itselft, but i want also a manual "rp" method
> that read also the port manually, at this point i dont know
> if will be sync or async, i have a "sale" class that will be polled into a form,
> but as Tobias said i used the Scom_Read
> but still need a manual read funtion due the inherits so i have :
> 
> 1) the read event:
> 
> ' this event will be triggered in read operations by the stream when data its available
> Public Sub SComm_Read()
>    dim buffers as string = ''
>   Read #SComm, buffers, Lof(SComm)
>    dataread &= buffers
> End
> 
> 2) here, how can i code a read manual operation do not conflict if there's already an operation triggered by teh read event?
> 
> i used some code proposed by Tobias but:
> 
> '' generic read port buffer data
> Public Sub rp()
>    dim  start as date = Now
>    dim buffers as string = ''
>   Do
>     Wait 0.1
>     Read #SComm, buffers, Lof(SComm)
>     dataread &= buffers
>   Loop Until DateDiff(start, Now, gb.Millisecond) > 500
> End
> 
> now i have the problem of locking, as Tobias said I not even sure how Read
> behaves when Lof is zero or if the read are just triggered at same time the rp was invoqued!
> so then reading would block.
> 
> i dont know how to use the SComm.Blocking = False to make manual polling,
> 
> 
> Lenz McKAY Gerardo (PICCORO)
> http://qgqlochekone.blogspot.com
> 
> 
> El lun., 6 de ago. de 2018 a la(s) 09:59, PICCORO McKAY Lenz (mckaygerhard at gmail.com <mailto:mckaygerhard at gmail.com>) escribió:
> 
>     El lun., 6 de ago. de 2018 a la(s) 09:52, Hans Lehmann (hans at gambas-buch.de <mailto:hans at gambas-buch.de>) escribió:
> 
>         have a look at the chapters and the projects... under https://www.gambas-buch.de/doku.php?id=k24:k24.1:k24.1.5:start!
> 
>     THANKS HANS! now reading...
>      isend this mail to the list.. maybe you send too quickly and forgett to do!
> 
> 
> 
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 


More information about the User mailing list