This is a simple application written in SharpDevelop. It is very easy
to interface with serial Ports on windows. This code should work
on VB.NET also.
to interface with serial Ports on windows. This code should work
on VB.NET also.
Now for the code…
' Created by SharpDevelop. ' User: Warren ' Date: 3/2/2012 ' Time: 12:15 PM ' ' To change this template use Tools | Options | Coding | Edit Standard Headers. ' Public Partial Class MainForm Dim WithEvents SerialPort As New IO.Ports.SerialPort Public Sub New() ' The Me.InitializeComponent call is required for Windows Forms designer support. Me.InitializeComponent() ' ' TODO : Add constructor code after InitializeComponents ' End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try GetSerialPortNames() Catch msgbox("No ports seem to be connected.") End Try 'load combo box with baudrates Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"} cmboBaudrate.Items.AddRange(BaudRates) 'start combobox with first available value cmboComm.SelectedIndex = 0 cmboBaudrate.SelectedIndex = 0 End Sub Private Sub ConnectSerial() If btnConnect.Text = "Disconnect" Then Try SerialPort.Close() btnConnect.Text = "Connect" cmboComm.Enabled = True cmboBaudrate.Enabled = True btnRefresh.Enabled = True Exit Sub Catch msgbox("Error..lost port connection??") End Try End If Try SerialPort.BaudRate = cmboBaudrate.SelectedItem.ToString SerialPort.PortName = cmboComm.SelectedItem.ToString SerialPort.Open() If SerialPort.IsOpen Then btnConnect.Text = "Disconnect" cmboComm.Enabled = False cmboBaudrate.Enabled = False btnRefresh.Enabled = False Exit Sub End If Catch SerialPort.Close() End Try End Sub Delegate Sub myMethodDelegate(ByVal [text] As String) Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod) Sub myShowStringMethod(ByVal myString As String) SerialText.AppendText(myString) End Sub Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived Dim str As String = SerialPort.ReadExisting() Invoke(myD1, str) End Sub Sub GetSerialPortNames() ' Show all available COM ports. For Each sp As String In My.Computer.Ports.SerialPortNames cmboComm.Items.Add(sp) Next End Sub Sub BtnConnectClick(sender As Object, e As EventArgs) SerialText.text = Nothing ConnectSerial() End Sub Sub BtnRefreshClick(sender As Object, e As EventArgs) cmboComm.Items.Clear() GetSerialPortNames() End Sub Sub BtnAboutClick(sender As Object, e As EventArgs) msgbox("FREE SOFTWARE from www.competefornothing.com") End Sub Sub BtnSendClick(sender As Object, e As EventArgs) If SerialPort.IsOpen Then SerialPort.Write(txtSend.Text) Else ConnectSerial() SerialPort.Write(txtSend.Text) End If End Sub Sub TxtSendKeyDown(sender As Object, e As KeyEventArgs) If e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True If SerialPort.IsOpen Then SerialPort.Write(txtSend.Text) Else ConnectSerial() SerialPort.Write(txtSend.Text) End If txtSend.Clear() End If End Sub End Class
Now some code for the Arduino to test the send and receive.
//Code from arduino.cc int incomingByte = 0; void setup(){ Serial.begin(9600); } void loop(){ if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // say what you got: Serial.print("I received: "); Serial.println(incomingByte, DEC); } }
Hiç yorum yok:
Yorum Gönder