0

the code is to get data from a microcontroller or any device from serial device using serial port,so i am having problem with port opening and getting data,am having this problem for last 20 days please kindly help me at the earliest :)

Private Sub Command1_Click()
    MsgBox ("The port is open " & MSComm1.PortOpen)

    If (MSComm1.PortOpen = False) Then
    MSComm1.PortOpen = True
    End If
    Command1.Enabled = False
    Command2.Enabled = True
End Sub

Private Sub Command2_Click()
    If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
    End If
    Command1.Enabled = True
    Command2.Enabled = False
End Sub

Private Sub Form_Load()
    With MSComm1
    .CommPort = 1
    .RThreshold = 1
    .RTSEnable = True
    .Settings = "9600,N,8,1"
    .InputLen = 127
    .SThreshold = 1
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
    End If
End Sub

Private Sub MSComm1_OnComm()
    Dim Buffer As String

    Select Case MSComm1.CommEvent
    Case comEvReceive
    'Text1.Text = " "
    Buffer = MSComm1.Input
    Text1.Text = Text1.Text & Buffer
    End Select
End Sub!

Below is the image of interface which contains the MScomm control ,a text box , two command buttons for connecting and disconnecting :

enter image description here

Hrqls
  • 2,944
  • 4
  • 34
  • 54
user1963933
  • 11
  • 1
  • 4
  • 12
  • what errors do you get? could you try it with inputlen=0 ? do you receive any data at all? (put a breakpoint on the line with text1.text=... and watch the contents of your buffer variable – Hrqls Feb 27 '13 at 06:32
  • i didnt get any data at all-"INVALID PORT number",RUNTIME ERROR 8002 i connected to port 1 itself , checked it in the device mangager – user1963933 Feb 27 '13 at 08:49
  • http://i.stack.imgur.com/vNgaM.jpg – user1963933 Feb 27 '13 at 09:03
  • i am using MAX 232 instead of RS 232 to connect the peripheral interface microcontroller and the computer – user1963933 Mar 05 '13 at 09:03
  • i am connecting the RS 232 to the USB port, isnt the com port and USB port the same? – user1963933 Mar 05 '13 at 09:21
  • i dont know max 232, whats the difference compared to rs232 ? – Hrqls Mar 05 '13 at 15:23
  • when a device is connected to an usb port, it sometimes can be reached like an rs232 port. connect the device, and run the program i posted in the answer, it will show you any rs232 ports your system has – Hrqls Mar 05 '13 at 15:24
  • The MAX232 from Maxim was the first IC which in one package contains the necessary drivers and receivers to adapt the RS-232 signal voltage levels to TTL logic. It became popular, because it just needs one voltage (+5V or +3.3V) and generates the necessary RS-232 voltage levels. – user1963933 Mar 06 '13 at 09:11
  • so the max232 is a driver for your usb connection? connect your device, and see if it shows a rs232 port in your device manager, or run the code from the project i posted in the answer below – Hrqls Mar 06 '13 at 15:50

2 Answers2

1
    '****** paste this in form'*********    
    Option Explicit

        Dim Portnumber As Integer


        Private Sub cmdClose_Click()

            On Error GoTo handler

            MSComm1.PortOpen = False
            Shape1.FillColor = vbRed
            cmdOpen.Enabled = True
            txtRecieve.Text = ""
            Exit Sub

        handler:                                MsgBox Err.Description
        End Sub

        Private Sub cmdOpen_Click()

            On Error GoTo handler

         '   Debug.Print cboComm.ItemData(cboComm.ListIndex)

            portnumber = Mid(cboComm.Text, 4, (Len(cboComm.Text) - 3))

            a = Mid(cboComm.Text, 4, (Len(cboComm.Text) - 3))
        '    If MSComm1.PortOpen = False Then
                MSComm1.CommPort = portnumber
                MSComm1.PortOpen = True
                Shape1.FillColor = vbGreen
                cmdOpen.Enabled = False
        '    End If

            Exit Sub

        handler:                   MsgBox Err.Description
        End Sub

        Private Sub Form_Load()
            cboComm.Clear   '*** cbo is for combobox
            MSComm1.Settings = "9600,n,8,1"
            ListComPorts
        End Sub

        Private Sub ListComPorts()

            Dim i As Integer

            cboComm.Clear

            Static iData As Integer
            iData = -1

            For i = 1 To 16
                If ComAvailable(i) Then
                    cboComm.AddItem (("COM") & i)
                    iData = iData + 1
                    cboComm.ItemData(iData) = i
                End If

            Next

            cboComm.ListIndex = 0
            '    cmdGet.Enabled = False
        End Sub

        Private Sub MSComm1_OnComm()

            Select Case MSComm1.CommEvent

                Case comEvReceive
                    txtRecieve.Text = MSComm1.Input

                Case Else
                    Debug.Print "Event: " & MSComm1.CommEvent
            End Select

        End Sub
        '**************** End of form code **************

       '*********** Now API code******************

        '********** Paste in Module**************

        Option Explicit

        '*** API Declarations
        Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

        Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

        '*** API Structures
        Public Type SECURITY_ATTRIBUTES

            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long

        End Type

        '***API Constants
        Public Const FILE_SHARE_READ = &H1

        Public Const FILE_SHARE_WRITE = &H2

        Public Const OPEN_EXISTING = 3

        Public Const FILE_ATTRIBUTE_NORMAL = &H80

        '*** Create a Fuction to check whether COM exists or not. If exists return "true" otherwise "false"
        Public Function ComAvailable(comnum As Integer) As Boolean

            Dim hcom As Long

            Dim ret  As Long

            Dim sec  As SECURITY_ATTRIBUTES

            hcom = CreateFile("\.\COM" & comnum & "", 0, FILE_SHARE_READ + FILE_SHARE_WRITE, sec, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)

            If hcom = -1 Then
                ComAvailable = False
            Else
                ComAvailable = True

                '*** close the CO MPort
                ret = CloseHandle(hcom)
            End If

        End Function

        ''''''''*******End of module code********

I think  this will help you.....
nkpandit
  • 44
  • 9
0

If you get error 8002 then the port probably doesn't exist.

Are you using an rs232 connection, or are you connect via an USB port?

Have a look at the code i posted here .... when you run it, it will give a list of available ports on your system.

Community
  • 1
  • 1
Hrqls
  • 2,944
  • 4
  • 34
  • 54