← 목록으로

[VB.NET] 프로그램 로그 기록하기 / RS232 통신

프로그램 로그 기록하기

프로젝트에서 사용한 VB.NET 에서 로그 기록하는
간단한 방법을 소개합니다.

' 로그 기록 함수
Private Sub LogWrite(ByVal log_text As String)
  ' 로그 기록 폴더
  My.Application.Log.DefaultFileLogWriter.CustomLocation = Application.StartupPath & "Log"
  ' 로그 파일 명(프로그램명_날짜)
  My.Application.Log.DefaultFileLogWriter.BaseFileName = "프로그램명" + Now().ToString("yyyy-MM-dd")
  ' 로그 내용 기록
  My.Application.Log.WriteEntry(Format(Today, "yyyy-MM-dd") & " " & Format(TimeOfDay, "HH:mm:ss") & log_text, TraceEventType.Information)
  ' 로그 기록 닫기
   My.Application.Log.DefaultFileLogWriter.Close()
End Sub

try catch 문이나 로그를 기록하고 싶은 위치에
LogWrite(“로그내용”) 을 호출하면 됩니다.

VB.NET 에서 간단하게 시리얼값 받아오기

시리얼 통신, RS232 통신

Imports System.IO.Ports
Imports System.Text
 
Public Class Form1
 
    Public Delegate Sub StringSubPointer(ByVal Buffer As String)
    Dim WithEvents COMPort As New SerialPort
    Dim Received As StringBuilder
 
    Private Sub Display(ByVal Buffer As String)
        ' 값이 수신되면 이쪽으로 온다.
        Received.AppendLine(Buffer)
    End Sub
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '포트 설정
        SerialPort.PortName = "COM3"
        SerialPort.BaudRate = 2400
        SerialPort.Parity = Parity.Even
        SerialPort.StopBits = StopBits.Two
        SerialPort.DataBits = 7
 
        SerialPort.ReadTimeout = 500
 
        Try
            SerialPort.Open()
        Catch ex As Exception
 
        End Try
    End Sub
 
    Private Sub COMPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMPort.DataReceived
        Me.BeginInvoke(New StringSubPointer(AddressOf Display), SerialPort.ReadLine)
    End Sub
End Class