yugant
asked on
MSComm (VB6) unable to convert data.. please help!
Hi Everyone!
Im doing a project for my Uni which requires me to connect a simple monitoring unit with a computer. The incoming "streaming" data to the rs232 is of the following format:
'S' A1H A1L A2H A2L....... A5L Dig_IPS, chksum etc
Using the mscomms code provided by microsoft as shown below, I get jibberish (unreadble characters) is there anmy way i can convert them to binary or hex so i can actually read them?:
Option Explicit
Private Sub Form_Load()
Form1.Caption = "Incoming Data From PIMEX Unit"
With MSComm1
'Comm Port Used
.CommPort = 1
'Handshaking Settings
.Handshaking = 0
'This procedure sets the RThreshold property, which determines
'how many bytes can arrive at the receive buffer before the OnComm
'event is triggered and the CommEvent property is set to comEvReceive.
.RThreshold = 1
'Determines whether to enable the Request To Send line.
.RTSEnable = True
'Sets/returns the baud rate, parity, data bit, and stop bit parameters.
.Settings = "4800,n,8,1"
'Sets/returns the minimum number of characters allowable in the transmit.
.SThreshold = 1
'Sets/returns the state of the communications port (open or closed).
.PortOpen = True
'Leave all other settings as default values.
End With
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
Dim InBuff As String
Select Case MSComm1.CommEvent
' Handle each event or error by placing
' code below each case statement.
' This template is found in the Example
' section of the OnComm event Help topic
' in VB Help.
' Errors
Case comEventBreak ' A Break was received.
Case comEventCDTO ' CD (RLSD) Timeout.
Case comEventCTSTO ' CTS Timeout.
Case comEventDSRTO ' DSR Timeout.
Case comEventFrame ' Framing Error.
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input
Call HandleInput(InBuff)
Case comEvSend ' There are SThreshold number of
' characters in the transmit buffer.
Case comEvEOF ' An EOF character was found in the
' input stream.
End Select
End Sub
Sub HandleInput(InBuff As String)
' This is where you will process your input. This
' includes trapping characters, parsing strings,
' separating data fields, etc. For this case, you
' are simply going to display the data in the TextBox.
Text1.SelStart = Len(Text1.Text)
Text1.SelText = InBuff
' If (Text1 = "s") Then
' Text2.Text = "s"
' End If
End Sub
Im doing a project for my Uni which requires me to connect a simple monitoring unit with a computer. The incoming "streaming" data to the rs232 is of the following format:
'S' A1H A1L A2H A2L....... A5L Dig_IPS, chksum etc
Using the mscomms code provided by microsoft as shown below, I get jibberish (unreadble characters) is there anmy way i can convert them to binary or hex so i can actually read them?:
Option Explicit
Private Sub Form_Load()
Form1.Caption = "Incoming Data From PIMEX Unit"
With MSComm1
'Comm Port Used
.CommPort = 1
'Handshaking Settings
.Handshaking = 0
'This procedure sets the RThreshold property, which determines
'how many bytes can arrive at the receive buffer before the OnComm
'event is triggered and the CommEvent property is set to comEvReceive.
.RThreshold = 1
'Determines whether to enable the Request To Send line.
.RTSEnable = True
'Sets/returns the baud rate, parity, data bit, and stop bit parameters.
.Settings = "4800,n,8,1"
'Sets/returns the minimum number of characters allowable in the transmit.
.SThreshold = 1
'Sets/returns the state of the communications port (open or closed).
.PortOpen = True
'Leave all other settings as default values.
End With
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
Dim InBuff As String
Select Case MSComm1.CommEvent
' Handle each event or error by placing
' code below each case statement.
' This template is found in the Example
' section of the OnComm event Help topic
' in VB Help.
' Errors
Case comEventBreak ' A Break was received.
Case comEventCDTO ' CD (RLSD) Timeout.
Case comEventCTSTO ' CTS Timeout.
Case comEventDSRTO ' DSR Timeout.
Case comEventFrame ' Framing Error.
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input
Call HandleInput(InBuff)
Case comEvSend ' There are SThreshold number of
' characters in the transmit buffer.
Case comEvEOF ' An EOF character was found in the
' input stream.
End Select
End Sub
Sub HandleInput(InBuff As String)
' This is where you will process your input. This
' includes trapping characters, parsing strings,
' separating data fields, etc. For this case, you
' are simply going to display the data in the TextBox.
Text1.SelStart = Len(Text1.Text)
Text1.SelText = InBuff
' If (Text1 = "s") Then
' Text2.Text = "s"
' End If
End Sub
I think you can also check the COM PORT settigs once again eg. Baud rate, stop bit etc.
yugant:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
Experts: Post your closing recommendations! Who deserves points here?
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
Experts: Post your closing recommendations! Who deserves points here?
yugant, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:
Save as PAQ -- No Refund.
DanRollins -- EE database cleanup volunteer
Moderator, my recommended disposition is:
Save as PAQ -- No Refund.
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the event handler,
Before opening the port
you need to this property along with the
others
...
MSComm1.InputMode = comInputModeBinary
...
Private Sub MSComm1_OnComm()
Dim inbuff as variant
dim mybytearray() as byte
Select Case MSComm1.CommEvent
...
...
...
Case comEvReceive ' Received RThreshold # of chars.
InBuff = MSComm1.Input
ReDim mybytearray(MSComm1.InBuff
'print the array
for i = 0 to MSComm1.InBufferCount - 1
debug.print Hex(mybytearray(i))
next
...
end select
end sub
Hope this helps