Serial port - Send data while continuously reading data from serial port

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas I have application that continuously reads data from serial port.( 3 characters after 80 milliseconds) the code is given below.
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas There are different buttons. By clicking user can send data.
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas My problem is when I click button to send data to microcontroller sometimes there is nothing happens. I have to click that button 2 – 3 times & then data can be sent.
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas How can I send data intermittently when I continuously reads data from serial port?<span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue ’’’’’’’’’’’’’’ code ‘’’’’’’’’
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue Dim<span style="font-size:9.5pt; font-family:Consolas newdata
<span style="color:blue As <span style="color:blue String = <span style="color:#A31515
""
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:blue Private<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue Sub SerialPort1_DataReceived(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As<span>
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
System.IO.Ports.<span style="color:#2B91AF SerialDataReceivedEventArgs)
<span style="color:blue Handles
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none

<span style="font-size:9.5pt; font-family:Consolas SerialPort1.DataReceived
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
newdata = <span style="color:#A31515 ""
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Dim newchr <span style="color:blue As <span style="color:blue
Char = <span style="color:#A31515 ""
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Try
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
newchr = ChrW(SerialPort1.ReadChar)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span> newdata = newchr
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green there are two types of data one three character start with "F" & another on<span>

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas; color:green <span>
character "c"
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue If newchr = <span style="color:#A31515 "F"
<span style="color:blue Then
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue For i <span style="color:blue As <span style="color:blue
Integer = 1 <span style="color:blue To 3
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
newchr = ChrW(SerialPort1.ReadChar)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
newdata = newdata & newchr
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Next
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green Me.Invoke(New MethodInvoker(AddressOf doupdates))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.Invoke(<span style="color:blue New
<span style="color:#2B91AF EventHandler(<span style="color:blue AddressOf doupdates))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue ElseIf newchr = <span style="color:#A31515
"C" <span style="color:blue Then
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:green Me.Invoke(New MethodInvoker(AddressOf doupdates))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Me.Invoke(<span style="color:blue New
<span style="color:#2B91AF EventHandler(<span style="color:blue AddressOf doupdates))
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue If
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Catch ex <span style="color:blue As
<span style="color:#2B91AF Exception
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
MsgBox(<span style="color:#A31515 "read " & ex.Message)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Finally
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
SerialPort1.Close()
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Try
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Sub
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue Private <span style="color:blue Sub doupdates()
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
Acknowledgement(Trim(newdata))<span> <span style="color:green procedure to ger MSG & processed

<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
Sleep(0)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
lblmsg.Text = MSG
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
lblProccessed.Text = <span style="color:#A31515 "Processed : " &
<span style="color:blue CStr(Processed)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
newdata = <span style="color:#A31515 ""
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
Sleep(0)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Sub
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span> <span style="font-size:9.5pt; font-family:Consolas; color:blue Private<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue Sub SendData()
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
If SerialPort1.close() then SerialPort1.open
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
SerialPort1.write(Mydata)<span>
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Sub
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span> <span style="font-size:9.5pt; font-family:Consolas; color:blue Public<span style="font-size:9.5pt; font-family:Consolas
<span style="color:blue Sub SendData(<span style="color:blue ByVal DataToSend
<span style="color:blue As <span style="color:blue String)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue If SerialPort1.IsOpen = <span style="color:blue
False <span style="color:blue Then
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
SerialPort1.Open()
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue If
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
SerialPort1.WriteLine(DataToSend)
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-size:9.5pt; font-family:Consolas <span>
<span style="color:blue End <span style="color:blue Sub

<br/>

<br/>

View the full article
 
Back
Top