I'm trying to receive serial data from Arduino asynchronously via "DataReceived" event, but it $event.messageData is always empty.
Here's my code so far:
$port = New-Object System.IO.Ports.SerialPort COM7,9600,None,8,one
$port.Open()
$port | Format-List -Property *
Register-ObjectEvent -InputObject $port -EventName "DataReceived" -Action {
$event | Format-List -Property * | Out-Host
}
Here's the output:
PORT
BaseStream : System.IO.Ports.SerialStream
BaudRate : 9600
BreakState : False
BytesToWrite : 0
BytesToRead : 0
CDHolding : False
CtsHolding : False
DataBits : 8
DiscardNull : False
DsrHolding : False
DtrEnable : False
Encoding : System.Text.ASCIIEncoding
Handshake : None
IsOpen : True
NewLine :
Parity : None
ParityReplace : 63
PortName : COM7
ReadBufferSize : 4096
ReadTimeout : -1
ReceivedBytesThreshold : 1
RtsEnable : False
StopBits : One
WriteBufferSize : 2048
WriteTimeout : -1
Site :
Container :
EVENT
ComputerName :
RunspaceId : 67c40d50-5a9b-4022-974e-0a6a6518c2af
EventIdentifier : 11
Sender : System.IO.Ports.SerialPort
SourceEventArgs : System.IO.Ports.SerialDataReceivedEventArgs
SourceArgs : {COM7, System.IO.Ports.SerialDataReceivedEventArgs}
SourceIdentifier : 26c24bdb-8085-4e88-828e-44cb7897e416
TimeGenerated : 6/16/2019 12:52:39 AM
MessageData :
On the arduino side the code is:
Serial.printlf("ready")
When I use syncronous approach everything works
$port = New-Object System.IO.Ports.SerialPort COM7,9600,None,8,one
$port.Open()
$port.ReadLine()
Output
ready
ready
ready
...
Please, help!
$event.MessageData
is not the right property to query - it contains custom data passed to the -MessageData
parameter when raising a custom event via the New-Event
cmdlet.
For .NET-originated events, it is generally the automatic $EventArgs
variable that contains the event-specific data.
In the specific case of the [System.IO.Ports.SerialPort]
type's DataReceived
event, however, it seems that the data isn't actually passed as an event argument, and you must instead use the $Sender
object to gain access to the originating [System.IO.Ports.SerialPort]
instance in order to call its .ReadExisting()
method, judging by the sample code in the official docs:
$eventJob =
Register-ObjectEvent -InputObject $port -EventName "DataReceived" -Action {
$Sender.ReadExisting() | Out-Host
}