Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Serial Port Data

Status
Not open for further replies.

vijip2001

Programmer
Oct 14, 2003
61
US
Hi all,

I am having problem reading the data from serial port(COM2) into a variable. The data coming back from the comm control has some special caracters,carriage and linefeed, along with my data.I have tried all the Baud rates and parities. It doesn't work. So i thought of parsing the value in the input buffer and take the value i need.
My code is:

Comm1.Object.CommPort = 2
Comm1.Object.Settings = "2400,O,7,1"
Comm1.Object.InputLen = 0
Comm1.Object.PortOpen = True
InString$ = Comm1.Object.Input

My program throws error when getting the input value in INSTRING$. I tried to assign the input to a string variable and byte variable. It won't work. Any idea? Thanks for all your help.

Subha
 
Subha,

Did you try using the MSComm control?

Code:
  MSComm1.CommPort = 2
  MSComm1.Settings = "2400,O,7,1"
  MSComm1.InputLen = 0
  MSComm1.PortOpen = True
  InString$ = MSComm1.Input




HyperEngineer
If it ain't broke, it probably needs improvement.
 
Yes. I did try that. It says 'Object invalid'.

Thanks,
Subha
 
Subha,

I will assume that you have added the MSComm contol to your project and placed it on a form. If you call the control from a sub or function in the form you should have no problem with the syntax MSComm1.Input. If you call it from a module or another form, you will need to precede it with the name of the form. I.e. Form1.MSComm1.Input. Other than that, I would tend to believe that somehow the MSComm file, MSCOMM32.OCX is not registered on your computer or has been corrupted. I'd be interested in the answer to this myself.



HyperEngineer
If it ain't broke, it probably needs improvement.
 
Which version of VB6 are you using? I seem to recall that Learning Edition doesn't include MSComm

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
Hi All,

Got the problem fixed. The comm port i was using was not directly connected to the scale. It was virtually connected through the network. Also it added extra characters and linefeeds. So i had to move the data to a string variable and parse it through.

Thanks for all your suggestions and help.

Viji
 
All,

I am still having few issues with the way the port opens and closes. Sometimes, the port doesn't close. Program throws error saying that the 'The Port Is Open'. The user has to physically unplug the comm port and replug and restart the computer and then it works. This is the code i have used.

If Comm1.Object.PortOpen = True Then
Comm1.Object.PortOpen = False
End If
Comm1.Object.CommPort = 1
Comm1.Object.Settings = "9600,O,7,1"
Comm1.Object.InputLen = 0
Comm1.Object.PortOpen = True
InString$ = Trim(Comm1.Object.Input)
Comm1.Object.PortOpen = False

Do i handle it in a different way? Any suggestions?
Thanks in advance for your help.

Subha
 
May be a timing problem. You may need to loop after closing your port until it returns PortOpen=False

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
Hi,
is there any way that the data you received you can save it in a text format? if you declare as data string. Thanks
 
Give this a try...

Dim strBuffer as String

If (MSComm1.InBufferCount > 0) Then
strBuffer = MSComm1.Input
Else
strBuffer = ""
End If

Here is a sample of some code I use... It's VBA but will work in VB as well....

Code:
Private Sub cmdOpenPort_Click()
PortName = VBA.Right(Me.cboPorts.Value, 1)

PortSetting = Me.intBaudRate & "," & Me.txtParity & "," _
    & Me.intDataBits & "," & Me.intStopBits
' Open the serial port
    MSComm1.CommPort = PortName
    MSComm1.Settings = PortSetting
    MSComm1.Handshaking = Me.txtHandShake
    MSComm1.InputLen = 0
    MSComm1.PortOpen = True
'Check buffer,
    If (MSComm1.InBufferCount > 0) Then
        strTimerInput = MSComm1.Input
    End If
Me.TimerInterval = 1000

MsgBox "Port is Open," & _
    vbLf & "Please Turn on your timer" & _
    vbCr & "Release starting gate, and trigger timer for test." 

End Sub

Private Sub Form_Timer()
    Dim intcounter As Integer
    Dim temp, a, b, c, d, e, f
    
    b = "B"
    c = "C"
    d = "D"
    e = "E"
    f = "F"
    
    If (MSComm1.InBufferCount > 0) Then
        strTimerInput = "" 'txtTestData.value
        strTimerInput = MSComm1.Input 'strTimerInput & MSComm1.Input
        txtTestData.SetFocus
        txtTestData.Value = strTimerInput
        temp = VBA.Mid(txtTestData, 1, 1)
        If temp = "A" Then  'Buffer has race data
            Lane6.Value = VBA.Mid(txtTestData, 3, 5)
            temp = InStr(1, txtTestData, b)
            Lane5.Value = VBA.Mid(txtTestData, temp + 2, 5)
            temp = InStr(1, txtTestData, c)
            Lane4.Value = VBA.Mid(txtTestData, temp + 2, 5)
            temp = InStr(1, txtTestData, d)
            Lane3.Value = VBA.Mid(txtTestData, temp + 2, 5)
            temp = InStr(1, txtTestData, e)
            Lane2.Value = VBA.Mid(txtTestData, temp + 2, 5)
            temp = InStr(1, txtTestData, f)
            Lane1.Value = VBA.Mid(txtTestData, temp + 2, 5)
             MsgBox "Test Data Recieved," & _
                vbCr & "If you are satisfied with readings" & _
                vbCr & "Please Save Port details for Race Configuration"
        End If
     End If

End Sub

Hope this helps...

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Hi wannabe1234

Take a look at the Open statement in help...

The code will be something like this...

Open "C:\test.txt" For Output as #1
Write #1, strInput
or
Print #1, strInput
or
Print #1, strInput;

They are similiar but will produce different results..

Don't forgot to close it when finished...

Close #1



AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Hi Guys,

Thanks for all your help. My commport is working fine now.
I am encountering another problem. I have a report that pulls up data from a select query. If i run the select query by itself against the database, it works perfect, except it is a lot of data and it takes along time to retrieve the data.
But if i run the report through the application, it goes into the form(record source is the select query) and after sometime it gives error 'ODBC connection failed'. How do identify the problem? Any ideas?

Thanks,
Subha
 
I'm quessing it has something to do with a Time Out setting in ODBC....

But it's just a guess!

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
I found the problem.The select query retuned 99000 rows. I am not sure why my access application cannot display so much of data. So i just wrote filters to reduce the data displayed. Now it works fine. Thanks for your help.

Subha
 
Hello AccessGuruCarl, I'm trying to write for the first time in my life a program that reads from the COM port.

The output of my COM device looks like this:
* B 1 E1 +021.8

I have to try to fetch the data and save only the actuel date/time and the data that comes after the E1 (HEX). The thing is I have no clu how to start with. I know that the COM port used is COM3.

But how the to read the data, NUL clu...

Could you please help me? I have to read the data from a COM thermometer. And the software deliverd with it doesn't do what I need, so I have to try to do it my self... ***smile***

I want to programm it in VB6, I know it's very old but it Legal.
 
Hello Kumschick,

From within VB --

Click Project from the menubar, Then Components...

Scroll down and click/add the Microsoft Comm Control.

Now add it to the form, add a button click event to open the port and read it. See previous code...

As for grabbing the data...

Does the result always have E1?
Is the + sign consistent?

If so take a look at the Instr, Mid, Left or Right Function.

Using the + sign, you would have something like this...

SearchChr = Chr(43) ' + sign
FoundChr = InStr(1, SearchChr)
CommResult = Mid(CommPort, FoundChr)

This is just a basic example! If you need help with these functions...

In the code window... Type the function name, Highlight it, then press F1 to open help.

Hope this gets you pointed in the right direction!



AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Hello Kumschick,

I forgot to mention....

If you right click the MS Comm after you place it on your form, the only setting you need to change will be the port number...

The baud, parity, data, and stop settings should be fine at the default setting! If you receive different input, you will need to find out the settings from the company or keep trying different ones, until they match.



AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top