• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

l need to know reason behind using Timer, please help?

Status
Not open for further replies.
hi,

l am currently trying to connect the serial port (COM1) of a computer to an 8051 Microcontroller. l've figured out that l have to use a Visual Basic timer in order to detect a packet that comes from the micrcontroller, but the question is why exactly do I need to use a timer for the serial port (COM1)?

please help
 

Nigel Goodwin

Super Moderator
Most Helpful Member
destiny_of_light said:
hi,

l am currently trying to connect the serial port (COM1) of a computer to an 8051 Microcontroller. l've figured out that l have to use a Visual Basic timer in order to detect a packet that comes from the micrcontroller, but the question is why exactly do I need to use a timer for the serial port (COM1)?
I wasn't aware that you need a timer?, and it sounds VERY unlikely, VB (like Delphi) is event driven, the interrupt initiated by the serial port when it receives a character is the event that lets the compiler know there's data ready.

I suggest you try specific VB forums, where there will be a lot more people who VB (I don't for one).
 

motion

New Member
The "packet" is a group of bytes sent serially from the microcontroller. You have to assume that they come one after another with little time delay. You need a timer to detect if the the sending of the packet has been disrupted.

Let's say your PC has received a partial packet and the message was disrupted for any reason. If the microcontroller sends another packet, the PC will assume the incoming bytes will still be part of the first packet. If a timer is employed, the partial packet is discarded if the receiver stops receiving after a set time delay.
 
You need the MSCOMM control in VB. Set it to the correct baud, stop bits, etc. It will take care of everything else. You put data into its transmit buffer, or read data from its receive buffer.

Here's a simple example:

Receive:
Dim buffer as string
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
If MSComm1.InBufferCount > 0 Then
buffer = MSComm1.Input
End If
MSComm1.PortOpen = False

Send:
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MSComm1.Output = "A"
MSComm1.PortOpen = False

Mike
 

mstechca

New Member
destiny_of_light said:
l've figured out that l have to use a Visual Basic timer in order to detect a packet that comes from the micrcontroller,
Visual Basic wasn't designed to deal with COM ports. But you could use special API files with Visual Basic to access the COM ports.

but the question is why exactly do I need to use a timer for the serial port (COM1)?
Let's make the assumption that your microcontroller is a "slow" internet server, and your computer is ridiculously fast.

Basically, your VB timer allows you to use the COM port in asynchronous mode. This means that you send a byte, formatted so that the microcontroller understands it, then you wait until a special bit is received, and once that bit is received, it can be assumed that the correct byte is arrived. These special bytes are packets.

The only way you wouldn't need a timer is if the program you are making is synchronizing with the COM port. For example, Sending a byte, and halting the program until a byte is received. This method is worse if you don't want your PC to lock up, and I doubt it is possible with any 32-bit version of VB.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Please ignore the post from 'mstechca', as usual it's completely wrong and just a load of random rubbish!.

As 'upand_at_them' said, you need the MSCOMM VB control - or write your own?, or you can even simply open the comport as a file. But MSCOMM is the normal method of doing it.
 
H

hyedenny

Guest
mstechca said:
Let's make the assumption that your microcontroller is a "slow"

Let's make the assumption that mistakeca is "slow."
 

akg

New Member
if ur using windows API , u need to use a timer/thread to poll the (blocking) read functions.

as "upand_at_them" said , using the mscomm ctrl in vb is the easiest way of doing that
from msdn :-
Code:
Private Sub Form_Load()
         Form1.Caption = "App2"
         With MSComm1
            .CommPort = 2
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .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
      End Sub
 

Pommie

Well-Known Member
Most Helpful Member
Something I discovered recently about the MSCOMM control. If you set the RThreshold property to zero it will generate an oncomm event for every character received. This works fine with generic com ports but fails miserably with USB to Com adapters. With USB adapters, the value of zero is interpreted as 65535 - IE the longest buffer possible. This caused various problems on a project I worked on a couple of years ago. Changing RThreshold to 1 solves the problem.

I suspect this maybe the problem with a lot of software.

Mike.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top