|
-
Sep 14th, 2009, 03:09 PM
#1
Thread Starter
New Member
Sending and receiving data to RS232/MSComm thru a VB6.0 app
Hi there.. please help! This is very urgent!
I am developing an application which is pretty much a replacement of a hyperterminal. I am trying to send commands to the serial port using MSComm in VB6.0
When I type in on the hyperterminal h003128 I get a response OK. Or when I type in i003 it displays back some values.
What I am trying to figure out how do I send the h003128 via VB6. I believe I've undertood how to send data to the port. It seems I am not getting the correct formate. Please help.
This wont work.. the formate iAAADDD where i = i AAA = 003 and DDD = 128
MSComm1.Output "i003128"
Any idea? Thanks much.
-
Sep 14th, 2009, 03:44 PM
#2
Thread Starter
New Member
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
Here are the following ways I tried without any luck..
MSComm1.output = "h003128" + Chr(13)
MSComm2.output = "h003128" + Chr(13)
-
Sep 14th, 2009, 11:08 PM
#3
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
 Originally Posted by [email protected]
Here are the following ways I tried without any luck..
MSComm1.output = "h003128" + Chr(13)
MSComm2.output = "h003128" + Chr(13)
That should read:
Code:
MSComm1.Output = "h003128" & Chr(13)
'or
MSComm1.Output = "h003128" & vbCrLf
'or
MSComm1.Output = "h003128" & vbNewLine
However, if you want echo returns from the modem you have to tell it to do so with AT Commands. Do you know your AT Commands?
Here's a link that shows how to send an AT command and get a OK response from the modem.
http://www.vbforums.com/showthread.p...19#post3393719
<--- 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?? 
-
Sep 15th, 2009, 09:48 AM
#4
Thread Starter
New Member
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
CDRIVE thanks for your response.
Here's some details: I have a sensor connected into an oven. Connected via serial RS232 and does have some kind of conversion in between. However, hyperterminal communicates well when COM4 is seleted. So I have also used COM4 in my VB program. The sensor itself has memory. What I am tring to read is the memory of this sensor. The way the memory goes is something like this:
i001 = 70
i002 = 70
i003 = 97
...
and so on
to
i122 = 30
So on hyperterminal soon as I type in i003 it returns 97. Let say I am not happy with value it returned I can change the value. To change the value I have a command hAAADDD. Where h = h; AAA = 003; DDD = whatever value I want to set to i003 (let say 100). With this if I simply type h003100, I get an OK back. That tell me i003 has been changed from 97 to 100.
With VB6, I sent (as per your suggestion)
MSComm1.Output = "h003100" & Chr(13)
and using hyperterminal I read the value for i003 but it has not been changed from 97 (previous value) to 100 (new value). I was wondering, since I am not sending any AT command (per your suggestion) at least sending command/writting to serial port should work. However it is not working. Is this because the AT commands are required? I saw the other forum link you sent but I dont think I have any AT commands. Or would AT command be i003? Because this command is reading the memory of the sensor.
Also these commands are recognized by hyperterminal as key entery. Meaning I am not pressing enter to send the command. Would this make any difference? Should I try keyPress?
I have used error handling to tell me kwow if there is any error with the COM PORT connection. To this it returned a blank message. When the port is already open it returns port is allready open. But I am not sure why it returns a blank message box when I try to first open the port.
Here's the code
Private Sub Command1_Click()
On Error GoTo handler
MSComm1.CommPort = 4
If Not MSComm1.PortOpen Then
MSComm1.PortOpen = True
End If
MSComm1.output = "h003128" & Chr(13)
Call MSComm1_OnComm
handler: MsgBox Err.Description
End Sub
Private Sub MSComm1_OnComm()
On Error GoTo handler
Dim strInput As String
With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input
Text1.SelText = strInput
End Select
End With 'MSComm1
handler: MsgBox Err.Description
End Sub
Any further advise on this will be highly appreciated. I have not played with reading or writing to serial port.
Thanks again.
Last edited by [email protected]; Sep 15th, 2009 at 10:05 AM.
Reason: want to add
-
Sep 15th, 2009, 06:33 PM
#5
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
OK, start a new project and paste this code. Then put a CommControl and two TextBoxes on your Form. Leave the default names of the controls. Set Text2 to Multiline in the Properties window but leave Text1 as Multiline = False. This will prevent a CarReturn being sent along with the text.
My code does not include your MSComm1.Settings because I don't know what your settings are. So write it in code in the Form_Load or the Properties window.
To use this code: Type your data in the Text1 and hit Enter to Output the data.
One last thing here. You stated that you don't hit Enter when sending from HyperTerm. This would seem to indicate that HyperTerm sends the data upon every KeyPress. So if this code doesn't do what you want we will try that next. 
Code:
Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Text1.TabIndex = 0
Text1.Text = ""
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then ' Send data when Enter is pressed
MSComm1.Output = Text1.Text
Text1.Text = "" ' Clear TextBox
End If
End Sub
Private Sub MSComm1_OnComm()
Dim InBuffer As String
InBuffer = MSComm1.Input
If MSComm1.CommEvent = comEvReceive Then
Text2.Text = Text2.Text & InBuffer
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End Sub
Last edited by CDRIVE; Sep 15th, 2009 at 07:45 PM.
Reason: add port close procedure
<--- 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?? 
-
Sep 16th, 2009, 12:41 PM
#6
Thread Starter
New Member
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
No luck even with your code.
I included
<<code>>
MSComm1.Settings = "9600,n,8,1"
<<//code>>
(ah.. the code thing is not working for me how do you create the little widnow to type in your code)
I tried the keyEnter Sub Routine without any luck. However, I would curtainly love to see your code to compare with mine.
Again whether we get it working or not, I cannot thank you enough to try to help me out. Specially posting the codes for me.
I have found out that the oven came along with a DLL. I am studying this more in detail to see if it provides any memory reading functionality.
I am new to programming as well as posting on a forum. I am glade to see there are people like you who are so willing to help others.
Last edited by [email protected]; Sep 16th, 2009 at 12:43 PM.
Reason: edit
-
Sep 16th, 2009, 04:25 PM
#7
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
The code that I posted is good to go as is except for your Settings which must match what your device expects to see. Text1 is your text editor window and Text2 is the receive window. You type your data in Text1, hit Enter and it should receive the return from the device in Text2.
<--- 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?? 
-
Sep 17th, 2009, 09:33 AM
#8
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
Sometimes we forget what it was like being a newbie to VB and how foreign everything was, so hang in there and we will walk you through.
Some questions:
Do you know how to copy and paste code from a post?
Do you know how to use Code Tags when posting your code?
If the answers to these questions are no we can help.
Here's some more Comm code that transmits the keyboard data whenever the user presses that key. So to restate, this app does not wait for Enter to be pressed to send data as a block or "Packet". This app also provides two methods for clearing the Text Editor, which is Text1. If you press Enter or click Command1 Text1 will be cleared. Text2 is used to Input the data from your device. This app needs two TextBoxes, One CommandButton and a CommControl.
Since I'm not certain if your device does not like carriage returns I've disabled it in the Text1_KeyPress event.
One more thing: Is that your real email address in your User Name? If it is I would highly recommend having the Mods change it for you. Posting your email address is a very bad idea, unless you love receiving spam. 
The following code was tested using a LoopBack Tester:
Code:
Option Explicit
Private Sub Form_Load()
Text2.Text = ""
Command1.Caption = "Clear Text Editor"
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MSComm1.RThreshold = 1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then ' If user presses Enter don't send it.
Exit Sub
Else
MSComm1.Output = Chr(KeyAscii) ' Output each character when user presses the key.
End If
End Sub
Private Sub MSComm1_OnComm()
Dim InBuffer As String
InBuffer = MSComm1.Input
If MSComm1.CommEvent = comEvReceive Then
Text2.Text = Text2.Text & InBuffer ' Put received data in Text2
End If
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then ' The user pressed Enter.
Text1.Text = "" ' Clear the Text (Text1) Editor
Text2.Text = Text2.Text & " " ' Provide a space to indicate new string
End If
End Sub
Private Sub Command1_Click()
Text1.Text = "" ' Clear the Text (Text1) Editor
Text2.Text = Text2.Text & " " ' Provide a space to indicate new string
Text1.SetFocus ' Keep the focus on the Text (Text1) Editor
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End Sub
Last edited by CDRIVE; Sep 17th, 2009 at 09:52 AM.
Reason: Modify code
<--- 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?? 
-
Aug 9th, 2014, 11:18 AM
#9
Registered User
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
Dear CDRIVE,
1.I would like try your code above, but I don't know how to send the data. As you said before, you've disable the Text1_KeyPress event. Does it is the reason why I couldn't send the data?
Kindly regards.
-
Aug 9th, 2014, 06:28 PM
#10
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
If you're using VB6, did you perchance install the MSDN help libraries that came with it.
There were a lot of example programs installed as part of the MSDN install.
On my XP virtual machine, the path is
C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98\Mscomm
That is an example Terminal program, similar to hyperTerm.
If you try that out, and can get it to work, then you may be able to pull the pieces you want from that to tailor it to your specific needs.
-
Aug 11th, 2014, 09:08 AM
#11
Re: Sending and receiving data to RS232/MSComm thru a VB6.0 app
 Originally Posted by hafizuddinlowhim
Dear CDRIVE,
1.I would like try your code above, but I don't know how to send the data. As you said before, you've disable the Text1_KeyPress event. Does it is the reason why I couldn't send the data?
Kindly regards.
This is an old thread and I haven't been active here in quite some time. That said I read through my last post it does give me pause as to why I used the term "disabled". The KeyPress event isn't disabled at all. It simply won't execute an MSComm Output if 'Enter' (VbCrLf) is pressed. Instead, it exits the event if 'Enter' is pressed.
<--- 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
|