When i scrolled this trackbar right or left, The value should has changed from zero to its max. value (255) and the LED at the output should has been brighter over trackbar values while scrolling right.
But this trackbar works like switch. Before trackbar is 127 the led is off and after that, led suddenly turns on.
Where do you think problem is?
c# code
{
public partial class Form1 : Form
{
SerialPort serialport;
public Form1()
{
InitializeComponent();
serialport = new SerialPort();
serialport.BaudRate = 9600;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "COM3";
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
try
{
serialport.PortName = textBox1.Text;
if (!serialport.IsOpen)
serialport.Open();
MessageBox.Show("connected");
}
catch
{
MessageBox.Show("error!");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
string ledval = trackBar1.Value.ToString();
textBox2.Text = ledval;
serialport.WriteLine(ledval);
serialport.WriteLine(",");
}
catch (Exception ex) { }
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
serialport.Close();
}
}}
arduino code
int led = 13;
String x;
int val;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop(){
if(Serial.available()>0)
{
x=Serial.readStringUntil(',');
val=x.toInt();
analogWrite(led, val);
}
}
Related
I need to calculate the Lb1SumF plus Lb2SumF equal Lb3SumF.
I ran it, and somehow the label3 does not display the expected result.
Here is a screenshot from result.
Here is my code.
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
Lb3_TextChanged might never be invoked as you are not changing the text of the label. I would suggest to change it to a private method and not an event handler. Here is what the code could be like:
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb2SumF.Text = "0";
}
}
// private void Lb3_TextChanged(object sender, EventArgs e)
private void UpdateSum()
{
int sum = 0;
if(!string.IsNullOrEmpty(Lb1SumF.Text) && !string.IsNullOrEmpty(Lb2SumF.Text))
{
sum = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
}
Lb3.Text = Convert.ToString(sum);
}
Replace you code with this it will work, you are using * operator where you have to use + operator, I have commented that lines in your code and replaced it for better understanding.
Happy Coding
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
private void Lb1SumF_Click(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void label4_Click(object sender, EventArgs e)
{
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text); // You were doing wrong here , you were multiplying these values
sumF = Convert.ToInt32(Lb1PriceF.Text) + Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text); //you are doing it wrong here , you are multiplying
sumF = Convert.ToInt32(Lb2PriceF.Text) + Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
private void Lb3SumF_Click(object sender, EventArgs e)
{
}
}
}
I'm working on a spambot but there is 1 problem I can't solve. I made buttons you can press, it says how much you want to spam. If you want to spam you will get a certain amount of time to go to the place where you want to spam. I made a function for how much the text will spam. But the problem is when it send 1 message it wait the certain amount of time time and not the 500 miliseconds
The script is written in C#. The target framework is: .NET Framework 4.6.1.
public partial class Form1 : Form
{
public void Time()
{
for (int i = 0; i <= 10;)
{
Stuur();
i++;
}
} // 10x
public void Stuur() // does the sending
{
System.Threading.Thread.Sleep(500);
SendKeys.Send(textBox3.Text);
SendKeys.Send("{ENTER}");
}
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Rush B", "5 sec voor spam",
MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Threading.Thread.Sleep(5000);
Time();
}
private void button2_Click(object sender, EventArgs e) // the credit
block
{
if (textBox6.Visible == true)
{
textBox6.Visible = false;
} else {
textBox6.Visible = true;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
Maybe something along these lines?
public void Stuur() // does the sending
{
System.Threading.Thread.Sleep(5000);
SendKeys.Send(textBox3.Text);
SendKeys.Send("{ENTER}");
}
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= Int32.Parse(textBox.Text4); i++) {
Stuur();
}
}
private void button2_Click(object sender, EventArgs e) // the credit block
{
if (textBox6.Visible == true)
{
textBox6.Visible = false;
}
else
{
textBox6.Visible = true;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e) { }
}
edit: closed parenthesis
I have a project where I need to communicate with Arduino over the serial ports. The problem I face is that I cannot print continously the data I receive from the serial monitor on multiple lines on a richtextbox. When I press the button "Reveice" I do receive only one value and after this, pressing again the Receive button will overwrite the line.
I'm tring to fix this for few days, but it's my first time programming in c# so I'm asking for your help.
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;
using System.IO.Ports;
namespace aplicatie_comanda_v1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getAvilablePorts();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
progressBar1.Value = 0;
button3.Enabled = true;
button1.Enabled = false;
receive.Enabled = false;
richTextBox1.Clear();
}
private void label1_Click(object sender, EventArgs e)
{
}
void getAvilablePorts()
{
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || comboBox2.Text == "" && serialPort1 != null && serialPort1.IsOpen)
{
richTextBox1.Text = "Select COM port and BAUD rate !";
serialPort1.Close();
}
else
{
string cmd = Convert.ToString(comboBox1.Text);
int baud = Convert.ToInt32(comboBox2.Text);
serialPort1.PortName = cmd;
serialPort1.BaudRate = baud;
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
serialPort1.Open();
progressBar1.Value = 100;
button1.Enabled = true;
button2.Enabled = true;
textBox1.Enabled = true;
button3.Enabled = false;
}
}
catch (UnauthorizedAccessException)
{
richTextBox1.Text = "Unauthorized !";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
serialPort1.Write(text);
}
private void receive_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Text = serialPort1.ReadLine() + "\n";
}
catch (TimeoutException)
{
richTextBox1.Text = "Timeout !";
}
}
private void button4_Click(object sender, EventArgs e)
{
serialPort1.Write("w");
}
private void button5_Click(object sender, EventArgs e)
{
serialPort1.Write("s");
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
serialPort1.Write("a");
}
private void button7_Click(object sender, EventArgs e)
{
serialPort1.Write("d");
}
private void button12_Click(object sender, EventArgs e)
{
serialPort1.Write("b");
}
private void button13_Click(object sender, EventArgs e)
{
string cmd = Convert.ToString(trackBar1.Value);
serialPort1.Write(cmd);
}
private void button8_Click(object sender, EventArgs e)
{
serialPort1.Write("q");
}
private void button11_Click(object sender, EventArgs e)
{
serialPort1.Write("e");
}
private void button9_Click(object sender, EventArgs e)
{
serialPort1.Write("z");
}
private void button10_Click(object sender, EventArgs e)
{
serialPort1.Write("c");
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
while (serialPort1.IsOpen)
{
try
{
string date = serialPort1.ReadLine();
richTextBox1.Text = date + "\n";
}
catch (TimeoutException)
{
richTextBox1.Text = "Timeout !";
}
}
}
}
}
Print screen of the final app: http://i.imgur.com/5f8EOly.png
Thank you !
I haven't written any Serial applications in C# yet, but already did a few projects involving Java <-> Arduino communication.
My first guess would be that you overwrite the existing line with the received line.
richTextBox1.Text = serialPort1.ReadLine() + "\n";
instead you would want:
richTextBox1.Text += serialPort1.ReadLine() + "\n";
Also you should take a look at this article on MSDN:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx
This shows how you could use Events to continuously receive text from the Arduino.
I made a winform with 1 richtextbox and two buttons,
and I hope that when I click on the yes button , it will show a method soal2 in richtextbox1 , and then when I click again it will show soal3 , how to do that?
this is my design
public void soal1()
{
richTextBox1.Text = "Hemofilia is xxxxx";
}
public void soal2()
{
richTextBox1.Text = "xxxxxxx";
}
public void soal3()
{
richTextBox1.Text = "yyyyyy";
}
private void Quiz1_Load(object sender, EventArgs e)
{
soal1();
}
private void button1_Click(object sender, EventArgs e)
{
}
/* ... */
bool alreadyShownSoal2 = false;
private void button1_Click(object sender, EventArgs e)
{
if(alreadyShownSoal2)
soal3();
else
soal2();
alreadyShownSoal2 = true;
}
or
/* ... */
bool alreadyShownSoal2 = false;
public void soal2()
{
if(alreadyShownSoal2)
soal3();
else
richTextBox1.Text = "xxxxxxx";
alreadyShownSoal2 = true;
}
/* ... */
private void button1_Click(object sender, EventArgs e)
{
soal2();
}
This is an absolutely terrible design, but unless you give more specifications... it'd definitely do what you are asking
I have used this code in VS
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string RxString,ComPort;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Handshake = Handshake.None;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
private void Start_Click(object sender, EventArgs e)
{
serialPort1.PortName = ComPort;
serialPort1.BaudRate = 9600;
serialPort1.Open();
if(serialPort1.IsOpen)
{
Start.Enabled = false;
Stop.Enabled = true;
textBox1.ReadOnly = false;
}
}
private void Stop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
Start.Enabled = true;
Stop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!serialPort1.IsOpen) return;
char[] buff = new char[1];
buff[0] = e.KeyChar;
serialPort1.Write(buff, 0 , 1);
e.Handled = true;
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComPort = comboBox1.SelectedItem.ToString();
}
}
}
but it's not working, I have tried using an avr for transmitting characters and successfully tested in hercules what its transmitting. But it is not showing up in my program. Please help.
I have updated the code and its working fine for receiving part but not transmitting correctly, i am not getting any error it is just not working as it should have worked.
You must set all the properties of your serialPort1.
Also, you should try to debug at multiple place to help us where it's going wrong : Does IsOpen return true? if no, this explain why you receive nothing.
See MSDN example if you want to try something is supposed to work : http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
Be sure that the serial port is not alerady open by another program and you have selected the good COM PORT. Otherwise the code looks good. (You could also looks in the RxString value each time you are passing by. (maybe many empty space or "/r"))
[Serializable]
public class Customer
{
[XmlElement("FirstName")]
public string FirstName { get; set; }
[XmlElement("LastName")]
public string LastName { get; set; }
[XmlElement("Age")] public int Age { get; set; }
public bool ShouldSerializeLastName()
{
return Age > 18; // Enter here only if it is XmlSerialize.
}
}