I have created this Arduino sketch that communicates with my pc through the Console library and using the serial monitor over my network. Essentially, when I enter the digit 1, my Arduino stepper motor moves 30 steps etc. However, it is inconvenient to have to type in the serial monitor. I was hoping anyone would know how I can create an executable windows form or something that can connect over my network, to the Arduino Yun console.
#include <Stepper.h>
#include <Console.h>
char incomingByte;
Stepper stepper(64,8,9,10,11);
int stepCount = 1;
void setup() {
Bridge.begin();
Console.begin();
while (!Console);
stepper.setSpeed(60);
}
void loop() {
if (Console.available() > 0)
{
incomingByte = Console.read();
if (incomingByte == '1')
{
stepCount += 30;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(30);
Console.println(stepCount);
}
else{stepCount = stepCount - 30;}
}
if (incomingByte == '2')
{
stepCount = stepCount - 30;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(-30);
Console.println(stepCount);
}
else{stepCount += 30;}
}
if (incomingByte == '3')
{
stepCount += 200;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(200);
Console.println(stepCount);
}
else{stepCount = stepCount - 200;}
}
if (incomingByte == '4')
{
stepCount = stepCount - 200;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(-200);
Console.println(stepCount);
}
else{stepCount += 200;}
}
}
}
Here is the windows form app I was hoping would work:
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 Remote_Focuser
{
public partial class Form1 : Form
{
private SerialPort myPort;
public Form1()
{
Console.Read();
InitializeComponent();
Init();
}
private void Fwd_30_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("1");
}
private void Backward_30_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("2");
}
private void Forward_200_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("3");
}
private void Backward_200_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("4");
}
private void Init()
{
myPort = new SerialPort();
myPort.BaudRate = 9600;
myPort.PortName = "10.1.1.211";
myPort.Open();
}
}
}
Thanks heaps in advance, and yes I have probably made a big mistake, I am not too proficient yet... :)
Related
I am making a password generator and on websites when you enter certain conditions are met the strength of the password changes how can I change the color and text of the label when the password strength is >= 8, <8<10, >12?
Here is the Code:
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;
namespace Password_Generator
{
public partial class PassGen : Form
{
int currentPasswordLength = 0;
Random Character = new Random();
private void PasswordGenerator(int PasswordLength)
{
String validChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$&?";
String randomPassword = "";
//25
for(int i = 0; i < PasswordLength; i++)
{
int randomNum = Character.Next(0, validChars.Length);
randomPassword += validChars[randomNum];
}
Password.Text = randomPassword;
}
public PassGen()
{
InitializeComponent();
PasswordLengthSlider.Minimum = 5;
PasswordLengthSlider.Maximum = 22;
PasswordGenerator(5);
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Copy_Click(object sender, EventArgs e)
{
Clipboard.SetText(Password.Text);
}
//52
private void PasswordLength_Click(object sender, EventArgs e)
{
}
private void PasswordLengthSlider_Scroll(object sender, EventArgs e)
{
PasswordLength.Text = "Password Length:" + " " + PasswordLengthSlider.Value.ToString();
currentPasswordLength = PasswordLengthSlider.Value;
PasswordGenerator(currentPasswordLength);
}
private void pswdStrengthTest()
{
if (currentPasswordLength <= 8)
{
pswdStrength.Text = "weak";
pswdStrength.ForeColor = Color.Red;
} else if (currentPasswordLength<= 9)
{
pswdStrength.Text = "ok";
pswdStrength.ForeColor = Color.Blue;
}
}
//78
private void pswdStrength_Click(object sender, EventArgs e)
{
}
}
}
If anyone could help me with this it would be greatly appreciated. This is based off a tutorial I found on YouTube. I'm not sure what the video is called but if it helps I could search for it and update my posting.
Try this:
Password.TextChanged += (s1, e1) =>
{
if (Password.Text.Length > 10)
pswdStrength.ForeColor = Color.Green
else if (Password.Text.Length > 8)
pswdStrength.ForeColor = Color.Blue
else
pswdStrength.ForeColor = Color.Red
};
Your code looks like a windows form application.
If you have for example one objetc txt_password, check to code some of these events:
TextChanged: this occurs when your textbox has been changed
Others events could be:
KeyPress or KeyDown
I have three Forms in my project. Form2 Connects the Serial Port. Form1 writes and reads data from the Serial Port. Form 3 only needs to write to the serial port, however when i send data to the serial port I get an "Port is closed error". I do not see any difference in the way i set up form 2 and form 3 so i am not sure why Visual Studios is giving me the "port closed" error.
Form 2 will connect to serial port
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; // added this
namespace ECE_323_LAB_10
{
public partial class Bluetooth_Settings : Form
{
public SerialPort _serial = new SerialPort(); // added this
public Bluetooth_Settings()
{
InitializeComponent();
_serial.BaudRate = int.Parse(baud_rate.Text); // added this
foreach (string s in SerialPort.GetPortNames()) // added this
{
com_port.Items.Add(s);
}
}
private void connet_button_Click(object sender, EventArgs e)
{
try
{
_serial.PortName = com_port.SelectedItem.ToString();
_serial.BaudRate = Convert.ToInt32(baud_rate.SelectedItem);
_serial.Open();
this.Close();
Form1 _main = new Form1();
foreach (Form1 tmpform in Application.OpenForms)
{
if (tmpform.Name == "Form1")
{
_main = tmpform;
break;
}
}
_main.toolStripStatusLabel1.Text = " Connected: " + _serial.PortName.ToString();
_main.toolStripStatusLabel1.ForeColor = Color.Green;
_main.toolStripProgressBar1.Value = 100;
}
catch
{
MessageBox.Show("Please select COM Port/ Baud Rate");
}
}
}
}
Form1 can read and write data to serial port
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;
using ZedGraph;
using System.Text.RegularExpressions;
namespace ECE_323_LAB_10
{
public partial class Form1 : Form
{
PointPairList list = new PointPairList();
double temp;
int flag = 1;
double x = 0;
int init = 0;
int digit = 0;
double temp1;
private T_settings t_settings;
private Bluetooth_Settings _setting = new Bluetooth_Settings();
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int text_length = 0;
text_length = richTextBox1.TextLength;
char send_ch = richTextBox1.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_setting._serial.Write("\r"); // sending carraige return
}
else
{
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}
private void Toggle_Click(object sender, EventArgs e)
{
// 2 = F , 1 = C
char[] ch = new char[1];
ch[0] = 'D';
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
private void Settings_Click(object sender, EventArgs e)
{
t_settings = new T_settings();
t_settings.Show();
}
}
}
Form1 has no errors when i read and write to the serial port. I have left a few lines of codes out for readability.
Now here is the code for Form3, I think i have done setup exactly the same as Form1, however I am getting port is closed error.
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; // added this
namespace ECE_323_LAB_10
{
public partial class T_settings : Form
{
private Bluetooth_Settings _enter = new Bluetooth_Settings();
public T_settings()
{
InitializeComponent();
}
string Sampling_text;
private void Text_Sampling_TextChanged(object sender, EventArgs e)
{
Sampling_text = Text_Sampling.Text;
}
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_enter._serial.Write("\r"); // sending carraige return
}
else
{
_enter._serial.Write(ch, 0, 1); // sending char to microcontroller
}
char[] enter = new char[1];
}
}
}
Can someone tell me what I need to add to Form3 so I do not get a port closed error. In my send sampling click method.
Ok, if you're totally new then concepts around OOP will take time to learn. Let me try and give you a solution to get you out of trouble.
private T_settings t_settings = null;
private Bluetooth_Settings _bsSettings = null;
public Form1()
{
InitializeComponent();
if (_bsSettings == null) _bsSettings = new Bluetooth_Settings();
_bsSettings.Show();
}
private void Form1_Shown(Object sender, EventArgs e) {
{
//Either here or in the constructor instantiate T_settings with a reference to the
//_bsSettings this way it will still be in scope, the same instance you've connected.
if (t_settings == null) t_settings = new T_settings(_bsSettings);
}
public partial class T_settings : Form
{
private Bluetooth_Settings _bsSettings = new Bluetooth_Settings();
public T_settings(Bluetooth_Settings bsSettings)
{
InitializeComponent();
//Pass a reference of the BS Settings class (one that's already connected)
this._bsSettings = bsSettings;
}
...
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_bsSettings._serial.Write("\r"); // sending carraige return
}
else
{
_bsSettings._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}
My goal is to make a project that can be able to control a simple system using the NodeMcu esp 8266, by a another simple Windows Form Application in C#.
But I had some mistakes on my code and I don't know why. So I would like to someone help me doing this.
Errors:
When I sent the position to the servo, he works only one time and every time in the same position.
When I sent the comand to the Bulbs turn on off, only one of them works.
Just the buzzer works as I want
Here is the Arduino Code:
// Programação do controle de dispositivos via Serial com ESP8266
#include <Servo.h>
// #include <Process.h>
Servo servo;
const int buzzer = D2;
const int lamp1 = D4;
const int lamp2 = D0;
const int pinServo = D3;
String serialData;
// String comando;
bool estadoBuzzer = LOW;
bool estadoLam1 = LOW;
bool estadoLam2 = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//SerialUSB.begin(115200);
pinMode(buzzer, OUTPUT);
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
servo.attach(pinServo);
Serial.setTimeout(10);
//Bridge.begin();
//while (!SerialUSB);
}
void loop() {
// put your main code here, to run repeatedly:
serialEvent(Serial.readString());
}
void serialEvent(String data){
/*Process p;
comando = "echo " + data + " > teste_arduino_serial_comandos.txt";
p.runShellCommand(comando);
*/
if(data.charAt(0) == 'B'){
if(estadoBuzzer == HIGH){
digitalWrite(buzzer, LOW);
estadoBuzzer = LOW;
} else {
digitalWrite(buzzer, HIGH);
estadoBuzzer = HIGH;
}
} else if(data.charAt(0) == 'L'){
data.remove(0);
if(data.toInt() == 1){
lampada1();
} else {
lampada2();
}
} else if(data.charAt(0) == 'A'){
data.remove(0);
int angulo = data.toInt();
Serial.println(angulo);
if(angulo >= 0 && angulo <= 180){
servo.write(angulo);
}
}
}
void lampada1(){
if(estadoLam1 == HIGH){
digitalWrite(lamp1, LOW);
estadoLam1 = LOW;
} else {
digitalWrite(lamp1, HIGH);
estadoLam1 = HIGH;
}
}
void lampada2(){
if(estadoLam2 == HIGH){
digitalWrite(lamp2, LOW);
estadoLam2 = LOW;
}
else {
digitalWrite(lamp2, HIGH);
estadoLam2 = HIGH;
}
}
And here is the C# code:
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.Diagnostics;
namespace ControleDeDispositivosViaSerialNodeMcuEsp8266
{
public partial class Form1 : Form
{
public Stopwatch watch { get; set; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
watch = Stopwatch.StartNew();
port.Open();
}
public void writeToPort(String indicador, String valor="0")
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();
port.Write(String.Format(indicador+valor));
System.Windows.Forms.MessageBox.Show(indicador + valor);
}
// System.Windows.Forms.MessageBox.Show(port.ReadExisting());
}
private void anguloBtn_Click(object sender, EventArgs e)
{
writeToPort("A", angulos.Text);
angulos.Refresh();
}
private void onoff1_Click(object sender, EventArgs e)
{
writeToPort("L", "1");
}
private void onoff2_Click(object sender, EventArgs e)
{
writeToPort("L", "2");
}
private void buzzerBtn_Click(object sender, EventArgs e)
{
writeToPort("B");
}
}
}
Your data.toInt() is not working, this is why it gets always only one lamp and to same postion on the servo.
The data.Remove(0) is removing all after first char so will be empty.
Use data = data. Substring(1)
What i normally do as feedback to check is write: Serial.println(data) etcto check if i really work with what i think i have.
I have a windows form application that basically pings an ip and then returns an image with a tooltip that displays the rtt to that ip.
What i want to do is have the the form ping that ip every 20 seconds, so that the form and images change. If i could get that to work then I would like to some how store maybe 4 rtt's and then show an average of the 4 in the tooltip.
So far the form is only pinging once, I've played around with a timer but I don't really know what I am doing. Here is my code so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Ping pingClass = new Ping();
PingReply pingReply = pingClass.Send("10.209.123.123");
label4.Text = (pingReply.RoundtripTime.ToString());
//+ "ms");
label5.Text = (pingReply.Status.ToString());
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
this.pictureBox1.Load("greenLAT.png");
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
this.pictureBox1.Load("yellowLAT.png");
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
this.pictureBox1.Load("redLAT.png");
ToolTip tt = new ToolTip();
tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("Timeout!");
Refresh();
}
}
}
Try this:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace DXWindowsApplication4
{
public partial class Form2 : Form
{
private readonly Timer _timer;
private readonly Ping _pingClass;
private readonly IPAddress _ipAddress;
private readonly int _timeout;
private Image _greenImage;
private Image _yellowImage;
private Image _redImage;
private int _pingCount;
private long _avgRtt;
public Form2()
{
InitializeComponent();
IPAddress.TryParse("98.138.253.109", out _ipAddress); // yahoo.com Ip address
_timer = new Timer();
_timeout = 3000;
_pingClass = new Ping();
_pingClass.PingCompleted += PingClassPingCompleted;
}
void PingClassPingCompleted(object sender, PingCompletedEventArgs e)
{
RefreshPing(e.Reply);
}
public void FormLoad(object sender, EventArgs e)
{
_timer.Tick += TimerTick;
_timer.Interval = 4000;
_timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
_pingClass.SendAsync(_ipAddress, _timeout);
}
private void RefreshPing(PingReply pingReply)
{
label4.Text = (pingReply.RoundtripTime.ToString(CultureInfo.InstalledUICulture));
label5.Text = (pingReply.Status.ToString());
_avgRtt = (_avgRtt * _pingCount++ + pingReply.RoundtripTime)/_pingCount;
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
{
SetImage(pictureBox1, Images.Green);
}
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
{
SetImage(pictureBox1, Images.Yellow);
}
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
{
SetImage(pictureBox1, Images.Red);
}
ToolTip tt = new ToolTip();
tt.SetToolTip(pictureBox1, "Your average network delay is " + _avgRtt + "ms");
Refresh();
}
private void SetImage(PictureBox pBox, Images images)
{
switch (images)
{
case Images.Green:
if (_greenImage == null)
{
_greenImage = new Bitmap("greenImage.png");
}
pictureBox1.Image = _greenImage;
break;
case Images.Yellow:
if (_greenImage == null)
{
_yellowImage = new Bitmap("yellowImage.png");
}
pictureBox1.Image = _yellowImage;
break;
case Images.Red:
if (_redImage == null)
{
_redImage = new Bitmap("redImage.png");
}
pictureBox1.Image = _greenImage;
break;
default:
throw new InvalidEnumArgumentException("invalid enum name");
}
}
}
internal enum Images
{
Green,
Yellow,
Red
}
}
I have a program that gets the incoming number, date and time. I want to check however if the person who is ringing me has put the phone down, how can I do this?
Below is the code which I currently have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace CallerID
{
public partial class CallerID : Form
{
int timesTicked = 0;
Point defaultLocation = new Point();
Point newLocation = new Point();
public CallerID()
{
InitializeComponent();
port.Open();
SetModem(); // SetModem(); originally went after WatchModem();
WatchModem();
//SetModem();
telephoneTimer.Interval = 16;
telephoneTimer.Tick += new EventHandler(telephoneTimer_Tick);
defaultLocation = pictureBox1.Location;
newLocation = pictureBox1.Location;
}
void telephoneTimer_Tick(object sender, EventArgs e)
{
if (timesTicked <= 2)
newLocation.X++;
if (timesTicked >= 4)
newLocation.X--;
if (timesTicked == 6)
{
timesTicked = 0;
pictureBox1.Location = defaultLocation;
newLocation = defaultLocation;
}
pictureBox1.Location = newLocation;
timesTicked++;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
//port.WriteLine("AT+VCID=1");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
//if (lblCallerIDTitle.InvokeRequired)
if (ReadData.Contains("NMBR"))
{
lblData.Invoke(new SetCallerIdText(() => lblData.Text = ReadData));
}
//else
// lblCallerIDTitle.Text = ReadData;
}
private void lblData_TextChanged(object sender, EventArgs e)
{
telephoneTimer.Start();
button1.Visible = true;
}
}
}
Please ignore the Timer Code as that is just for animation.
Have you tried the PinChanged event? Normally Carrier Detect will go low when the remote end disconnects.