본문 바로가기
VB

[VB6.0] Serial 포트

by 엘딘 2022. 5. 16.

먼저 가상 포트 연결하기

 

* 앞선 두 프로그램 설치 및 포트 설정

 

 

1. 좌측 아이템창 우클릭하여 구성요소 클릭

2. 구성요소에서 Microsoft Comm Control 6.0선택

3. 전화기 모양이 생성되면 폼에 생성

 

Form_Load()
Private Sub Form_Load()
    Dim sBuf As String
    Dim RetVal As String
    Dim rsPor As String
    Dim rsBaudRate As String
    Dim rsParity$
    Dim rsDataBit$
    Dim rsStopBlt$
    
    
    With MSComm1
    
        sBuf = String(255, 0)
        ' RetVal은 Port의 값이 아닌 "Port"글자의 자릿수를 저장, sBuf에 ini파일 내의 Port 값이 저장됨
        RetVal = GetPrivateProfileString("MSCOMM", "Port", "", sBuf, 255, App.Path & "\Test.ini")           'Text.ini파일에 Port를 가져오는 함수
        
        If RetVal = 0 Then
        Else
            ' sBuf에 들어간 값을 LeftH함수를 통해 좌측에서 RetVal의 자릿수만큼 가져옴
            rsPort = LeftH(sBuf, RetVal)                                                                    'Port를 rsPort에 저장
        End If
        
        rsPor = rsPort
            
        
        sBuf = String(255, 0)
        RetVal = GetPrivateProfileString("MSCOMM", "BaudRate", "", sBuf, 255, App.Path & "\Test.ini")
        
        If RetVal = 0 Then
        Else
            rsPort = LeftH(sBuf, RetVal)
        End If
        
        
        rsBaudRate = rsPort
            
        
        sBuf = String(255, 0)
        RetVal = GetPrivateProfileString("MSCOMM", "Parity", "", sBuf, 255, App.Path & "\Test.ini")
        
        If RetVal = 0 Then
        Else
            rsPort = LeftH(sBuf, RetVal)
        End If
        
        rsParity = rsPort
        
        
        sBuf = String(255, 0)
        RetVal = GetPrivateProfileString("MSCOMM", "DataBit", "", sBuf, 255, App.Path & "\Test.ini")
        
        If RetVal = 0 Then
        Else
            rsPort = LeftH(sBuf, RetVal)
        End If
        
        rsDataBit = rsPort
        
        
        sBuf = String(255, 0)
        RetVal = GetPrivateProfileString("MSCOMM", "StopBIt", "", sBuf, 255, App.Path & "\Test.ini")
        
        If RetVal = 0 Then
        Else
            rsPort = LeftH(sBuf, RetVal)
        End If
        
        rsStopBlt = rsPort
        
        .CommPort = rsPor
        .Settings = rsBaudRate & "," & rsParity & "," & rsDataBit & "," & rsStopBlt
        .RTSEnable = True
        .RThreshold = 1
        .PortOpen = True
    
    End With
    
    ' 하드코딩을 하게되면 매번 설정을 변경해줘야하므로 ini 내용만 바꿔도 작동할 수 있도록 설정
'    With MSComm1
'        .CommPort = 1                                                           '포트설정
'        .Settings = "9600" & "," & "n" & "," & "8" & "," & "1"
'        .RTSEnable = True                                                           '통신할 장비에 요청을 할수있도록 설정
'        .RThreshold = 1                                                             '데이터를 자동으로 수신하도록 설정
'        .PortOpen = True                                                            '포트열기
'    End With
End Sub

 

버튼
Private Sub Command1_Click()
    MSComm1.Output = Text1.Text  'output에 넣어준다     '텍스트를 출력 시킴
End Sub

 

MSComm
Private Sub MSComm1_OnComm()
    Dim sWkDat As String

    Select Case MSComm1.CommEvent '장비내의 이벤트가 발생할 시 결과에 따라 각 케이스로 움직인다. 이때, 이벤트 결과값을 확인할수 있는 방법은 모듈안에 있다.
       ' Events
        Case MSCOMM_EV_SEND     ' There are SThreshold number of
                                ' character in the transmit buffer.
        Case MSCOMM_EV_RECEIVE  ' Received RThreshold # of chars.
            ' MSComm Virtual Machine에서 입력한 값을 input에 넣어 Text2에 출력
            sWkDat = MSComm1.Input
            Text2.Text = sWkDat
            
        Case MSCOMM_EV_CTS      'j
        Case MSCOMM_EV_DSR      ' Change in the DSR line.
        Case MSCOMM_EV_CD       ' Change in the CD line.
        Case MSCOMM_EV_RING     ' Change in the Ring Indicator.
        ' Errors
        Case MSCOMM_ER_BREAK    ' A Break was received.
        ' Code to handle a BREAK goes here, and so on.
        Case MSCOMM_ER_CTSTO    ' CTS Timeout.
        Case MSCOMM_ER_DSRTO    ' DSR Timeout.
        Case MSCOMM_ER_FRAME    ' Framing Error.
        Case MSCOMM_ER_OVERRUN  ' Data Lost.
        Case MSCOMM_ER_CDTO     ' CD (RLSD) Timeout.
        Case MSCOMM_ER_RXOVER   ' Receive buffer overflow.
        Case MSCOMM_ER_RXPARITY ' Parity Error.
        Case MSCOMM_ER_TXFULL   ' Transmit buffer full.
    End Select
End Sub

 

 

모듈
Option Explicit

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

' File: MSCOMM.BAS
'
' Copyright (C) 1996 Microsoft Technologies.
' All rights reserved.
'
'----------------------------------------------------------
'--- MSComm event constants
Public Const MSCOMM_EV_SEND = 1     ' There are SThreshold number of characters in the transmit buffer.
Public Const MSCOMM_EV_RECEIVE = 2  ' Received RThreshold # of chars.
Public Const MSCOMM_EV_CTS = 3      ' Change in the CTS line.
Public Const MSCOMM_EV_DSR = 4      ' Change in the DSR line.
Public Const MSCOMM_EV_CD = 5       ' Change in the CD line.
Public Const MSCOMM_EV_RING = 6     ' Change in the Ring Indicator.
Public Const MSCOMM_EV_EOF = 7      ' An EOF character was found in the input stream.
                            

'--- MSComm error code constants
Public Const MSCOMM_ER_BREAK = 1001     ' A Break was received.
Public Const MSCOMM_ER_CTSTO = 1002     ' CTS Timeout.
Public Const MSCOMM_ER_DSRTO = 1003     ' DSR Timeout.
Public Const MSCOMM_ER_FRAME = 1004     ' Framing Error.
Public Const MSCOMM_ER_OVERRUN = 1006   ' Data Lost.
Public Const MSCOMM_ER_CDTO = 1007      ' CD (RLSD) Timeout.
Public Const MSCOMM_ER_RXOVER = 1008    ' Receive buffer overflow.
Public Const MSCOMM_ER_RXPARITY = 1009  ' Parity Error.
Public Const MSCOMM_ER_TXFULL = 1010    ' Transmit buffer full.
Public Const MSCOMM_ER_DCB = 1011       ' Unexpected error retrieving DCB]

Public Function LeftH(ByVal anystr As String, ByVal nPos As Integer) As String
    LeftH = StrConv(LeftB(StrConv(anystr, vbFromUnicode), nPos), vbUnicode)
End Function

'VB' 카테고리의 다른 글

[VB6.0] Socket Server(소켓 서버)  (0) 2022.05.18
[VB6.0] Socket Client(소켓 클라이언트)  (0) 2022.05.18
[VB.NET] TreeView  (0) 2022.05.13
[VB.NET] ListBox / ListView  (0) 2022.05.13
[VB.NET] Do Loop문  (0) 2022.05.13

댓글