Showing posts with label Visual Basic. Show all posts
Showing posts with label Visual Basic. Show all posts
Monday, January 9, 2017
Creating Dynamic Link Library (DLL) in C++ and Using it in Visual Basic .NET
Visual Basic .NET 2012 (vs11) မှာသုံးဖို့ အတွက် DLL (Dynamic Link Library) တခုကို C++ နဲ့ ဖန်တီးဖို့ နည်းလမ်း ၂ ခု ကို ဆွေးနွေးချင်ပါတယ်။
CLR Class Library
Win32 DLL
Tuesday, February 2, 2010
VB2005 Timers
The .NET Framework ထဲမှာ သုံးစရာ timer အမျိုးမျိုး ရှိပါတယ်။
Windows Forms applications တွေမှာ ဆိုရင် System.Windows.Forms.Timer control ကို သုံးနိုင်ပြီး
user interface မရှိတဲ့ application မှာ ဆိုရင်တော့ System.Threading.Timer class ဒါမှမဟုတ် the System.Timers.Timer class ကို သုံးနိုင်ပါတယ်။
အောက်မှာ ပြထားတဲ့ နမူနာ မှာ System.Threading.Timer class ကို သုံး ပြီး procedure တစ်ခုကို ခေါ်သုံးထား ပါတယ်။ Timer ကို စပြီးတာ နဲ့ သူ့ရဲ့ တန်ဖိုးတွေကို ပြောင်းချင်ရင်
Change method ကိုသုံးပြီးမှ ပြောင်းလို့ ရပါတယ်။ Stop method မရှိပါဘူး။ ရပ်ချင်ရင် Dispose method ကိုသုံးမှ ရပါမယ်။
Imports System.Threading Dim dueTime as New TimeSpan(0,0,1) Dim period as New TimeSpan(0,0,0,0,500) Dim t As New Timer(AddressOf TimerProc, Nothing, dueTime, period) Dim tEn As Boolean = True Private Sub TimerProc(ByVal state As Object) If tEn = True Then 'Do timer things End If End Sub
Friday, September 25, 2009
CRC Calculation in VB and C
CRC -Cyclic Redundancy Check ကို တွက်ထုတ် ဖို့ အတွက် Visual Basic 2005 နဲ့ C ကို သုံးပြီး ရေးထားတဲ့ code တချို့ပါ။
CRC Calculation - GitHub
ဥပမာ အနေနဲ့ CRC16 CCITT Calculation နမူနာ တစ်ခုကို အောက်မှာ ပြထားပါတယ်။
CRC16 CCITT ရဲ့ initial value က 0xFFFF ဖြစ်ပြီး သူ့ ကို အခြား တန်ဖိုး နဲ့ စသုံးချင်ရင်တော့ အောက်ပါအတိုင်း သုံးနိုင်ပါတယ်။
CRC Calculation - GitHub
ဥပမာ အနေနဲ့ CRC16 CCITT Calculation နမူနာ တစ်ခုကို အောက်မှာ ပြထားပါတယ်။
အောက်ကလို Online checksum calculator တွေကလည်း ကုဒ်တွေကို debug လုပ်တဲ့ အခါ အသုံးကျပါတယ်။
Online Checksum Calculator
CRC Calculation in VB2005
CRC Calculation အချို့အတွက် Visual Basic 2005 source code များကို အောက်ပါ လင့်ခ် မှာတွေ့နိုင်ပါတယ်။ Calculation ကို ပိုမြန်စေဖို့ အတွက် table ကို သုံးပြီး တွက်ထားပါတယ်။CRC Calculation - GitHub
ဥပမာ အနေနဲ့ CRC16 CCITT Calculation နမူနာ တစ်ခုကို အောက်မှာ ပြထားပါတယ်။
Dim StrIn as String= "String to calculate CRC" Dim CRCVal16 As UInt16 = 0 Dim crc As String CRCVal16 = CRC16_CCITT.Calculate(StrIn) crc = CRC16_CCITT.ToString(CRCVal16)
CRC16 CCITT ရဲ့ initial value က 0xFFFF ဖြစ်ပြီး သူ့ ကို အခြား တန်ဖိုး နဲ့ စသုံးချင်ရင်တော့ အောက်ပါအတိုင်း သုံးနိုင်ပါတယ်။
CRCVal16 = CRC16_CCITT.Calculate(Str1) CRCVal16 = CRC16_CCITT.Calculate(Str2, CRCVal16) crc = CRC16_CCITT.ToString(CRCVal16)
CRC Calculation in C
CRC Calculation အချို့အတွက် C source code များကို အောက်ပါ လင့်ခ် မှာတွေ့နိုင်ပါတယ်။ Storage သုံးတာ သက်သာအောင် table ကို မသုံးပဲ တွက်ထားပါတယ်။CRC Calculation - GitHub
ဥပမာ အနေနဲ့ CRC16 CCITT Calculation နမူနာ တစ်ခုကို အောက်မှာ ပြထားပါတယ်။
#define STRLEN 4
char str[STRLEN]={0x01,0x01,0x00,0x0B};
unsigned char c[2];
unsigned int crc;
//Calculate CRC16 CCITT
crc=CRC16CCITT_InitialValue();
crc=CRC16CCITT_Calculate(str,STRLEN,crc);
CRC16CCITT_ToString(crc,c);
printf("CRC16 CCITT = %02X %02X \n",c[0],c[1]);
အောက်ကလို Online checksum calculator တွေကလည်း ကုဒ်တွေကို debug လုပ်တဲ့ အခါ အသုံးကျပါတယ်။
Online Checksum Calculator
Monday, March 16, 2009
String and ASCII Code Conversion in VB 2005
ကျွန်တော်တို့ အခါအားလျော်စွာ သုံးဖို့ လိုတဲ့ ASCII code နဲ့ string အပြန် အလှန် ပြောင်းလဲတဲ့ ဖန်ရှင်လေး တွေကို ရေးကြည့်ထား ပါတယ်။
အဲဒါ ကို A Visual Basic class to convert between hexadecimal, ascii, and text string on GitHub မှာ ယူနိုင်ပါတယ်။
'Author: Yan Naing Aye
'WebSite: http://cool-emerald.blogspot.sg/
'Updated: 2009 April 24
'-----------------------------------------------------------------------------
Public Class ClsMyStr
Public Shared Function AsAsciiEncodedStr(ByVal CharString As String) As String
Dim outS As String = ""
Dim temp As String = ""
Dim i As Integer = 0
For i = 0 To CharString.Length - 1
temp = "00" & Hex(Asc(CharString(i)))
temp = Right(temp, 2)
outS = outS & temp
Next i
Return outS
End Function
Public Shared Function AsSpacedAsciiEncodedStr(ByVal CharString As String) As String
Dim outS As String = ""
Dim temp As String = ""
Dim i As Integer = 0
For i = 0 To CharString.Length - 1
temp = "00" & Hex(Asc(CharString(i)))
temp = Right(temp, 2)
outS = outS & temp & " "
Next i
Return outS
End Function
Public Shared Function AsAsciiDecodedStr(ByVal AsciiEncodedStr As String) As String
Dim outS As String = ""
Dim i As Integer = 0
Dim l As Integer = AsciiEncodedStr.Length - 2
If (AsciiEncodedStr.Length Mod 2) <> 0 Then
l -= 1
End If
For i = 0 To l Step 2
outS = outS & Chr(Val("&H" & AsciiEncodedStr.Substring(i, 2)))
Next i
Return outS
End Function
Public Shared Function GetAsciiEncodedStr(ByVal RawAsciiEncodedStr As String) As String
Dim i As Integer = 0
Dim c As String
Dim cmd As String = ""
For i = 0 To RawAsciiEncodedStr.Length - 1
c = RawAsciiEncodedStr(i)
If (Asc(c) >= &H30) AndAlso (Asc(c) <= &H39) Then
cmd = cmd & c
ElseIf (Asc(c) >= &H41) AndAlso (Asc(c) <= &H5A) Then
cmd = cmd & c
ElseIf (Asc(c) >= &H61) AndAlso (Asc(c) <= &H7A) Then
cmd = cmd & Chr(Asc(c) - &H20) 'change to upper case
Else
'MessageBox.Show("Got invalid character.")
End If
Next i
If cmd.Length < 2 Then
cmd = "0" & cmd
End If
Return cmd
End Function
Public Shared Function DoubleQuote() As String
Return ControlChars.Quote
End Function
Public Shared Function Byte2Text(ByVal byteArray() As Byte) As String
Dim str As String = BitConverter.ToString(byteArray)
Return str
End Function
Public Shared Function Byte2Str(ByVal byteArray() As Byte) As String
'Dim str As String = System.Text.Encoding.ASCII.GetString(byteArray)
Dim str As String = ""
For i As Integer = 0 To UBound(byteArray)
str &= Chr(byteArray(i))
Next
Return str
End Function
Public Shared Function Str2Byte(ByVal str As String) As Byte()
'Dim ba() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
Dim ba() As Byte
Try
ReDim ba(str.Length - 1)
For i As Integer = 0 To UBound(ba)
ba(i) = Asc(str(i))
Next
Catch ex As Exception
ReDim ba(0)
ba(0) = 0
End Try
Return ba
End Function
Public Shared Function GetSignedDecimalText(ByVal RawStr As String) As String
Dim i As Integer = 0
Dim c As String
Dim cmd As String = ""
For i = 0 To RawStr.Length - 1
c = RawStr(i)
If (Asc(c) >= &H30) AndAlso (Asc(c) <= &H39) Then
cmd = cmd & c
ElseIf (Asc(c) = &H2D) Then
If cmd.Length = 0 Then
cmd = cmd & c
End If
Else
'MessageBox.Show("Got invalid character.")
End If
Next i
Return cmd
End Function
End Class
Sunday, February 22, 2009
Reading and Writing a File in VB 2005
တစ်ခါ တလေ Visual Basic 2005 ကိုသုံးပြီး ဖိုင်တွေရေးဖို့ ဖတ်ဖို့ လိုတတ်ပါတယ်။ အဲဒီ အခါ အလွယ်တကူ ကိုးကားလို့ရအောင် တစ်ချို့ ကုဒ်လေးတွေ ကို တင်လိုက်ပါတယ်။ အောက်က နမူနာ နှစ်ခုကတော့ ဒီနေရာက နေ ကိုးကားထားတာပါ။
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Boot.ini")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Dim objStreamWriter As StreamWriter
'Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = New StreamWriter("C:\Testfile.txt")
'Write a line of text.
objStreamWriter.WriteLine("Hello World")
'Write a second line of text.
objStreamWriter.WriteLine("From the StreamWriter class")
'Close the file.
objStreamWriter.Close()
ဖိုင်ကိုသုံးနေတုန်း error တက်ခဲ့ရင် တော့ သုံးလက်စ ဖိုင်ကို မပိတ်နိုင်ပဲ ထွက်သွားတာမျိုး ဖြစ်နိုင်ပါတယ်။ အဲဒါကို တားဆီးဖို့ ကတော့ Try ... Finally ကိုအောက်ပါအတိုင်း သုံးနိုင်ပါတယ်။
Dim strLine As String
Dim sr As StreamReader = Nothing
Try
sr = New StreamReader(fileName)
strLine = sr.ReadToEnd()
Finally
sr.Close()
End Try
Visual Basic 2005 မှာ Using ဆိုတဲ့ statement အသစ် က IDisposable object တွေကို အလိုအလျောက် release လုပ်ပေးနိုင်ပါတယ်။ Exception တစ်ခုခု ဖြစ်ရင် သူ့ရဲ့ caller ဆီကိုရောက်သွားမှာပါ။ အကယ်၍ အဲဒီ exception ကို ကိုယ့်ဟာကိုယ် catch လုပ်ချင်ရင်တော့ Try ... Catch ... Finally block ကိုပဲ သုံးရမှာပါ။
Dim strLine As String
Using sr As New StreamReader(fileName)
strLine = sr.ReadToEnd
End Using
ဖိုင်ဖတ်တဲ့ အခါ ပိုပြီးလွယ်ကူ ရှင်းလင်း ချင်ရင် File.ReadAllText or File.ReadAllBytes static method ကိုသုံးလို့လဲ ရပါတယ်။
Log File
Log files တွေ အလွယ်တကူ ရေးဖို့ class တစ်ခု ကို ရေးကြည့် ထားပါတယ်။ သူ့ရဲ့ ကုဒ်တွေကို VB class for log file on GitHub ကနေ ယူလို့ သုံးလို့ ရပါတယ်။
'Author: Yan Naing Aye
'WebSite: http://cool-emerald.blogspot.sg/
'Updated: 2009 June 25
'-----------------------------------------------------------------------------
Imports System.IO
Public Class ClsLog
Private mEnableLog As Boolean = False
Private mLogFileDirectory As String = ""
Private mLogFilePath As String = ""
Private mLogFileLifeSpan As Integer = 0
Private mLogFileFirstName As String = "AppName"
Public Sub New()
mEnableLog = False
mLogFileDirectory = My.Application.Info.DirectoryPath
mLogFileLifeSpan = 0
mLogFileFirstName = My.Application.Info.AssemblyName
End Sub
Public Property LogFileLifeSpan() As Integer
Get
Return mLogFileLifeSpan
End Get
Set(ByVal value As Integer)
mLogFileLifeSpan = IIf(value >= 0, value, 0)
End Set
End Property
Public Property LogFileFirstName() As String
Get
Return mLogFileFirstName
End Get
Set(ByVal value As String)
mLogFileFirstName = value
End Set
End Property
Public Property LogEnable() As Boolean
Get
Return mEnableLog
End Get
Set(ByVal value As Boolean)
If value = True Then
If Directory.Exists(Me.LogFileDirectory) Then
mEnableLog = value
Else
mEnableLog = False
Throw New Exception("Invalid file directory.")
End If
Else
mEnableLog = value
End If
End Set
End Property
Public Property LogFileDirectory() As String
Get
Return mLogFileDirectory
End Get
Set(ByVal value As String)
value = Trim(value)
If Directory.Exists(value) Then
Dim i As Integer = value.Length - 1
If (((value(i)) = "\") OrElse ((value(i)) = "/")) Then
value = value.Substring(0, i)
End If
mLogFileDirectory = value
Else
Throw New Exception("Invalid file directory.")
End If
End Set
End Property
Public ReadOnly Property LogFilePath() As String
Get
Return mLogFileDirectory & "\" & mLogFileFirstName & Format(Now, "-yyyy-MMM-dd") & ".log"
End Get
End Property
Public Sub WriteLog(ByVal LogEntry As String)
If mEnableLog = True Then
mLogFilePath = mLogFileDirectory & "\" & mLogFileFirstName & Format(Now, "-yyyy-MMM-dd") & ".log"
Dim objStreamWriter As StreamWriter = New StreamWriter(mLogFilePath, True)
Try
objStreamWriter.WriteLine(LogEntry)
Catch ex As Exception
Finally
objStreamWriter.Close()
End Try
End If
End Sub
Public Sub WriteTimeAndLog(ByVal LogEntry As String)
WriteLog(Now.ToLongTimeString & " " & LogEntry)
End Sub
Public Sub CleanupFiles()
If mLogFileLifeSpan > 0 Then 'if life span is zero, there will be no cleaning up
Try
Dim LogFiles() As String = Directory.GetFiles(mLogFileDirectory)
For Each LogFile As String In LogFiles
If (DateDiff("d", File.GetLastWriteTime(LogFile), Now) > mLogFileLifeSpan) _
AndAlso (Right(LogFile, 4) = ".log") Then
File.Delete(LogFile)
End If
Next
Catch ex As Exception
End Try
End If
End Sub
End Class
Tuesday, January 20, 2009
Using Notify Icon in Visual Basic 2005
Notify icon ကို system tray မှာ ပေါ်ဖို့ အတွက် VB2005 မှာရေးရတာ VB6 ထက် အများကြီးပိုလွယ်ပါတယ်။ VB2005 မှာ Windows Application တစ်ခုကို ဖန်တီးပါ။ ထို့နောက် အောက်ပါပုံတွင် ပြထားသည့် အတိုင်း NotifyIcon control ကို ထည့်ပါ။
"NotifyIcon1" control ကို Select မှတ်ပါ။ ထို့နောက် သူ့ရဲ့ properties window မှာ "Icon" ကိုရွေးပြီး သင်သုံးချင်တဲ့ icon ကို သတ်မှတ်ပါ။ ဘာ icon မှ သတ်မှတ်မပေးခဲ့ရင် notify icon ကို system tray မှာ မြင်ရမှာ မဟုတ်ပါဘူး။
သင့် application ကို ပိတ်တဲ့ အခါ notify icon ကို ပေါ်စေဖို့ အတွက် အောက်ပါ code များကို FormClosing event တွင်ရေးပါ။
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Do you really want to exit?", "Exit", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
e.Cancel = True
Me.Visible = False
NotifyIcon1.Visible = True
End If
End Sub
Notify Icon ကို double click နှိပ်လိုက်ရင် application ကို ပြန်ပေါ်လာစေဖို့ အောက်ပါ code များကို NotifyIcon1 ၏ MouseDoubleClick event တွင်ရေးပါ။
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.Visible = True ' Show the form.
Me.Activate() ' Activate the form.
NotifyIcon1.Visible = False
End Sub
File menu နဲ့ context menu တွေကနေ application ပိတ်လို့ရစေဖို့ အောက်ပါ controls များကိုထည့်ပါ။
ထို့နောက် 'Me.Close()' ကို သူတို့ရဲ့ click event မှာဖြည့်ပါ။
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
' Close the form, which closes the application.
Me.Close()
End Sub
Private Sub ContextMenuStrip1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.Click
Me.Close()
End Sub
Notify Icon ပေါ်မှာ right click နှိပ်တဲ့အခါ pop up menu ပေါ်စေဖို့ NotifyIcon1 control ကို Select မှတ်ပြီး သူ့ရဲ့ ContestMenuStrip property မှာ ContextMenuStrip1 ကိုသတ်မှတ်ပေးပါ။
ပြီးရင် သင့် program ကို run နိုင်ပါပြီ။ ဝင်းဒိုး ကို ပိတ်လိုက်ရင် တစ်ကယ် ထွက်မထွက် မေးဖို့ message box တစ်ခုပေါ်လာမှာ ဖြစ်ပြီး "No" ကိုရွေးရင် system tray ထဲမှာ ကျန်နေပါလိမ့်မယ်။ Notify Icon ကို double click ပြန်နှိပ်ရင် application ပြန်ပေါ်လာမှာဖြစ်ပါတယ်။
Wednesday, October 22, 2008
Sending Email in VB2005
အီးမေး ပို့တဲ့ ပရိုဂရမ်လေးကို VB2005 နဲ့ နမူနာရေးကြည့်ထားတာပါ။ ဒီနမူနာမှာ gmail ကိုသုံးထားတဲ့အတွက် စမ်းကြည့်ဖို့အတွက် gmail account တစ်ခုတော့လိုပါလိမ့်မယ်။
---------------------------------------------------
Imports System.Net.Mail Public Sub SendMail() Dim mail As New MailMessage() 'set the addresses mail.From = New MailAddress("yourname@gmail.com") mail.To.Add("DestinationAddress@gmail.com") 'set the content mail.Subject = "Sample Subject" mail.Body = "This is a body" 'send the message Dim smtp As New SmtpClient("smtp.gmail.com", 587) smtp.Credentials = New System.Net.NetworkCredential("yourname@gmail.com", "yourpassword") smtp.EnableSsl = True Try smtp.Send(mail) Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub
---------------------------------------------------
Wednesday, April 23, 2008
Using PC Serial Port
ကွန်ပျူတာရဲ့ serial port ကို Visual Basic programming သုံးပြီး ထိန်းချုပ် တဲ့အကြောင်းပြောမှာပါ။ ကွန်ပျူတာတွေမှာ serial port တစ်ခုတော့ ပါလေ့ရှိပါတယ်။ မပါခဲ့ရင်လဲ USB ကနေ RS232 converter လေးတွေ ကို အလွယ်တကူ ဝယ်လို့ရပါတယ်။
ဒီမှာ Serial port ကို သုံးတာနဲ့ ပတ်သက်ပြီး
1. Using VB6
2. Using VB2005 (VB.NET2)
3. Serial Port as IO
ဆိုပြီး သုံးပိုင်း ခွဲပြောထားပါတယ်။
VB6 မှာ ဆိုရင် serial port ကို အသုံးပြုဖို့အတွက် MSComm ဆိုတဲ့ ActiveX contorl ကိုသုံးမှာပါ။ စစချင်း VB6 မှာ Standard Exe ကိုရွေးပြီး New project တစ်ခုဖန်တီးပါ။ ပြီးရင် Project Menu က Components ဆိုတဲ့ command ကိုနှိပ်ပြီးရင် တက်လာတဲ့ Components dialog မှာ Microsoft Comm Control 6.0 ဆိုတာကိုရွေးပါ။
OK နှိပ်ပြီးရင် တယ်လီဖုန်း icon နဲ့ MSComm control ကို Toolbox ထဲမှာ တွေ့ရမှာ ဖြစ်ပါတယ်။ ပြီးရင် အဲဒီ control ပေါ်မှာ double click နှိပ်ပြီး Form ပေါ်ကိုထည့်ပါ။ အောက်ကပုံမှာ ပြထားတဲ့ အတိုင်း ထည့်ပြီးတဲ့အခါ ဒီ control ရဲ့ နာမည်က MSComm1 ဆိုပြီး ဖြစ်နေမှာဖြစ်ပြီး ဘေးအောက်နားက Properties box ထဲမှာ သူ့ရဲ့ setting တွေကို ကြိုက်သလိုပြင်လို့ရပါတယ်။
ဒီနမူနာ အနေနဲ့ ကတော့ ဒီအတိုင်းထားပြီး settings တွေကို Form ရဲ့ လွတ်တဲ့ တစ်နေရာရာမှာ double click နှိပ်ပြီး ပေါ်လာမယ့် Form_Load() ဆိုတဲ့ event မှာပဲ အောက်မှာ အတိုင်းသွားပြင်ပါမယ်။
Settings ထဲကိုထည့်လိုက်တဲ့ "9600,N,8,1" ဆိုတာက Baud rate ကို 9600 သုံးပြီး၊ No parity, 8 data bit and 1 stop bit လို့ဆိုလိုတာပါ။ RThreshold ကတော့ receive buffer ထဲကို data byte ဘယ်နှစ်လုံးရောက်ရင် လက်ခံရရှိကြောင်း Event ကို trigger လုပ်ရမလဲဆိုတာ ကိုသတ်မှတ်ပါတယ်။ ဒီနမူနာ မှာတော့ တစ်လုံးရောက်လာရင်တောင် Receive Event ကို ခေါ်ခိုင်းထားပါတယ်။ ပြီးရင် PC ရဲ့ Comm port 1 ကို အသုံးပြုထားပါတယ်။ ကြိုက်တဲ့ Comm port ကို သုံးနိုင်ပါတယ်။ ပြီးရင် Comm port ကို ဖွင့်ပါတယ်။ နောက်တစ်ခုကတော့ လက်ခံရရှိတဲ့ ဒေတာ တွေကို ဖော်ပြဖို့ Textbox ကို clear လုပ်ထားတာပါ။ ပြီးရင် data အစမ်းပို့ကြည့်ဖို့ command button တစ်ခုနဲ့ textbox တစ်ခုကို form ပေါ်မှာထည့်ပါ။ Command button ရဲ့ Caption ကို 'Send' လို့ ပြင်ပေးလိုက်ပါမယ်။
ပြီးရင် အဲဒီ button ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ click ဆိုတဲ့ event function မှာ အောက်ပါအတိုင်းရေးပါမယ်။
ဒါဆိုရင် button ကိုနှိပ်တိုင်း ABCD ဆိုတဲ့ data ကို serial port ကနေ ပို့ပေးမှာပါ။ ABCD အစား ကိုယ်ပို့ချင်တဲ့ data ကို MSComm1.Output ထဲကို ထည့်ပေးလို့ ရပါတယ်။ နောက် data ကို လက်ခံဖို့ အတွက်ကတော့ MSComm control ရဲ့ OnComm ဆိုတဲ့ Event ကို သုံးပါမယ်။ MSComm control ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ event function ထဲမှာ အောက်ပါအတိုင်းရေးပါမယ်။ OnComm Event အမျိုးမျိုး ရှိတဲ့ အထဲက data ကို လက်ခံရရှိခဲ့ ရင် Textbox ထဲကို သွားပေါင်းထည့်ပြီး ဖော်ပြပေးမှာပါ။
အဲဒါဆိုရင် program က အောက်မှာ ပြထားတဲ့အတိုင်းဖြစ်သွားပါပြီ။
ပြီးရင် program ကို run ကြည့်ပါ။ Send button ကို နှိပ်တိုင်း ပို့ပေးမှာ ဖြစ်ပြီး၊ လက်ခံရရှိတဲ့ data တွေကို textbox ထဲမှာ ဖော်ပြပေးမှာ ဖြစ်ပါတယ်။
VB2005 မှာဆိုရင် တော့ Serial port ကို သုံးရတာ ပိုပြီးလွယ်ကူ သွားပါတယ်။ ဘာဖြစ်လို့လဲဆိုတော့ သူ့မှာ serial port ကိုသုံးဖို့ SerialPort ဆိုတဲ့ control ပါလာပြီးသားမို့ပါ။ စစချင်း File Menu ထဲက New Project command ကိုရွေးပြီး Windows Application အသစ်တစ်ခုကို ဖန်တီးပါ။ ပြီးရင် Toolbox ထဲက SerialPort ဆိုတဲ့ Control ကို double click နှိပ်ပြီး ထည့်ပါ။
ပြီးရင် control ကို select မှတ်ပြီး ညာဖက်အောက်နားက properties box မှာ သူ့ရဲ့ properties တွေကို ပြင်ပါမယ်။ နာမည်ကတော့ ဒီအတိုင်း SerialPort1 လို့ပဲထားပါမယ်။ ဒီနမူနာမှာ BaudRates ကို 9600, Databits=8, Parity=None, StopBits= One လို့ထားပါမယ်။ ကိုယ်သုံးမယ့် အပေါ်မူတည်ပြီး settings တွေကို ထားနိုင်ပါတယ်။ ပြီးရင် ReceivedBytesThreshold ကိုလဲ နဂို တန်ဖိုးအတိုင်း 1 လို့ထားပါမယ်။ PC ရဲ့ COM1 ကို သုံးမှာ ဖြစ်တဲ့ အတွက် PortName ကို COM1 လို့ထားပါမယ်။
ပြီးရင် Form ပေါ်မှာ double click နှိပ်ပြီး Form Load Event မှာ အောက်ပါအတိုင်းရေးပါမယ်။
ဒါဆိုရင် ပရိုဂရမ် စစချင်းမှာ port ကို ဖွင့်ပြီးသားဖြစ်သွားပါမယ်။ ပုံမှန် serial port control ရဲ့ Encoding က ASCII ဖြစ်တာကြောင့် ကိုယ်သုံးချင်တဲ့ပေါ်မူတည်ပြီး Encoding ကို Form Load Event မှာ ပြင်နိုင်ပါတယ်။
ပြီးရင် ပို့ဖို့ Button တစ်ခုနဲ့၊ လက်ခံရရှိတာတွေကို ဖော်ပြဖို့ TextBox တစ်ခုထည့်ပါမယ်။ Button ရဲ့ Text ကို Send လို့ပြင်ပါမယ်။ Button ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ click event မှာ အောက်ပါအတိုင်းရေးပါမယ်။
ဒါဆိုရင် အဲဒီ Send ဆိုတဲ့ button ကို နှိပ်တိုင်း ABCD ဆိုတဲ့ ဒေတာကို ပို့ပေးမှာပါ။ အဲဒီ ABCD အစား ကိုယ် ပို့ချင်တဲ့ ဒေတာ ကို အစားထိုးနိုင်ပါတယ်။ နောက် data လက်ခံဖို့ အတွက်ကတော့ SerialPort1 ရဲ့ DataReceived event မှာ အောက်ပါအတိုင်းသွားရေးပါမယ်။
လက်ခံရရှိတဲ့ ဒေတာတွေကို တစ်လုံးချင်း ဖတ်ချင်တယ် ဆိုရင်လဲ အဲဒီအစား အောက်က ပုံစံလဲ ရေးလို့ရပါတယ်။
အဲလိုရေးပြီးတဲ့အခါ ပရိုဂရမ်က အောက်ပါအတိုင်းဖြစ်သွားပါမယ်။
ပြီးရင် ပရိုဂရမ်ကို run ပြီး ဒေတာ ပို့တာ၊ လက်ခံတာ စမ်းသပ်လို့ရပါပြီ။
Sending Binary Data
အထက်မှာ ပြောခဲ့တာက character တွေပို့ဖို့အတွက်ပါ။ ဥပမာ ASCII code number 127 ထက်ကြီးတဲ့ binary code (128-255) တွေ အတွက်ကတော့ အောက်မှာပြထားတဲ့ အတိုင်းပို့နိုင်ပါတယ်။
သူတို့ကိုလက်ခံဖို့အတွက်ကလဲ ReadChar နဲ့ဖတ်လို့မရပါဘူး။ အဲဒီအစား ReadByte ကိုသုံးနိုင်ပါတယ်။ နမူနာ အနေနဲ့ အောက်မှာကြည့်နိုင်ပါတယ်။
Getting Port List in Your PC
သင့် PC မှာရှိတဲ့ port တွေကို Combo box တစ်ခုထဲမှာပြချင်ရင်၊ ပြီးတော့ သင့် setting ထဲမှာသိမ်းထားတဲ့ နံပါတ်ကို ပြနေစေချင်ရင် အောက်ပါအတိုင်းရေးပါမယ်။
အမှန်တော့ serial port မှာ data ပို့တဲ့၊ လက်ခံတဲ့ Tx, Rx အပြင် တခြား control output နှစ်လိုင်းနဲ့ status input လေးလိုင်းကို သုံးလို့ရပါသေးတယ်။ RTS, DTR တွေက output အဖြစ်သုံးနိုင်ပြီး၊ CTS, DSR, CD, RI တို့ကို input အဖြစ်သုံးနိုင်ပါတယ်။ IO အများကြီး မလိုတဲ့ application တွေမှာ serial port ကို IO interface အဖြစ်သုံးရင် အရမ်းလွယ်ကူ အဆင်ပြေပါတယ်။
Related post:
ဒီမှာ Serial port ကို သုံးတာနဲ့ ပတ်သက်ပြီး
1. Using VB6
2. Using VB2005 (VB.NET2)
3. Serial Port as IO
ဆိုပြီး သုံးပိုင်း ခွဲပြောထားပါတယ်။
Using VB6
VB6 မှာ ဆိုရင် serial port ကို အသုံးပြုဖို့အတွက် MSComm ဆိုတဲ့ ActiveX contorl ကိုသုံးမှာပါ။ စစချင်း VB6 မှာ Standard Exe ကိုရွေးပြီး New project တစ်ခုဖန်တီးပါ။ ပြီးရင် Project Menu က Components ဆိုတဲ့ command ကိုနှိပ်ပြီးရင် တက်လာတဲ့ Components dialog မှာ Microsoft Comm Control 6.0 ဆိုတာကိုရွေးပါ။
OK နှိပ်ပြီးရင် တယ်လီဖုန်း icon နဲ့ MSComm control ကို Toolbox ထဲမှာ တွေ့ရမှာ ဖြစ်ပါတယ်။ ပြီးရင် အဲဒီ control ပေါ်မှာ double click နှိပ်ပြီး Form ပေါ်ကိုထည့်ပါ။ အောက်ကပုံမှာ ပြထားတဲ့ အတိုင်း ထည့်ပြီးတဲ့အခါ ဒီ control ရဲ့ နာမည်က MSComm1 ဆိုပြီး ဖြစ်နေမှာဖြစ်ပြီး ဘေးအောက်နားက Properties box ထဲမှာ သူ့ရဲ့ setting တွေကို ကြိုက်သလိုပြင်လို့ရပါတယ်။
ဒီနမူနာ အနေနဲ့ ကတော့ ဒီအတိုင်းထားပြီး settings တွေကို Form ရဲ့ လွတ်တဲ့ တစ်နေရာရာမှာ double click နှိပ်ပြီး ပေါ်လာမယ့် Form_Load() ဆိုတဲ့ event မှာပဲ အောက်မှာ အတိုင်းသွားပြင်ပါမယ်။
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
Text1.Text = ""
End Sub
Settings ထဲကိုထည့်လိုက်တဲ့ "9600,N,8,1" ဆိုတာက Baud rate ကို 9600 သုံးပြီး၊ No parity, 8 data bit and 1 stop bit လို့ဆိုလိုတာပါ။ RThreshold ကတော့ receive buffer ထဲကို data byte ဘယ်နှစ်လုံးရောက်ရင် လက်ခံရရှိကြောင်း Event ကို trigger လုပ်ရမလဲဆိုတာ ကိုသတ်မှတ်ပါတယ်။ ဒီနမူနာ မှာတော့ တစ်လုံးရောက်လာရင်တောင် Receive Event ကို ခေါ်ခိုင်းထားပါတယ်။ ပြီးရင် PC ရဲ့ Comm port 1 ကို အသုံးပြုထားပါတယ်။ ကြိုက်တဲ့ Comm port ကို သုံးနိုင်ပါတယ်။ ပြီးရင် Comm port ကို ဖွင့်ပါတယ်။ နောက်တစ်ခုကတော့ လက်ခံရရှိတဲ့ ဒေတာ တွေကို ဖော်ပြဖို့ Textbox ကို clear လုပ်ထားတာပါ။ ပြီးရင် data အစမ်းပို့ကြည့်ဖို့ command button တစ်ခုနဲ့ textbox တစ်ခုကို form ပေါ်မှာထည့်ပါ။ Command button ရဲ့ Caption ကို 'Send' လို့ ပြင်ပေးလိုက်ပါမယ်။
ပြီးရင် အဲဒီ button ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ click ဆိုတဲ့ event function မှာ အောက်ပါအတိုင်းရေးပါမယ်။
Private Sub Command1_Click()
MSComm1.Output = "ABCD"
End Sub
ဒါဆိုရင် button ကိုနှိပ်တိုင်း ABCD ဆိုတဲ့ data ကို serial port ကနေ ပို့ပေးမှာပါ။ ABCD အစား ကိုယ်ပို့ချင်တဲ့ data ကို MSComm1.Output ထဲကို ထည့်ပေးလို့ ရပါတယ်။ နောက် data ကို လက်ခံဖို့ အတွက်ကတော့ MSComm control ရဲ့ OnComm ဆိုတဲ့ Event ကို သုံးပါမယ်။ MSComm control ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ event function ထဲမှာ အောက်ပါအတိုင်းရေးပါမယ်။ OnComm Event အမျိုးမျိုး ရှိတဲ့ အထဲက data ကို လက်ခံရရှိခဲ့ ရင် Textbox ထဲကို သွားပေါင်းထည့်ပြီး ဖော်ပြပေးမှာပါ။
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then
Text1.Text = Text1.Text & MSComm1.Input
End If
End Sub
အဲဒါဆိုရင် program က အောက်မှာ ပြထားတဲ့အတိုင်းဖြစ်သွားပါပြီ။
ပြီးရင် program ကို run ကြည့်ပါ။ Send button ကို နှိပ်တိုင်း ပို့ပေးမှာ ဖြစ်ပြီး၊ လက်ခံရရှိတဲ့ data တွေကို textbox ထဲမှာ ဖော်ပြပေးမှာ ဖြစ်ပါတယ်။
Using VB2005
VB2005 မှာဆိုရင် တော့ Serial port ကို သုံးရတာ ပိုပြီးလွယ်ကူ သွားပါတယ်။ ဘာဖြစ်လို့လဲဆိုတော့ သူ့မှာ serial port ကိုသုံးဖို့ SerialPort ဆိုတဲ့ control ပါလာပြီးသားမို့ပါ။ စစချင်း File Menu ထဲက New Project command ကိုရွေးပြီး Windows Application အသစ်တစ်ခုကို ဖန်တီးပါ။ ပြီးရင် Toolbox ထဲက SerialPort ဆိုတဲ့ Control ကို double click နှိပ်ပြီး ထည့်ပါ။
ပြီးရင် control ကို select မှတ်ပြီး ညာဖက်အောက်နားက properties box မှာ သူ့ရဲ့ properties တွေကို ပြင်ပါမယ်။ နာမည်ကတော့ ဒီအတိုင်း SerialPort1 လို့ပဲထားပါမယ်။ ဒီနမူနာမှာ BaudRates ကို 9600, Databits=8, Parity=None, StopBits= One လို့ထားပါမယ်။ ကိုယ်သုံးမယ့် အပေါ်မူတည်ပြီး settings တွေကို ထားနိုင်ပါတယ်။ ပြီးရင် ReceivedBytesThreshold ကိုလဲ နဂို တန်ဖိုးအတိုင်း 1 လို့ထားပါမယ်။ PC ရဲ့ COM1 ကို သုံးမှာ ဖြစ်တဲ့ အတွက် PortName ကို COM1 လို့ထားပါမယ်။
ပြီးရင် Form ပေါ်မှာ double click နှိပ်ပြီး Form Load Event မှာ အောက်ပါအတိုင်းရေးပါမယ်။
CheckForIllegalCrossThreadCalls = False
SerialPort1.Open()
ဒါဆိုရင် ပရိုဂရမ် စစချင်းမှာ port ကို ဖွင့်ပြီးသားဖြစ်သွားပါမယ်။ ပုံမှန် serial port control ရဲ့ Encoding က ASCII ဖြစ်တာကြောင့် ကိုယ်သုံးချင်တဲ့ပေါ်မူတည်ပြီး Encoding ကို Form Load Event မှာ ပြင်နိုင်ပါတယ်။
SerialPort1.Encoding = System.Text.Encoding.Default
ပြီးရင် ပို့ဖို့ Button တစ်ခုနဲ့၊ လက်ခံရရှိတာတွေကို ဖော်ပြဖို့ TextBox တစ်ခုထည့်ပါမယ်။ Button ရဲ့ Text ကို Send လို့ပြင်ပါမယ်။ Button ပေါ်မှာ double click နှိပ်ပြီး သူ့ရဲ့ click event မှာ အောက်ပါအတိုင်းရေးပါမယ်။
SerialPort1.Write("ABCD")
ဒါဆိုရင် အဲဒီ Send ဆိုတဲ့ button ကို နှိပ်တိုင်း ABCD ဆိုတဲ့ ဒေတာကို ပို့ပေးမှာပါ။ အဲဒီ ABCD အစား ကိုယ် ပို့ချင်တဲ့ ဒေတာ ကို အစားထိုးနိုင်ပါတယ်။ နောက် data လက်ခံဖို့ အတွက်ကတော့ SerialPort1 ရဲ့ DataReceived event မှာ အောက်ပါအတိုင်းသွားရေးပါမယ်။
TextBox1.Text &= SerialPort1.ReadExisting()
လက်ခံရရှိတဲ့ ဒေတာတွေကို တစ်လုံးချင်း ဖတ်ချင်တယ် ဆိုရင်လဲ အဲဒီအစား အောက်က ပုံစံလဲ ရေးလို့ရပါတယ်။
Dim n As Int32
Dim i As Int32
Dim cmd As String
Dim c As String
n = SerialPort1.BytesToRead
For i = 1 To n
c = Chr(SerialPort1.ReadChar())
cmd &= c
Next
TextBox1.Text &= cmd
အဲလိုရေးပြီးတဲ့အခါ ပရိုဂရမ်က အောက်ပါအတိုင်းဖြစ်သွားပါမယ်။
ပြီးရင် ပရိုဂရမ်ကို run ပြီး ဒေတာ ပို့တာ၊ လက်ခံတာ စမ်းသပ်လို့ရပါပြီ။
Sending Binary Dataအထက်မှာ ပြောခဲ့တာက character တွေပို့ဖို့အတွက်ပါ။ ဥပမာ ASCII code number 127 ထက်ကြီးတဲ့ binary code (128-255) တွေ အတွက်ကတော့ အောက်မှာပြထားတဲ့ အတိုင်းပို့နိုင်ပါတယ်။
Dim a(3) As Byte
a(0) = &H41
a(1) = &H31
a(2) = &HFF
a(3) = &H80
SerialPort1.Write(a, 0, 4)
သူတို့ကိုလက်ခံဖို့အတွက်ကလဲ ReadChar နဲ့ဖတ်လို့မရပါဘူး။ အဲဒီအစား ReadByte ကိုသုံးနိုင်ပါတယ်။ နမူနာ အနေနဲ့ အောက်မှာကြည့်နိုင်ပါတယ်။
Dim n As Int32
Dim i As Int32
Dim cmd As String = ""
Dim c As String
n = SerialPort1.BytesToRead
For i = 1 To n
c = Chr(SerialPort1.ReadByte())
cmd &= c
Next
TextBox1.Text &= cmd
Getting Port List in Your PC
သင့် PC မှာရှိတဲ့ port တွေကို Combo box တစ်ခုထဲမှာပြချင်ရင်၊ ပြီးတော့ သင့် setting ထဲမှာသိမ်းထားတဲ့ နံပါတ်ကို ပြနေစေချင်ရင် အောက်ပါအတိုင်းရေးပါမယ်။
Dim ports As String() = SerialPort.GetPortNames()
Dim port As String
Array.Sort(ports)
cmbSerial.Items.Clear()
For Each port In ports
cmbSerial.Items.Add(port)
Next port
Dim index As Integer
index = cmbSerial.FindString(My.Settings.MCOM)
cmbSerial.SelectedIndex = index
Using Serial Port as IO
အမှန်တော့ serial port မှာ data ပို့တဲ့၊ လက်ခံတဲ့ Tx, Rx အပြင် တခြား control output နှစ်လိုင်းနဲ့ status input လေးလိုင်းကို သုံးလို့ရပါသေးတယ်။ RTS, DTR တွေက output အဖြစ်သုံးနိုင်ပြီး၊ CTS, DSR, CD, RI တို့ကို input အဖြစ်သုံးနိုင်ပါတယ်။ IO အများကြီး မလိုတဲ့ application တွေမှာ serial port ကို IO interface အဖြစ်သုံးရင် အရမ်းလွယ်ကူ အဆင်ပြေပါတယ်။
Related post:
Tuesday, April 8, 2008
Parallel Port as IO
ဒစ်ဂျစ်တယ် device တွေကိုထိန်းဖို့အတွက်၊ ရိုးရှင်းတဲ့ ပရောဂျက်တွေအတွက် I/O တွေအနေနဲ့ ကွန်ပျူတာရဲ့ Parallel Port ကို အလွယ်တကူ သုံးလို့ရပါတယ်။
တချို့ စစ်စတမ် တွေမှာလဲ Industry PC တွေကိုသုံးပြီး Parallel Port နဲ့ အယ်စီဒီ မော်ဂျူး လေးတွေကို ထိန်းတာမျိုးလဲ တွေ့ဖူးပါတယ်။ တကယ်တော့ Parallel Port မှာ I/O အများကြီးပါတဲ့ အတွက် Relay တွေ၊ Transistor တွေနဲ့ တွဲ သုံးပြီး မော်တာတွေ၊ မီးသီးတွေ စသည်ဖြင့် တော်တော်များများ ထိန်းလို့ရပါတယ်။ ကျွန်တော်တော့ Parallel Port တစ်ခုထဲကို သုံးပြီး သူငယ်ချင်းတစ်ယောက်ရဲ့ ကံစမ်းမဲ ဖောက်တဲ့ မီးပြေးဘုတ်ကြီးက မီးသီး ၂၄ လုံးကို Solid State Relays တွေနဲ့ ထိန်းဖူးပါတယ်။ Parallel Port တစ်ခုမှာ အများဆုံး output က ၁၂ ခု ( data ၈ ခု၊ control line ၄ ခု)ထိပဲ ထုတ်လို့ရပေမယ့် demultiplex လုပ်ပြီး လိုအပ်ချက် အလိုက် ပြောင်းသုံးတာပါ။ Input ကတော့ ၅ ခုပါ ပါတယ်။ Data line ၈ ခုကိုပါ (နောက်ပိုင်း parallel port တွေမှာ) input အနေနဲ့ သုံးလို့ရမယ်ဆိုရင်တော့ စုစုပေါင်း input ၁၃ ခုထိ ရနိုင်ပါတယ်။ Parallel Port မှာ Mode ငါးခုရှိပြီး အသေးစိတ်ကို ဒီမှာ ကြည့်နိုင်ပါတယ်။ ကွန်ပျူတာ တစ်ခုမှာ Parallel Port တစ်ခုတော့ ပါတတ်ပါတယ်... အကယ်၍ မပါခဲ့ရင်လဲ Parallel Port ပါတဲ့ ကဒ်တွေကို အလွယ်တကူနဲ့ ဝယ်စိုက်လို့ရပါတယ်။
Parallel Port တစ်ခုမှာ ရှိတဲ့ ဝါယာလိုင်းတွေကို
၁။ Data လိုင်းများ
၂။ Control လိုင်းများ
၃။ Status လိုင်းများ
ဆိုပြီး သုံးမျိုးခွဲခြားနိုင်ပါတယ်။ Data လိုင်းတွေကို Data register က ထိန်းချုပ်နိုင်၊ ဖော်ပြနိုင် ပြီးတော့ .... Control လိုင်းတွေကို Control register က ထိန်းချုပ်ပါတယ်။ Status လိုင်းတွေ မှာ ရှိတဲ့ တန်းဖိုးတွေကိုတော့ Status register မှာ ဖတ်ရှုနိုင်ပါတယ်။ သူတို့ရဲ့ ဆက်သွယ်မှုကို အောက်ကပုံမှာ ကြည့်နိုင်ပါတယ်။
Control lines တွေကို output အနေနဲ့ သုံးလို့ရပြီး၊ Status lines တွေကိုတော့ input အနေနဲ့သုံးလို့ရပါတယ်။ Data lines တွေကတော့ အဝင်၊ အထွက် နှစ်မျိုးလုံးသုံးလို့ရပါတယ်။ Data lines ကို အဝင်အနေနဲ့ သုံးချင်ရင်တော့ Bidirectional data transfer ကို ပထမဦးဆုံး enable လုပ်ဖို့လိုပါတယ်။ အဲဒီလိုလုပ်ဖို့ အတွက် Control register ထဲက bit 5 "Bidirectional" ကို set လုပ်ရမှာပါ။ အဲဒါကို Control register ထဲမှာ ၃၂ သွားရေးခြင်းအားဖြင့် set လုပ်နိုင်ပါတယ်။ ပင်တစ်ခုချင်းရဲ့ဆက်သွယ်မှုကိုတော့ အောက်က ဇယားမှာကြည့်နိုင်ပါတယ်။
ထိုနည်း၄င်းပဲ IO address 0x37A မှာ သုည ကိုရေးလိုက်ရင် ကွန်ထရိုး ရီဂျစ်စတာထဲကို သုည ဝင်သွားပြီး၊ သူနဲ့ဆက်ထားတဲ့ Pin 1, 14, 16, 17 တို့မှာ သက်ဆိုင်ရာတန်ဖိုးတွေထွက်လာမှာပါ။ Pin 1, 14, 17 တို့ဟာ ပြောင်းပြန်ဖြစ်တဲ့ အတွက် 0 ဆိုရင် +5V ထွက်လာမှာဖြစ်ပြီး၊ Pin 16 ကတော့ အတည့် ဖြစ်တဲ့ အတွက် 0 ဟာ 0V ပဲထွက်လာမှာ ဖြစ်ပါတယ်။
IO address 0x379 မှာရှိတဲ့ စတေတပ် ရီဂျစ်စတာ ကတော့ input ဖြစ်တဲ့ အတွက် သွားဖတ်ရမှာဖြစ်ပါတယ်။ Pin 10, 11,12, 13, 15 တို့မှာ ရှိတဲ့ Voltage တွေဟာ သူတို့ရဲ့ ရီဂျစ်စတာမှာရှိတဲ့ သက်ဆိုင်ရာ bit တွေမှာ သွားရောက်ထင်ဟပ်နေမှာ ဖြစ်ပါတယ်။ Pin 11 ကတော့ 0V ဆိုရင် 1၊ +5V ဆိုရင် 0 ဆိုပြီး ပြောင်းပြန် ဝင်နေမှာ ဖြစ်ပြီး၊ ကျန်တာတွေကတော့ အတည့် ပေါ်နေမှာ ဖြစ်ပါတယ်။
နမူနာ အနေနဲ့ ဒေတာ ပင် ၂ ကနေ ၉ အထိရယ်၊ ကွန်ထရိုးပင် ၁၊ ၁၄၊ ၁၆၊ ၁၇ တို့ကို output အနေနဲ့သုံးကြည့်မှာဖြစ်ပြီး၊ status pin ၁၀၊ ၁၁၊ ၁၂၊ ၁၃၊ ၁၅ တို့ကို input အနေနဲ့ သုံးကြည့်မှာဖြစ်ပါတယ်။ ဒါကြောင့် output တွေကို အောက်ကပုံအတိုင်း LED မီးလုံးလေးတွေနဲ့ ဆက်ပြီး input တွေကို ခလုတ်လေးတွေနဲ့ ဆက်ကြည့်မှာပါ။
ဒီအောက်က ပုံကတော့ ဒေတာ ငုတ်တွေကို အဝင်နေနဲ့ရော၊ အထွက်အနေနဲ့ရော သုံးထားတာပါ။ ကျွန်တော်တို့ နမူနာမှာတော့ ရှင်းလင်းအောင် ဒေတာ ကို အထွက် အနေနဲ့ပဲ သုံးမှာပါ။
ဘယ် ပင်က ဘယ်ဝါယာလဲ ဆိုတာ မသိခဲ့ရင်တော့ မီတာကို အသုံးပြုပြီး တိုင်းကြည့်နိုင်ပါတယ်။ တချို့ကြိုးတွေ ကတော့ အရောင်ခွဲမထားဘူး။ ကျွန်တော့် ကြိုးတုန်းကတော့
၁။ အနက်
၂။ နက်ဖြူကြား
၃။ အညို
၄။ ညိုဖြူကြား
၅။ အနီ
၆။ နီဖြူကြား
၇။ နီနက်ကြား
၈။ လိမ်မော်
၉။ လိမ်မော်၊ဖြူကြား
၁၀။ လိမ်မော်၊နက်ကြား
၁၁။ ပန်းရောင်
၁၂။ ပန်းနက်ကြား
၁၃။ အဝါ
၁၄။ ဝါနက်ကြား
၁၅။ အစိမ်း
၁၆။ စိမ်းဖြူကြား
၁၇။ စိမ်းနက်ကြား
၁၈။ အစိမ်းနု
၁၉။ အပြာ
၂၀။ ပြာဖြူကြား
၂၁။ ခရမ်း
၂၂။ ရမ်းဖြူကြား
၂၃။ မီးခိုး
၂၄။ ခိုးနက်ကြား
၂၅။ အဖြူ
လို့တွေ့ရှိရပါတယ်။ အထက်ပါ အတိုင်း မီးလုံးများ၊ ခလုပ်များတပ်ဆင်ပြီးတဲ့အခါ သူတို့ကို ထိန်းချုပ်ဖို့၊ ဖတ်ရှုဖို့ အတွက် ပရိုဂရမ် ရေးဖို့လိုပါတယ်။
အခု ကျွန်တော် တို့ ဝင်းဒိုး XP မှာ သုံးမှာဖြစ်ပါတယ်။ ပြီးရင် "inpout32.dll" ဆိုတဲ့ ဖိုင်ကို "C:/windows/system32/" ထဲမှာသွားထားပါ။ ပြီးရင်Visual Basic 2005 ကနေ သုံးပြီး ထိန်းချုပ်မှာ ဖြစ်ပါတယ်။ Visual Basic 6 အတွက်လည်း အတူတူပါပဲ အဲဒီအတိုင်း သုံးလို့ရပါတယ်။ အပေါ်လင့်က ဆိုရင် နမူနာ test application တွေလည်း ပါပါတယ်။ ပထမဆုံး အနေနဲ့ Visual Basic 2005 ထဲမှာ Project အသစ်တစ်ခုဖန်တီးပါမယ်။ File Menu ကနေ New Project... ကိုနှိပ်ပါ။ ပြီးရင် Windows Application ကိုရွေး၊ နာမည်ပေး ပြီးရင် အိုကေ ကိုနှိပ်ပါ။ ပြီးရင် Tools bar ထဲက Add New Item ကနေ Add Module ... ကိုနှိပ်ပြီး Module တစ်ခုထည့်ပါ။ ပြီးရင် Module ထဲမှာ အောက်က နှစ်ကြောင်းကို ကြေငြာပေးမှာပါ။
ပြီးရင် ဖောင် ပေါ်မှာ ခလုပ် နှစ်ခု၊ တက်စ်ဘောက်စ် နှစ်ခုထည့်ပါမယ်... ပုံ အကြီးကြည့်ဖို့ ပုံပေါ်မှာ ကလစ်နှိပ်ပြီးကြည့်နိုင်ပါတယ်။
ပထမ ခလုတ် ကိုနှိပ်ရင် သူ့ဘေး မှာထည့်ထားတဲ့ hexa တန်ဖိုးကို ဒေတာ ငုတ်တွေမှာ ထွက်သွားအောင် အခုလိုရေးနိုင်ပါတယ်။
ဒုတိယ ခလုတ်ကိုနှိပ်ရင် စတေးတပ် ငုတ်တွေမှာရှိတဲ့ တန်ဖိုးတွေကိုဖတ်ပြီး သူ့ဘေးက TextBox ထဲမှာ ဖော်ပြပေးမှာပါ။ IO address 0x379 မှာရှိတဲ့ စတေးတပ် ရီဂျစ်စတာကို ဖတ်ပြီး hexa တန်ဖိုးနဲ့ ဖော်ပြဖို့ အခုလိုရေးနိုင်ပါတယ်။
အဲဒါဆိုရင် Program နဲ့ စမ်းသပ်လို့ရပါပြီ။
Reference:
http://logix4u.net/Legacy_Ports/Parallel_Port/A_tutorial_on_Parallel_port_Interfacing.html
Hardware
အောက်က ပုံမှာ ကွန်ပျူတာရဲ့ Female 25 DB ခေါင်းတစ်ခုရဲ့ ပုံကို ပြထားပါတယ်။၁။ Data လိုင်းများ
၂။ Control လိုင်းများ
၃။ Status လိုင်းများ
ဆိုပြီး သုံးမျိုးခွဲခြားနိုင်ပါတယ်။ Data လိုင်းတွေကို Data register က ထိန်းချုပ်နိုင်၊ ဖော်ပြနိုင် ပြီးတော့ .... Control လိုင်းတွေကို Control register က ထိန်းချုပ်ပါတယ်။ Status လိုင်းတွေ မှာ ရှိတဲ့ တန်းဖိုးတွေကိုတော့ Status register မှာ ဖတ်ရှုနိုင်ပါတယ်။ သူတို့ရဲ့ ဆက်သွယ်မှုကို အောက်ကပုံမှာ ကြည့်နိုင်ပါတယ်။
Control lines တွေကို output အနေနဲ့ သုံးလို့ရပြီး၊ Status lines တွေကိုတော့ input အနေနဲ့သုံးလို့ရပါတယ်။ Data lines တွေကတော့ အဝင်၊ အထွက် နှစ်မျိုးလုံးသုံးလို့ရပါတယ်။ Data lines ကို အဝင်အနေနဲ့ သုံးချင်ရင်တော့ Bidirectional data transfer ကို ပထမဦးဆုံး enable လုပ်ဖို့လိုပါတယ်။ အဲဒီလိုလုပ်ဖို့ အတွက် Control register ထဲက bit 5 "Bidirectional" ကို set လုပ်ရမှာပါ။ အဲဒါကို Control register ထဲမှာ ၃၂ သွားရေးခြင်းအားဖြင့် set လုပ်နိုင်ပါတယ်။ ပင်တစ်ခုချင်းရဲ့ဆက်သွယ်မှုကိုတော့ အောက်က ဇယားမှာကြည့်နိုင်ပါတယ်။အိုင်ဘီအမ် ပီစီ တွေမှာ data register၊ control register၊ status register တွေ ဟာ တိကျတဲ့ IO address ရှိပါတယ်။ သူတို့ရှိတဲ့ နေရာတွေကို အောက်ကဇယားမှာ ကြည့်နိုင်ပါတယ်။
----------------------------------------------------------
|Pin No |Signal Name |Direction |Register -bit |Inverted |
----------------------------------------------------------
| 1.... | nStrobe....| Out......| Control-0....| Yes ....|
| 2.... | Data0......| In/Out...| Data-0.......| No......|
| 3.... | Data1......| In/Out...| Data-1.......| No......|
| 4.... | Data2......| In/Out...| Data-2.......| No......|
| 5.... | Data3......| In/Out...| Data-3.......| No......|
| 6.... | Data4......| In/Out...| Data-4.......| No......|
| 7.... | Data5......| In/Out...| Data-5.......| No......|
| 8.... | Data6......| In/Out...| Data-6.......| No......|
| 9.... | Data7......| In/Out...| Data-7.......| No......|
| 10... | nAck.......| In.......| Status-6.....| No......|
| 11... | Busy.......| In.......| Status-7.....| Yes.....|
| 12... | Paper-Out..| In.......| Status-5.....| No......|
| 13... | Select.....| In.......| Status-4.....| No......|
| 14... | Linefeed...| Out......| Control-1....| Yes.....|
| 15... | nError.....| In.......| Status-3.....| No......|
| 16... | nInitialize| Out......| Control-2....| No......|
| 17... | nSelect-Printer| Out..| Control-3....| Yes.....|
| 18-25 | Ground.....|-.........| - ...........| - ......|
----------------------------------------------------------
ဥပမာ IO address 0x378 မှာ သုည ကိုသွားရေးလိုက်ရင် LPT1 ပထမ parallel port ရဲ့ ဒေတာ ရီဂျစ်စတာထဲကို သုည ဝင်သွားမှာဖြစ်ပြီး သူနဲ့ဆက်ထားတဲ့ DB25 ခေါင်းရဲ့ Pin No ၂ ကနေ ၉ အထိ သုည ဗို့ထွက်သွားမှာပါ။ အဲဒီလို ပဲ IO address 0x378 မှာ ပဲ တစ်ရှစ်လုံး ( 0xFF ) သွားရေးလိုက်ရင် ဒေတာရီဂျစ်စတာရဲ့ Data 0 ကနေ Data 7 ထိ တစ်တွေဝင်သွားပြီး Pin ၂ ကနေ ၉ အထိမှာ +5V ထွက်လာမှာပါ။
---------------------------------------------------
| Register........| LPT1 | LPT2 |
---------------------------------------------------
| Data Register...| 0x378| 0x278|
| Status Register.| 0x379| 0x279| (baseaddress + 1)
| Control Register| 0x37A| 0x27A| (baseaddress + 2)
---------------------------------------------------
ထိုနည်း၄င်းပဲ IO address 0x37A မှာ သုည ကိုရေးလိုက်ရင် ကွန်ထရိုး ရီဂျစ်စတာထဲကို သုည ဝင်သွားပြီး၊ သူနဲ့ဆက်ထားတဲ့ Pin 1, 14, 16, 17 တို့မှာ သက်ဆိုင်ရာတန်ဖိုးတွေထွက်လာမှာပါ။ Pin 1, 14, 17 တို့ဟာ ပြောင်းပြန်ဖြစ်တဲ့ အတွက် 0 ဆိုရင် +5V ထွက်လာမှာဖြစ်ပြီး၊ Pin 16 ကတော့ အတည့် ဖြစ်တဲ့ အတွက် 0 ဟာ 0V ပဲထွက်လာမှာ ဖြစ်ပါတယ်။
IO address 0x379 မှာရှိတဲ့ စတေတပ် ရီဂျစ်စတာ ကတော့ input ဖြစ်တဲ့ အတွက် သွားဖတ်ရမှာဖြစ်ပါတယ်။ Pin 10, 11,12, 13, 15 တို့မှာ ရှိတဲ့ Voltage တွေဟာ သူတို့ရဲ့ ရီဂျစ်စတာမှာရှိတဲ့ သက်ဆိုင်ရာ bit တွေမှာ သွားရောက်ထင်ဟပ်နေမှာ ဖြစ်ပါတယ်။ Pin 11 ကတော့ 0V ဆိုရင် 1၊ +5V ဆိုရင် 0 ဆိုပြီး ပြောင်းပြန် ဝင်နေမှာ ဖြစ်ပြီး၊ ကျန်တာတွေကတော့ အတည့် ပေါ်နေမှာ ဖြစ်ပါတယ်။
Circuit Connection
ကွန်ပျူတာထဲမှာ ရှိတဲ့ Hardware အကြောင်းပြီးတဲ့ အခါ Parallel Port နဲ့ ဆက်သွယ်ဖို့ ဝါယာဆက်သွယ်မှု အကြောင်း ဆက်ပြောပါမယ်။ Male 25 DB ခေါင်းပါတဲ့ ကြိုးတစ်ခုကို ဝယ်ပြီး ကွန်ပျူတာဘက်ကိုဆက်နိုင်ပြီး ကျန်တဲ့ဘက်က ဝါယာတွေကိုတော့ input, output တွေနဲ့ဆက်နိုင်ပါတယ်။
ဒီအောက်က ပုံကတော့ ဒေတာ ငုတ်တွေကို အဝင်နေနဲ့ရော၊ အထွက်အနေနဲ့ရော သုံးထားတာပါ။ ကျွန်တော်တို့ နမူနာမှာတော့ ရှင်းလင်းအောင် ဒေတာ ကို အထွက် အနေနဲ့ပဲ သုံးမှာပါ။
ဘယ် ပင်က ဘယ်ဝါယာလဲ ဆိုတာ မသိခဲ့ရင်တော့ မီတာကို အသုံးပြုပြီး တိုင်းကြည့်နိုင်ပါတယ်။ တချို့ကြိုးတွေ ကတော့ အရောင်ခွဲမထားဘူး။ ကျွန်တော့် ကြိုးတုန်းကတော့
၁။ အနက်
၂။ နက်ဖြူကြား
၃။ အညို
၄။ ညိုဖြူကြား
၅။ အနီ
၆။ နီဖြူကြား
၇။ နီနက်ကြား
၈။ လိမ်မော်
၉။ လိမ်မော်၊ဖြူကြား
၁၀။ လိမ်မော်၊နက်ကြား
၁၁။ ပန်းရောင်
၁၂။ ပန်းနက်ကြား
၁၃။ အဝါ
၁၄။ ဝါနက်ကြား
၁၅။ အစိမ်း
၁၆။ စိမ်းဖြူကြား
၁၇။ စိမ်းနက်ကြား
၁၈။ အစိမ်းနု
၁၉။ အပြာ
၂၀။ ပြာဖြူကြား
၂၁။ ခရမ်း
၂၂။ ရမ်းဖြူကြား
၂၃။ မီးခိုး
၂၄။ ခိုးနက်ကြား
၂၅။ အဖြူ
လို့တွေ့ရှိရပါတယ်။ အထက်ပါ အတိုင်း မီးလုံးများ၊ ခလုပ်များတပ်ဆင်ပြီးတဲ့အခါ သူတို့ကို ထိန်းချုပ်ဖို့၊ ဖတ်ရှုဖို့ အတွက် ပရိုဂရမ် ရေးဖို့လိုပါတယ်။
Programming
အရင် ဝင်းဒိုး ၉၈ တို့၊ ဝင်းဒိုး Me တို့မှာ တုန်းက Turbo C တို့၊ Borland C တို့မှာ ပါတဲ့ "InportB", "OutportB" တို့၊ VC++ မှာ ပါတဲ့ "_inp" တို့ "_outp" တို့ကို သုံးပြီး Port ကို တိုက်ရိုက်ထိန်းနိုင်ပါတယ်။ သူတို့ကို "conio.h" ထဲမှာကြေငြာထားတာပါ။ ဒါပေမယ့် ဝင်းဒိုး NT/2000/XP တို့မှာ တော့ တိုက်ရိုက်ထိန်းလို့ မရတဲ့အတွက် "inpout32.dll" ဆိုတဲ့ လိုက်ဘရီဖိုင်ကို ထည့်ပြီးမှ သုံးမှာဖြစ်ပါတယ်။ သူ့ကို http://logix4u.net/inpout32_source_and_bins.zip မှာဒေါင်းလုပ် လုပ်ပြီးယူနိုင်ပါတယ်။အခု ကျွန်တော် တို့ ဝင်းဒိုး XP မှာ သုံးမှာဖြစ်ပါတယ်။ ပြီးရင် "inpout32.dll" ဆိုတဲ့ ဖိုင်ကို "C:/windows/system32/" ထဲမှာသွားထားပါ။ ပြီးရင်Visual Basic 2005 ကနေ သုံးပြီး ထိန်းချုပ်မှာ ဖြစ်ပါတယ်။ Visual Basic 6 အတွက်လည်း အတူတူပါပဲ အဲဒီအတိုင်း သုံးလို့ရပါတယ်။ အပေါ်လင့်က ဆိုရင် နမူနာ test application တွေလည်း ပါပါတယ်။ ပထမဆုံး အနေနဲ့ Visual Basic 2005 ထဲမှာ Project အသစ်တစ်ခုဖန်တီးပါမယ်။ File Menu ကနေ New Project... ကိုနှိပ်ပါ။ ပြီးရင် Windows Application ကိုရွေး၊ နာမည်ပေး ပြီးရင် အိုကေ ကိုနှိပ်ပါ။ ပြီးရင် Tools bar ထဲက Add New Item ကနေ Add Module ... ကိုနှိပ်ပြီး Module တစ်ခုထည့်ပါ။ ပြီးရင် Module ထဲမှာ အောက်က နှစ်ကြောင်းကို ကြေငြာပေးမှာပါ။
ဒီပုံထဲကလိုပါ။
'Inp and Out declarations for port I/O using inpout32.dll.
Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" (ByVal PortAddress As Short) As Short
Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" (ByVal PortAddress As Short, ByVal Value As Short)
ပြီးရင် ဖောင် ပေါ်မှာ ခလုပ် နှစ်ခု၊ တက်စ်ဘောက်စ် နှစ်ခုထည့်ပါမယ်... ပုံ အကြီးကြည့်ဖို့ ပုံပေါ်မှာ ကလစ်နှိပ်ပြီးကြည့်နိုင်ပါတယ်။
ပထမ ခလုတ် ကိုနှိပ်ရင် သူ့ဘေး မှာထည့်ထားတဲ့ hexa တန်ဖိုးကို ဒေတာ ငုတ်တွေမှာ ထွက်သွားအောင် အခုလိုရေးနိုင်ပါတယ်။Out function ရဲ့ ပထမ argument က သူသွားပြီးရေးရမယ့် IO address ပါ။ ဒေတာ ရီဂျစ်စတာမှာ ရေးချင်လို့ 0x378 ကိုသုံးပါတယ်။ ဒုတိယ argument က ရေးရမယ့် တန်ဖိုးပါ။ TextBox ထဲကတန်ဖိုးကို hexa တန်ဖိုး အနေနဲ့ ဖတ်ဖို့ "&H" ပေါင်းထည့်ပြီး တန်ဖိုးပြန်ပြောင်းထားတာပါ။
Out(&H378, Val("&H" & txtData.Text))
ဒုတိယ ခလုတ်ကိုနှိပ်ရင် စတေးတပ် ငုတ်တွေမှာရှိတဲ့ တန်ဖိုးတွေကိုဖတ်ပြီး သူ့ဘေးက TextBox ထဲမှာ ဖော်ပြပေးမှာပါ။ IO address 0x379 မှာရှိတဲ့ စတေးတပ် ရီဂျစ်စတာကို ဖတ်ပြီး hexa တန်ဖိုးနဲ့ ဖော်ပြဖို့ အခုလိုရေးနိုင်ပါတယ်။
Inp function ရဲ့ argument က သူသွားပြီးဖတ်ရမယ့် IO address ပါ။ ရတဲ့ တန်ဖိုးကို Hex function နဲ့ hexa string ပြောင်းပြီး Textbox မှာသွားထည့်ပါတယ်။
txtStatus.Text = Hex(Inp(&H379))
အဲဒါဆိုရင် Program နဲ့ စမ်းသပ်လို့ရပါပြီ။
Reference:
http://logix4u.net/Legacy_Ports/Parallel_Port/A_tutorial_on_Parallel_port_Interfacing.html
Subscribe to:
Posts (Atom)