View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
mscomm |
Posted: Mon Jan 07, 2013 4:14 pm |
|
|
I started the first application using RS232 to send data to PC. First I used the hyperterminal and I sent the following every second:
Date: 02/01/13 Time: 01:33:16
..and it worked perfectly. When I tried to make the same using the VB6, I ma getting junk characters. What I am doing wrong? I believe that the problem is in the vb code. The VB code is:
Code: |
Dim str As String
Private Sub Form_Load()
Dim stringline As String
Dim fnum As Integer
MSComm1.CommPort = 6
MSComm1.InputLen = 0
MSComm1.InBufferSize = 5000
MSComm1.PortOpen = True
MSComm1.RThreshold = 1
End Sub
Private Sub MSComm1_OnComm()
If (MSComm1.CommEvent = comEvReceive) Then ' if something received
Text1.Text = MSComm1.inpout 'save it to text3
End If
End Sub
|
thanks for eveyrhing
Using PIC18F4550, RS232 parameters: 57600,n,8,1 |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jan 07, 2013 4:59 pm |
|
|
Hi,
You need to set the MSCOMM Settings property to tell the code the correct
baud rate, date bits, etc. If this is not set then undoubtedly there are some
default values, and they don't match the ones in use by your PIC.
John |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Mon Jan 07, 2013 5:04 pm |
|
|
I set these parameters from the gui of visual studio. I removed them from the code to keep it short as possible. But i can confirm that settings match with those of the pic |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jan 07, 2013 5:57 pm |
|
|
Hi,
OK, I like to do it "in-line" so that I can see for sure what is going on. There
is also an inputmode property that should be set to 'text'...
John |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Mon Jan 07, 2013 6:01 pm |
|
|
How? The rest is ok? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jan 07, 2013 7:12 pm |
|
|
Hi,
The syntax is mscomm.inputmode = comInputModeText
This is the default setting, so it may be OK, I just like to set it explicitly.
If you don't get it working, I'll send some tested code in the morning.
John |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Jan 07, 2013 8:45 pm |
|
|
Two things come to mind - unfortunately, I don't have visual studio 6 installed on this machine, but I seem to remember that the upper baud rate limit was lower than what you are indicating (without adding one of the commercial com ocx packages - I thought it was either 9600 or 19,200). Also you have "Text1.Text = MSComm1.inpout 'save it to text3 " inpout?
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Tue Jan 08, 2013 3:23 am |
|
|
Start with making sure the config is right:
If the port is open, close it. Set the settings, then open it. So:
Code: |
'On form load.
If MSComm.PortOpen Then MSComm.PortOpen = False
MSComm.CommPort = your_port_number
MSComm.Settings = "57600,N,8,1"
MSComm.InputMode=comInputModeText
MSComm.InBufferSize=2048
MSComm.InputLen=1 'Must be one if you want single characters
MSComm.Handshaking=comNone
MSComm.RThreshold=1
MSComm.NullDiscard=False
If MSComm.PortOpen = False Then MSComm1.PortOpen = True
MSComm.DTREnable = True ''Need these if using USB
MSComm.RTSEnable = True
|
This configures a port at 57600bps, via a standard serial connection, or USB, for VB6.
Then if you have an event:
Code: |
Private Sub MSComm1_OnComm()
Dim dummy As String
dummy = MSComm.Input
Text1.Text = Text1.Text + dummy 'Obviously put this where you want
End Sub
|
You don't actually need the 'if' you have here, since MSComm.Input will simply return null, if there is no data to read. It'll trigger on every character though, so to see what is coming, you need to append the character to the existing string.
On whether 57600 will work, it depends on the version of VB6, and the chipset. There were problems with the older MSCOMM driver at higher baud rates on some chipsets, but this has been fixed by one of the service packs, and the standard driver will happily run at 57600bps, provided you have these later service packs.
Best Wishes |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed Jan 09, 2013 12:39 pm |
|
|
Thanks to everyone i changed the baud rate to 9600 imsteadof 57600 and modified the program accordingly and everything is working good. One issue is that when i change the code from
Code: | text1.text =text1.text + dummy |
To
Code: | label1.caption = dummy |
The data is not being updated. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Jan 09, 2013 3:13 pm |
|
|
Your question really has nothing to do with CCS C relative to PICs ! You should either goto a VB forum or PM one of the responders who has already helped you..quick.before this thread is 'locked' as being off topic.
hth
jay |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Wed Jan 09, 2013 3:15 pm |
|
|
ok thanks. I think it will be much better to keep it open. Somebody might have the same problem and he find help. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Wed Jan 09, 2013 3:42 pm |
|
|
Note the line I put in my post:
"It'll trigger on every character though, so to see what is coming, you need to append the character to the existing string. "
The +dummy, is this 'appending'.
Problem is that otherwise you will just get a single character changing as each character arrives, and only have one character in the text to be read.
Best Wishes |
|
|
|