• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

Await event to complete before continuing code

W

WojszaM

Guest
Hi, I have a question. I have a method that sends and receives messengers via serial port as a byte arrays,

to SerialPort DataRecived I am adding an event that will read response and assign it to type

so basically it should be easy and looks like this:

async {

let buffer = Array.zeroCreate<byte> port.ReadBufferSize
let response = new Response(buffer)

let data_recived =
async{
let! read_buffer = port.AsyncReadLineAsByte()
response.Data <- read_buffer

printfn "Response Buffer: %A response type: %A" read_buffer response.Data
}
|> Async.RunSynchronously

port.DataReceived.Add(fun _-> data_recived)

do! port.AsyncWriteLineAsByte messange

//Something to wait for fired event

return response.Data
}

And type looks like

type Response(buffer: byte[]) =

let mutable _response = buffer

member this.Data
with get() = _response
and set(value) = _response <- value


Between sending messenge and returning byte array i need something that will wait for event to fire and finish so far i tried:

let! args = port.DataReceived |> Async.RunSynchronously

But this get stuck, also i tried:

while response.Data = buffer
do printfn "Response: %A \r Buffer: %A" response.Data buffer

Well this works one time, if I send second message it goes to endless loop like this while is fire before event is fired.

Is there any way for this to work?

Continue reading...
 
Top Bottom