|
-
Dec 6th, 2009, 03:10 AM
#1
Thread Starter
New Member
MSCOMM, how to use non-standard speed
Hello everyone.
This is my first email requesting help in this forum involving experts in VB.
I have a problem with VB6 (SP6) I am trying to resolve for several months.
I am developing an application that uses a PIC microcontroller interfaced with an application in VB6 and I'm using two-way communication using MSCOMM.
I need to exploit the faster serial transmission that allows the PIC. The speed in KBaud are:
156.250
230.400
250.000
312.500
625.000
1000.000
1250.000
Just to have a reference, this is what I use to test speed:
Code:
Private Sub Form_Load ()
On Error Resume Next
MSComm1.CommPort = 7
'MSComm1.Settings = "19200, n, 8,1"' OK
'MSComm1.Settings = "115200, n, 8,1"' OK
'MSComm1.Settings = "128,000, N, 8,1"' no message
'MSComm1.Settings = "250000, n, 8,1"' no message
'MSComm1.Settings = "256,000, N, 8,1"' no message
MSComm1.PortOpen = True
MSComm1.Output = "test message"
I ask this community as I do to communicate with these speeds using VB6?
Ciao
Leo
Last edited by wirecut; Dec 6th, 2009 at 03:11 AM.
Reason: typo error
-
Dec 6th, 2009, 11:31 AM
#2
Re: MSCOMM, how to use non-standard speed
According to the documentation:
The following baud rate values are valid: 110, 300, 600, 1200, 2400, 4800, 9600 (default), 14400, 19200, 28800, 38400, 56000, 57600, 115200, 128000, 256000.
-
Dec 6th, 2009, 11:58 AM
#3
Re: MSCOMM, how to use non-standard speed
Looking at the values for dwMaxBaud in COMMPROP Structure even leaves me with doubts about that 256000 value.
But this is really telling you the limitation isn't MSComm32.ocx, nor VB6, nor even Windows, but instead the range of standard bitrates supported by standard UARTs used in Windows machines. To get beyond those standard settings probably requires setting the rate to "user programmable" and then pushing some clock-related value (divisor?) to the UART directly.
-
Dec 6th, 2009, 04:35 PM
#4
Fanatic Member
Re: MSCOMM, how to use non-standard speed
It is possible to use non-standard speeds and data lengths. These speeds must be supported by the UART (thus, probably this only applies to hardware UARTs, not software UARTs that are used by USB serial adapters).
MSComm32.ocx does not support these non-standard speeds and lingths natively. You have to first open the port using the PortOpen method, then call a couple of Windows serial communications APIs to overwrite the settings that you used when the port was opened, with the ones that you actually want to use. I have a VB6 code example that shows this in my book, Visual Basic Programmer's Guide to Serial Communications 4. I've copied it here:
Copyright 2006, Richard Grier
Private Declare Function SetCommState Lib "Kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Private Declare Function GetCommState Lib "Kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
Private Type DCB ' Win32API.TXT is incorrect here.
DCBlength As Long
BaudRate As Long
Bits1 As Long
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved2 As Integer
End Type
Private Sub Command1_Click()
Dim SpeedToSet As Long
SpeedToSet = 7812
SetSpeed SpeedToSet
End Sub
Private Sub Command2_Click()
Dim Size As Byte
Size = 5
SetLength Size
End Sub
Private Sub Form_Load()
With MSComm1
.Settings = "9600, N, 8, 1"
.CommPort = 1
.RTSEnable = True
.PortOpen = True
End With
End Sub
Private Sub SetSpeed(ByVal Speed As Long)
Dim MSCommDCB As DCB
Dim Port As Integer
Port = GetCommState(MSComm1.CommID, MSCommDCB)
MSCommDCB.BaudRate = Speed
Port = SetCommState(MSComm1.CommID, MSCommDCB)
End Sub
Private Sub SetLength(ByVal Length As Byte)
Dim MSCommDCB As DCB
Dim Port As Integer
Port = GetCommState(MSComm1.CommID, MSCommDCB)
MSCommDCB.ByteSize = Length
Port = SetCommState(MSComm1.CommID, MSCommDCB)
End Sub
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Dec 6th, 2009, 04:36 PM
#5
Fanatic Member
Re: MSCOMM, how to use non-standard speed
Typo: that should be lengths!
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Dec 8th, 2009, 04:24 PM
#6
Thread Starter
New Member
Re: MSCOMM, how to use non-standard speed
Hello DickGrier,
just to get the situation clear now I am using a desktop PC with an ASUS motherboard P5QEL/EPU Intel Pentium Dual E2200 and use the COM: 1 native of the motherboard.
The piece of code I'm using is as follows:
Code:
Private Sub Form_Load ()
On Error Resume Next
MSComm1.CommPort = 1
MSComm1.Settings = "115200, n, 8,1"
'MSComm1.Settings = "128000, n, 8,1"
'MSComm1.Settings = "256000, n, 8,1"
MSComm1.PortOpen = True
For n = 1 To 100
MSComm1.Output = "test message" & vbCrLf
Next n
To remove any doubt, I connected an oscilloscope to pin 3 TX and with the set-up
Code:
MSComm1.Settings = "115200, n, 8,1"
I see the output data on the scope.
If I use
Code:
MSComm1.Settings = "128000, n, 8,1"
or
Code:
MSComm1.Settings = "256000, n, 8,1"
from pin 3 TX nothing comes out and the level still locked at -12V.
Ciao
Leo
-
Dec 8th, 2009, 04:38 PM
#7
Fanatic Member
Re: MSCOMM, how to use non-standard speed
Perhaps these speeds are not supported by your hardware.
So, how do you determine what speeds are supported? That is fairly easy. Standard RS-232 port UARTs support a maximum speed of 115200 bps. Thus, you will not be able to achieve speeds higher than this! Perhaps this is not surprising?
You would need specialized serial hardware for these higher speeds -- and the documentation for that hardware will tell you exactly what speeds are supported.
Now, why is this, you may ask. Here is the story, for anyone interested. A UART has a crystal clock frequency input. The absolute maximum asynchronous serial speed that a UART can support is 1/16 x this clock speed. Thus, a crystal oscillator that furnishes 1.8432 MHz clock input to the UART can operate at speeds up to 115200 bps, and no higher. This is the standard oscillator frequency used. Higher speed UARTs require a faster clock (and an upgraded UART).
Now, just because the UART supports speeds up to 115200 bps, does not mean that [B]all] lower speeds are supported. Any lower speed must be some multiple of the minimum speed supported. Other speeds may be secected, and the UART will attempt to set its operation to the closest actual supported speed.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Dec 8th, 2009, 05:07 PM
#8
-
Dec 9th, 2009, 06:16 PM
#9
Thread Starter
New Member
Re: MSCOMM, how to use non-standard speed
Hi DickGrier.
Your explanation is thorough and convincing, but I often wonder why we read on the web that can be set with the API anon standard speeds.
So to have a speed greater than 115,200 bps I have to buy a card with a special high speeds USART with related software.
Or I can alternatively use a USB-> Serial?
Regards
Leo
-
Dec 9th, 2009, 08:24 PM
#10
Fanatic Member
Re: MSCOMM, how to use non-standard speed
Some USB serial adapters may work, some may not. Many support speeds higher than 115200 bps, but they may not allow "just any speed" (the ones that you mention probably will be supported, if they allow any such higher speed). It is up to the manufacturer to provide such a driver.
However... At the end of the day, what have you really gained? It isn't as though you actually will see higher throughput, except in some very obscure applications. The practical bandwidth limit for a single serial channel is about 100 Kbps. We can get into the reasons for that, if you are interested. In any case, any high-speed data transfer will require hardware flow control, because of hardware and Windows limitations, keeping the raw data rate to much less than you might expect.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
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
|