-
Nov 5th, 2008, 01:37 AM
#1
Thread Starter
Lively Member
[RESOLVED] Mscomm input
I have a board that outputs a 8bit binary value (0-255) what do i need to put into my program to have it read the input and put it in a text box?
I have this:
Private Sub MSComm1_OnComm()
Dim strData As String
Select Case MSComm1.CommEvent
Case comEvReceive
strData = MSComm1.Input
Text1.Text = strData
End Select
End Sub
But would i need to adjust the mscomm settings?
-
Nov 5th, 2008, 02:23 AM
#2
Re: Mscomm input
depends on the setting of the board, if in doubt set them so they match the setting of the board
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 5th, 2008, 09:07 AM
#3
Thread Starter
Lively Member
Re: Mscomm input
what about the rthreshold, inputlen, inputmode settings? and what should i expect to see in the textbox? i would like a number 0-255, but all i can get so far is a symbol or a letter once in a while?
-
Nov 5th, 2008, 03:32 PM
#4
Re: Mscomm input
you also need the settings for parity, number of bits etc (n,8,1 or whatever the correct settings are) and make sure you are working with the correct com port
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 5th, 2008, 06:36 PM
#5
Frenzied Member
Re: Mscomm input
Maybe something in this thread will point you in the right direction...
http://www.vbforums.com/showthread.p...68#post2124168...
-
Nov 7th, 2008, 01:03 AM
#6
Thread Starter
Lively Member
Re: Mscomm input
I have:
Private Sub MSComm1_OnComm()
Dim strData As String
Select Case MSComm1.CommEvent 'incoming data
Case comEvReceive
strData = MSComm1.Input
Text1.Text = strData
End Select
End Sub
-
Nov 7th, 2008, 01:07 AM
#7
Thread Starter
Lively Member
Re: Mscomm input
I have:
with 11500,n,8,1
rthresh=0 inputlength=0
cominputmode= binary
Private Sub MSComm1_OnComm()
Dim strData As String
Select Case MSComm1.CommEvent 'incoming data
Case comEvReceive
strData = MSComm1.Input
Text1.Text = strData
End Select
End Sub
but i get nothing in my text box?
if i use hyperterminal with the same 11500,n,8,1 i get stuff back?
-
Nov 7th, 2008, 02:17 AM
#8
Frenzied Member
Re: Mscomm input
Your speed setting should be 115,000 bps not 11500. Check for typo's in your code. Also set ...
.RThreshold = 1
.InputLen = 0
.RTSEnable = True ' If you are using hardware handshaking
.PortOpen = True ' It's obvious I know - but if you have missed it !!!!
-
Nov 7th, 2008, 09:35 PM
#9
Thread Starter
Lively Member
Re: Mscomm input
i had the spped right, i just missed the 0 in the post, i have tried all of those things
-
Nov 8th, 2008, 01:32 AM
#10
Re: Mscomm input
no one else has the same board, so it is not possible to test for you, i don't even have a com port on this computer
are you putting the string into the textbox before it has all arrived?
see this thread here http://www.vbforums.com/showthread.p...ghlight=mscomm and someother links from that thread
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 8th, 2008, 10:20 AM
#11
Frenzied Member
Re: Mscomm input
Do you have to poll your test board before it sends or is it streaming?
-
Nov 8th, 2008, 11:59 AM
#12
Thread Starter
Lively Member
Re: Mscomm input
I poll it by sending "VA" & vbcr , and that is to read input A voltage, i can send it trough hyperterminal and it responds fine
-
Nov 9th, 2008, 03:13 PM
#13
Frenzied Member
Re: Mscomm input
So are you sending the command
frmComms.MSComm1.Output = "VA" & vbCRLF
somewhere in your code, in say a 200msec loop, to initiate the comms from your target board?
-
Nov 9th, 2008, 04:09 PM
#14
Re: Mscomm input
Originally Posted by mystorchid
I poll it by sending "VA" & vbcr , and that is to read input A voltage, i can send it trough hyperterminal and it responds fine
What is the return (with Hyperterminal) when you send that?
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 9th, 2008, 05:05 PM
#15
Thread Starter
Lively Member
Re: Mscomm input
I send:
Private Sub Command2_Click()
MSComm1.Output = "VA" & vbCr
End Sub
and i get letter or character back in hyperterminal. the board is supposed to output a binary value between 0-255 (8bit) , do i have to write the code for vb to make the conversion from binary to ascii from the recieve buffer?
-
Nov 9th, 2008, 08:24 PM
#16
Re: Mscomm input
Originally Posted by mystorchid
I send:
Private Sub Command2_Click()
MSComm1.Output = "VA" & vbCr
End Sub
and i get letter or character back in hyperterminal. the board is supposed to output a binary value between 0-255 (8bit) , do i have to write the code for vb to make the conversion from binary to ascii from the recieve buffer?
Which is it? A binary value 0 to 255 or ASCII? Your reply implies both. MSComm can't receive both Binary (Byte data) and ASCII chars simultaneously. I can help you with either but I must know which.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 9th, 2008, 08:37 PM
#17
Thread Starter
Lively Member
Re: Mscomm input
the board sends a Binary 0-255 8bit responce to the pc,, I want ascii in my textbox.
-
Nov 10th, 2008, 12:22 AM
#18
Re: Mscomm input
Originally Posted by mystorchid
the board sends a Binary 0-255 8bit responce to the pc,, I want ascii in my textbox.
OK, we can work with that. Here are some MSComm rules to follow. If you want to Input Binary (Byte) data and display it as ASCII in a TextBox then do not use MSComm1.InputMode = comInputModeBinary. Binary mode is useful for displaying the Decimal value of the Byte data only. In Binary Mode, (which is only applicable to the Input property, not the Output property) embedded control characters are not stripped off the Input stream. All data Cr,Lf,EOF,tab etc are returned as their Decimal equivalent. Unfortunately setting Input Mode to binary doesn't return the decimal values on it's own. That data has to also be put in a Variant, passed to a Byte array and manipulated with conversion functions.
The code below should input your Byte data from your Microcontroller and display it in Text1 as ASCII characters.
Code:
Option Explicit
Private Sub Form_Load()
MSComm1.InputMode = comInputModeText ' This is default
End Sub
Private Sub MSComm1_OnComm()
Dim BytesReceived() As Byte ' Undemensioned (Dynamic) array
Dim InBuffer 'Variant
If MSComm1.CommEvent = comEvReceive Then
InBuffer = MSComm1.Input ' Pass the Input data to the InBuffer
BytesReceived() = InBuffer ' Pass the Byte data to the Byte array
Text1.Text = BytesReceived() ' Display ASCII in Text1
End If
End Sub
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 10th, 2008, 01:01 AM
#19
Thread Starter
Lively Member
Re: Mscomm input
I get a odd character back like a "y" with a comma above it, but not a number like I m looking for
-
Nov 10th, 2008, 02:44 AM
#20
Frenzied Member
Re: Mscomm input
Assuming that you get a single 0-255 binary value representing a voltage, to display the number in the text box you need to do ....
Text1.Text = Asc(BytesReceived(0))
As you should only get one character back you ignore anything else that is in the buffer.
-
Nov 10th, 2008, 08:56 AM
#21
Re: Mscomm input
Originally Posted by mystorchid
I get a odd character back like a "y" with a comma above it, but not a number like I m looking for
I asked you repeatedly whether you wanted to input this data as ASCII text or Decimal numbers. You finally said ASCII! Now you say you want numbers! If your microntroller happens to send a none printable control character then you will see funny looking ASCII like you're describing. What will be displayed will be subject to the Font that you're using. Control Char display (as ASCII)are subject to your font.
One more time... Do you want the input decoded and displayed as decimal numbers or don't you?
Last edited by CDRIVE; Oct 10th, 2010 at 11:42 AM.
Reason: Removal of obnoxious font
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 10th, 2008, 09:17 AM
#22
Re: Mscomm input
I wrote this demo that inputs data as Binary for someone else. It is intended to be used with a Loopback tester or a Nullmodem. If you use a Nullmodem then two MSComm Controls must be used and the Input port must be changed to something other than the Output port. You could also just extract the Input code alone and use it to test your controller. All MSComm properties are left at their default values. As is, this demo needs 1 TextBox, 2CommandButtons, 2 Labels, 1 CommControl and a Timer.
This demo converts the Byte values to its Decimal equivalent and displays it in Text1. Label1 shows you what's in the InBuffer and is updated every 500mS in the Timer event. Label2 displays CommEvents and when they fire. As you will see in the Else statement in the CommEvent procedure OnComm fires when ANY change at the port occurs subsequent to the PortOpen command. In this case the DTR line defaults to True in Properties. This is why it fires when the PortOpen command is executed. It isn't firing because the port was opened. It's up to the programmer to sift through what the events are. The only defined event that I wrote was the comEvEOF.
Code:
Option Explicit
Private Sub Form_Load()
MSComm1.InputMode = comInputModeBinary
MSComm1.EOFEnable = True
Command1.Caption = "Output Data"
Command2.Caption = "Input Data"
Label2.Caption = "CommEvents"
Text1.FontName = "Garamond" ' I chose this font because it's
Text1.FontBold = True ' easier to distinguish between
Text1.FontSize = 18 ' a 1 and an l (lower case L)
Timer1.Interval = 500
End Sub
Private Sub Command1_Click()
Dim BytesToSend(4) As Byte ' Dimensioned to 5 Bytes
Dim OutBuffer
BytesToSend(0) = &H49 ' = Char 'I' & Decimal '73'
BytesToSend(1) = &H23 ' = Char '#' & Decimal '35'
BytesToSend(2) = &H4A ' = Char 'J' & Decimal '74'
BytesToSend(3) = &H4B ' = Char 'K' & Decimal '75'
BytesToSend(4) = &H1A ' = EOF Char: Decimal '26'
' Bytes are outputted in the order of there Index Number(Index)!
MSComm1.PortOpen = True
OutBuffer = BytesToSend()
MSComm1.Output = OutBuffer
Command1.Enabled = False
Command2.Enabled = True
Text1.Text = ""
End Sub
Private Sub Command2_Click()
Dim InBuffer
Dim DimensionedByteArray(7) As Byte ' Can handle up to 8 Bytes
Dim intCount As Integer
Dim intDecimal As Integer
Text1.Text = ""
InBuffer = MSComm1.Input
On Error GoTo ErrTrap
For intCount = 0 To (LenB(InBuffer) - 1)
' For intCount = 0 To (LenB(InBuffer))
DimensionedByteArray(intCount) = CByte(InBuffer(intCount))
intDecimal = DimensionedByteArray(intCount)
Text1.Text = Text1.Text & " " & intDecimal ' Display as decimal number
Next intCount
Debug.Print TypeName(intDecimal)
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
Command1.Enabled = True
Command2.Enabled = False
Exit Sub
ErrTrap:
If Err.Number = (13) Then
Text1.Text = "Error 13 (Type Mismatch)"
Err.Clear
End If
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvEOF Then
Label2.Caption = "EOF Character Found!"
Else
Label2.Caption = "An OnComm Event Fired!."
End If
End Sub
Private Sub Timer1_Timer()
Label1.Caption = "InbufferCount = " & MSComm1.InBufferCount & " Bytes"
End Sub
Last edited by CDRIVE; Nov 10th, 2008 at 10:22 AM.
Reason: Added additional info
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 10th, 2008, 04:27 PM
#23
Thread Starter
Lively Member
Re: Mscomm input
Maybe I miss understood, Last time I checked ASCII included the numbers of the english language, and binary was only 1's and 0's, the board sends binary readings, I want REAL english numbers in my text box ex. 1,10,99,153 is that clear enough
-
Nov 10th, 2008, 07:32 PM
#24
Re: Mscomm input
Originally Posted by mystorchid
Maybe I miss understood, Last time I checked ASCII included the numbers of the english language, and binary was only 1's and 0's, the board sends binary readings, I want REAL english numbers in my text box ex. 1,10,99,153 is that clear enough
Sorry that I barked at you. The problem is, is that we don't want MSComm or VB to regard the Microcontroller output data as ASCII. We want to Input it as Binary (Byte) data and to be treated as an Integer when were done processing it. When using MSComm Binary data is declared and treated as Byte data .. passed to a Byte Array. Same thing goes for Outputing Byte data. Even one Byte must be declared as an Array.
I modified the demo that I posted earlier so you should be able to just copy and paste this as is. RThreshold is set to fire when the InBuffer has 8 Bytes in it. Just add your MSComm1.Settings(). If you have any problems I'll do my best to help.
Code:
Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1
mscomm1.Settings = (Your Settings)
MSComm1.RThreshold = 8
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then
Dim InBuffer
Dim DimensionedByteArray(7) As Byte ' Can handle up to 8 Bytes
Dim intCount As Integer
Dim intDecimal As Integer
InBuffer = MSComm1.Input
For intCount = 0 To (LenB(InBuffer) - 1)
DimensionedByteArray(intCount) = CByte(InBuffer(intCount))
intDecimal = DimensionedByteArray(intCount)
Text1.Text = Text1.Text & " " & intDecimal ' Display Decimal number
Next intCount
Debug.Print TypeName(intDecimal) ' Confirm InBuffer data is Integer
End If
End Sub
Last edited by CDRIVE; Nov 11th, 2008 at 10:26 AM.
Reason: Remove Rem (') from MSComm1.Settings( )
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 11th, 2008, 01:15 AM
#25
Thread Starter
Lively Member
Re: Mscomm input
Great, it works, thanks for your help
-
Nov 11th, 2008, 10:19 AM
#26
Re: Mscomm input
Originally Posted by mystorchid
Great, it works, thanks for your help
You're more than welcome and once again I'm truly sorry for being impatient and short with you. There are few MSComm users and even less Microcontroller Techies on the forum. I should be elated when one posts!
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
-
Nov 19th, 2009, 10:30 AM
#27
New Member
Re: [RESOLVED] Mscomm input
Hello , i am new to VB 6.0 and the code i am working on works on sigle data type and i am using serial communication that send me ascii numbers and i need to convert the ascii to single. The Code is :
Private Sub WithoutGE_Click()
Dim serialflag As Boolean
Dim serportname As String
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 5 'Defined COM port available on PC
MSComm1.InputLen = 1
MSComm1.Settings = "115200, N, 8, 1"
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() 'on serial event
If (MSComm1.CommEvent = comEvReceive) Then ' if something received
'serialData = MSComm1.Input shows me data type is byte i have mismatch error
'serialdata is variant error is not observed
serialByteData = MSComm1.Input
End If
End Sub
What should i do need help . Ascii to Byte and Byte to single ! but how !???
-
Nov 19th, 2009, 10:32 AM
#28
New Member
Re: [RESOLVED] Mscomm input
i get ascii character when i use the code ! but i need single data type !
-
Nov 19th, 2009, 06:08 PM
#29
Re: [RESOLVED] Mscomm input
You should post your own topic. It's not polite to hijack someone else's thread, especially when it's marked resolved.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|