Serial Port Datareceived

Serial Port Datareceived 4,0/5 4125 reviews

Fortnite for mac computer. I try to port project from.NET to mono, and have a problem: SerialPort.DataReceived event doesn't handle SerialDataReceivedEventHandler procedure at all. I try mono 2.10.5 Linux, 2.10.6 Windows with same result. But on.NET 4.0 Windows my code works.

Hi, I have a weighing machine which is connected to a Serial Port. I need to read the weight from the weighing machine and insert it into database. Below is how the user use my system 1.User press Print Weight button and then the weight is captured into system.

Problem The problem i face now is my SerialPort.DataReceived is fired twice and i do not know how to capture the complete weight then only insert it into database. For example Weight 25.89kg will be sent as 2 packets.

For the first time, SerialPort.DataReceived event will receive '25.' And the second time, SerialPort.DataReceived event receive '89'. Both the read when concetenate together will then become the actual weight. Below is my code Dim comPort As New SerialPort() Private Sub FRMWeight_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try comPort.PortName = 'COM1' comPort.Handshake = Handshake.XOnXOff comPort.BaudRate = 9600 comPort.Parity = Parity.Even comPort.DataBits = 1 comPort.StopBits = 1 AddHandler comPort.DataReceived, AddressOf DataReceviedHandler comPort.Open() Catch ex As Exception mlogger.Error(ex) MessageBox.Show(ex.Message, 'Error HERE!!'

, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub SerialPort_DataRecevied(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Try Dim sp As SerialPort = CType(sender, SerialPort) Dim strWeight As String = sp.ReadExisting() txtWeight.Text = Trim(strWeight) If Trim(txtWeight.Text) ' Then InsertIntoDatabase(txtWeight.Text) End If Catch ex As Exception mlogger.Error(ex) MessageBox.Show(ex.Message, 'Error HERE!!' , MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub My first thinking on this is i should wait until all the data has been finished received then only insert into database. Video editing mac software. But where can i write my code on this because SerialPort.DataReceived is called by AddHandler.Does anyone has idea on this? HELP Please!! HI JohnWein,Bob Bao and jcddcjjcd John, I understand your logic but find no solution to do it.

I have try below code to but it is still not working. Do you have any idea? Private Sub DataReceviedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Try Dim sp As SerialPort = CType(sender, SerialPort) Dim strWeight As String = sp.ReadExisting() While sp.BytesToRead > 0 strWeight = strWeight & sp.ReadExisting txtWeight.Text = strWeight End While txtWeight.Text = Trim(strWeight) Catch ex As Exception mlogger.Error(ex) MessageBox.Show(ex.Message, 'Error HERE!!' , MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub. HI, I understand that this event is fired twice because there is more data to be received. My question is, is there any possible way to read all the data received then should i insert the data into database. Below is my code: Private Sub FRMWeight_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try comPort.PortName = 'COM1' comPort.Handshake = Handshake.XOnXOff comPort.BaudRate = 9600 comPort.Parity = Parity.Even comPort.DataBits = 1 comPort.StopBits = 1 AddHandler comPort.DataReceived, AddressOf DataReceviedHandler comPort.Open() Catch ex As Exception mlogger.Error(ex) MessageBox.Show(ex.Message, 'Error HERE!!'

, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub SerialPort_DataRecevied(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Try Dim sp As SerialPort = CType(sender, SerialPort) Dim strWeight As String = sp.ReadExisting() While sp.BytesToRead > 0 strWeight = strWeight & sp.ReadExisting txtWeight.Text = strWeight InsertIntoDatabase(txtWeight.Text) End While Catch ex As Exception mlogger.Error(ex) MessageBox.Show(ex.Message, 'Error HERE!!' , MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub The problem i face now is i insert the data twice into database because this event is fired twice. I have try with the code: While sp.BytesToRead > 0 strWeight = strWeight & sp.ReadExisting txtWeight.Text = strWeight End While but it is still not working, the event is still fired twice and i am still able to receive the part 1 data only.

Related Post