Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo servo1;
- void setup() {
- // put your setup code here, to run once:
- servo1.attach(6);
- Serial.begin(9600);
- Serial.println("Start");
- }
- void loop() {
- if (Serial.available() > 0){
- int i = Serial.parseInt();
- Serial.println(i);
- servo1.write(i);
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- private delegate void SafeCallDelegate(string text);
- public Form1()
- {
- InitializeComponent();
- comboBox1.Items.Clear();
- foreach (string portName in SerialPort.GetPortNames())
- {
- comboBox1.Items.Add(portName);
- }
- comboBox1.SelectedIndex = 0;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- serialPort1.Open();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- serialPort1.Close();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (serialPort1.IsOpen)
- {
- try
- {
- serialPort1.Write(textBox1.Text);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- SerialPort sp = (SerialPort)sender;
- WriteTextSafe(sp.ReadExisting());
- }
- private void WriteTextSafe(string text)
- {
- if (label1.InvokeRequired)
- {
- var d = new SafeCallDelegate(WriteTextSafe);
- label1.Invoke(d, new object[] { text });
- }
- else
- {
- label1.Text = text;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement