CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: MSComm

  1. #1
    Join Date
    Oct 2001
    Posts
    3

    MSComm

    I would like to capture the weight through the signal send out from the weighing machine. The weighing machine is connecting to the PC by using com port.
    Can I have a sample code for capturing the signal by using MSComm or maybe you have better suggestion on this matter. Thanks for your help.



  2. #2
    Join Date
    Jul 2001
    Location
    Tijuana, Mexico.
    Posts
    5

    Re: MSComm

    The following sample reads from the com port looking for temperature data and displaying it on a label control.

    For your application you would have to change the MScomm1 event code to look for whatever your machine sends, then parse it and format accordingly.
    The Static Buffer within that procedure is required since the data might not arrive complete, hence you have to save it until you can determine it's safe to process.





    option Explicit
    Dim Temperature as Single

    private Sub MScomm1_OnComm()
    static Buffer as string
    Dim i as Integer, ts as string
    on error resume next
    Select Case MScomm1.CommEvent
    ' Handle each event or error by placing ' code below each case statement
    ' Errors
    Case comEventBreak ' A Break was received.
    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
    com.InputLen = 0
    'retrieve all chars
    Buffer = Buffer & MScomm1.input
    'search for CH01 data (look for what the particular device sends)
    i = InStr(Buffer, "CH01:")
    'if string found and the temp data is there then grab it
    Do While i > 0 And len(Buffer) > (i + 6)
    'grab the numeric part of the data
    ts = mid(Buffer, i + 5, 6)
    'convert to number
    Temperature = Val(Trim(ts))
    'display Temp and/or do whatever
    lblTemp.Caption = Format(Temperature, "##0.0 C")
    'discard used part of string
    Buffer = mid(Buffer, i + 11)
    'see if more data is available for CH01
    i = InStr(Buffer, "CH01:")
    Loop
    Case comEvSend ' There are SThreshold number of chars in the TX buffer
    Case comEvEOF ' An EOF character was found in the input stream
    End Select
    End Sub

    private Sub Form_Load()
    on error resume next
    MScomm1.CommPort = 2
    MScomm1.Settings = "9600,n,8,1"
    'no handshaking
    MScomm1.Handshaking = comNone
    'ample buffer size
    MScomm1.InBufferSize = 2048
    'fire the RECEIVE event when 1 or more chars waiting
    MScomm1.RThreshold = 1
    'don't fire the SEND event
    MScomm1.SThreshold = 0
    'open port
    MScomm1.PortOpen = true
    If Not MScomm1.PortOpen then
    MsgBox "Unable to open COM" & MScomm1.CommPort, vbCritical, "error"
    End
    End If
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    on error resume next
    'don't leave port open, release it
    MScomm1.PortOpen = false
    End Sub






  3. #3
    Join Date
    Jan 2002
    Posts
    1

    Re: MSComm objects

    Hi,

    I use MSCOMM control to track the calls made through PABX system. The program must display the details such as duration, date and number of the calls.

    The problem is MSComm1_OnComm() never occur in my program. I can caputure the MSCOMM1.Input during some other events such as form closing.

    When ever the user puts down the phone (after making calls) the details about the call must be updated to the screen.

    Your help will be greatly appreciated.

    Thank you.

    Rathi.J [email protected]


  4. #4
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: MSComm objects

    See MSDN VB sample VBTerm.

    btw: Could send you a sample if you still need.

    Capture new dimensions http://www.leica-geosystems.com
    Leica Geosystems - when it has to be right

  5. #5
    Join Date
    Dec 2005
    Posts
    3

    Re: MSComm

    Hii markus,
    Can u please send a sample code,that will b useful for me too for some reference.
    regards
    Naseem

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: MSComm

    Quote Originally Posted by naseem_cm
    Hii markus,
    Can u please send a sample code,that will b useful for me too for some reference.
    regards
    Naseem
    Hello Naseem!
    Just so you know, this thread is quite OLD - in future it would be best to start your own thread, based on your problem.

    Here's a small pice of code that might be useful to you
    Code:
    Private Sub  Form_Load ()
       ' Buffer to hold input string
       Dim  Instring As String
       ' Use COM1.
       MSComm1.CommPort = 1
       ' 9600 baud, no parity, 8 data, and 1 stop bit.
       MSComm1.Settings = "9600,N,8,1"
       ' Tell the control to read entire buffer when Input
       ' is used.
       MSComm1.InputLen = 0
       ' Open the port.
       MSComm1.PortOpen = True
       ' Send the attention command to the modem.
       MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that 
       ' the modem responds with "OK".
       ' Wait for data to come back to the serial port.
       Do
          DoEvents
       Buffer$ = Buffer$ & MSComm1.Input
       Loop Until InStr(Buffer$, "OK" & vbCRLF)
       ' Read the "OK" response data in the serial port.
       ' Close the serial port.
       MSComm1.PortOpen = False
    End Sub
    I've included a sample as well - it's one of MSDN's examples
    Enjoy!
    Last edited by HanneSThEGreaT; December 19th, 2008 at 03:00 AM.

  7. #7
    Join Date
    Dec 2005
    Posts
    3

    Re: MSComm

    dear markus,
    Thnax for ur code...
    naseem

  8. #8
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: MSComm

    ?? Did nor send you any sample ... first had no time and
    then forgot ... sorry...

    I appended the sample project here. It's quick and dirty, but you
    should be able to see how to use the MSComm control. You can
    use a terminal program to test or a Null-modem (send line of the
    port connected to the receive line). If you use a Null-modem,
    you should see in the second text box what you have entered
    in the first one if you press the send button. The binary option
    is for integer numbers only.
    Attached Files Attached Files
    Leica Geosystems - when it has to be right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured