You are on page 1of 6

Public Class dieukhien

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
updatePort()
End Sub

Private Sub updatePort()
If ComboBox1.Items.Count > 0 Then
ComboBox1.ResetText()
Else
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
'Ly danh sch cc cng c trn my'
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count -
1
ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
Button2.Enabled = False

Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False

ComboBox1.Select()
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If ComboBox1.Text <> "" Then
Try
With SerialPort1()
.PortName = ComboBox1.Text
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
SerialPort1.Open()
Label2.Text = " Kt ni ti " & ComboBox1.Text & " thnh cng
"
Button1.Enabled = False
Button2.Enabled = True

Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Else
MsgBox("Vui lng chn cng kt ni")
End If




End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Try
If SerialPort1.IsOpen Then

Else
Label2.Text = "Vui lng chn cng kt ni " & ComboBox1.Text
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
SerialPort1.Close()
Label2.Text = "Ngt kt ni vi " & ComboBox1.Text
Button1.Enabled = True
Button2.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal
e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Control.CheckForIllegalCrossThreadCalls = False

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Try
If SerialPort1.IsOpen Then
SerialPort1.Write("1")

Button1.Enabled = False
Button2.Enabled = True

Button4.Enabled = False
Button5.Enabled = True
Button6.Enabled = True
Else
Label2.Text = "Vui lng chn cng kt ni " & ComboBox1.Text
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
Try
If SerialPort1.IsOpen Then
SerialPort1.Write("3")


Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = False

Else
Label2.Text = "Vui lng chn cng kt ni " & ComboBox1.Text
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Try
If SerialPort1.IsOpen Then
SerialPort1.Write("2")


Button4.Enabled = True
Button5.Enabled = False
Button6.Enabled = True
Else
Label2.Text = "Vui lng chn cng kt ni " & ComboBox1.Text
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label2.Click

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs)

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button7.Click
updatePort()
End Sub
End Class


CODE PIC 16F877A


#include <16f877a.h>
#device 16f877a*16ADC=10
#include <DEFS_16F877A.h>
#include <string.h>
#include <stdlib.h>
#fuses nowdt,nolvp,noprotect,xt,put
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(c)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7) // ?i.nh nghi~a COM


#int_RDA // khai bo nga('t khi c du+~ lie^.u gu+?i xuo^'ng tu+` PC
void ngat_RDA()
{
char data;
data=getc(); // nha^.n du+~ lie^.u
switch (data)
{
case '1':
{
RD0=1;
RD1=0;
putc('1');
delay_ms(100);

break;
}

case '2':
{
RD0=0;
RD1=1;
putc('1');
delay_ms(100);

break;
}
case '3':
{
RD0=0;
RD1=0;
putc('1');
delay_ms(100);

break;
}
}
}

void main()
{
enable_interrupts(int_RDA);
enable_interrupts(global);
set_tris_b(0);
set_tris_a(0b1);
set_tris_c(0x80);
set_tris_d(0);
PORTB=0;
porta=0;
portc=0;
portd=0;
while(1)
{
rb7=1;
delay_ms(1000);// led bao
rb7=0;
delay_ms(1000);
}
}