 |
|
|
Topic: The problem with receiving data with comm32
|
By: Mihael | Posted on: May 1 2011 at 11:58:23 AM | I wrote a program in Visaul Basic 6 using Comm32 trial to read data from serial port. But in reading error occurs, namely the program I received a series of prints like this:
01.
05.2011; 12:50:25, 23.8, 23.6; ~
01.05.2011; 12:
51:25, 23.6, 23.4; ~
01.05.2011, 12:52:25, 23.7, 2
3.4; ~
If I use a program hercules from hw-group.com me program prints as it should, therefore:
01.05.2011, 12:37:25, 23.9, 23.9; ~
01.05.2011, 12:38:25, 24.0, 24.0; ~
01.05.2011, 12:39:25, 23.8, 23.8; ~
01.05.2011, 12:40:25, 23.9, 23.7, ~
01.05.2011, 12:41:25, 24.1, 24.0; ~
Code, how I have configured the serial port.
Function OpenSerialPort ()
As String Dim SelectParity
IF Parity.ListIndex = 0 Then 'none
SelectParity = "n"
ElseIf Parity.ListIndex = 1 Then 'sec
SelectParity = "o"
ElseIf Parity.ListIndex = 2 Then 'Even
SelectParity = "e"
ElseIf Parity.ListIndex = 3 Then 'mark
SelectParity = "m"
ElseIf Parity.ListIndex = 4 Then 'space
SelectParity = 's'
End If
With Comm1
If. PortOpen Then. PortOpen = False
. CommPort SerialPort.ListIndex + 1 =
. InBufferSize = 1024
. Settings = Speed.Text & ""& SelectParity & ""& Wordlen.Text & ""& Stopbits.Text "" 19200, n, 8.1 "
. Handshaking = Handshaking.ListIndex
'set the DRT and RTS flags
. DTREnable = False
. RTSEnable = False
'enable the oncomm event for Every character reveived
. RThreshold = 1
'disable the event for oncomm send characters
. SThreshold = 0
. PortOpen = True
End With 'Comm1
End Function
Thanks for the help | |
By: Support | Posted on: May 1 2011 at 05:15:19 PM | Could you show the code of your OnComm receive event.
If you don't want to show it on the forum then you can email it
support at comm32.com
| |
By: Mihael | Posted on: May 1 2011 at 08:18:48 PM | No problem, show it is not a secret: D
Select Case Comm1.CommEvent
Case comEvReceive
txtUDPData.Text = ""
'Now This Is Quite Useful. Any data arriving at the com
'Port Will Be placed Into the textbox.
By whil Comm1.InBufferCount> 0
txtUDPData.Text = txtUDPData.Text & Comm1.Input
Loop
'Comm1.InBufferCount = 0
SendDataUDP
txtUDPData.Text = ""
Case Is> 1000
"The property always returns CommEvent a numerical value.
"Whenever the CommEvent property returns a number
"Above 1000 then you know That an error occurred.
txtUDPData.Text = "Some Error occurred ComPort"
Case Else
'What happened? It wasn't the Arrival of data - and it wasn't
'an error. See the 'CommEvent property for a full listing
'of all the events and errors.
End Select
SendDataUDP is a feature in which only continue to send data using UDP protocol. But the problem is already at or comm32. receiving data from the serial. | |
By: Support | Posted on: May 2 2011 at 10:11:56 AM | Continued the support case via email - the resolution is shown below | |
By: Support | Posted on: May 3 2011 at 04:27:54 PM | Mihael's problem was that he was expecting the oncomm event to occur at the end of every line of RS232 data. Of course that isn't the case because if you set rThreshold = 1 then you're going to get OnComm events as soon as one character arrives. Much better to buffer the data locally untill you have a whole line.
This code fixed Mihael's problem.
'// at the very top of the program declare this global variable
'// we use this as a buffer to store data outside of the OnComm event
Dim rxBuff As String
'// now here is the OnComm event.
Private Sub Comm1_OnComm()
'// in the oncomm event do this.
Case comEvReceive
Do While Comm1.InBufferCount > 0
rxBuff = rxBuff & Comm1.Input
'// In Mihael's RS232 data the "~" symbol indicates end of line
If InStr(rxBuff, "~") > 0 Then
txtUdpData = rxBuff
'// We have a whole line. Now Do something with it
'// and clear the buffer ready for the next line
rxBuff = ""
Else
'// did not receive "~" yet so the data will
'// stay in rxBuff for next event.
End If
Loop
End Sub
| |
|