character sent to serial port not read by arduino - c#

I am sending a character from visual studio C# to the serial port and I want arduino to read the character and do some work. But, its not reading it correctly. I have tested the arduino program using the serial monitor and it is working perfectly.Here's my C# code.
private void startBit_Click(object sender, EventArgs e)
{
char start = '!';
byte[] buffer = new byte[] { Convert.ToByte(start) };
timerRealTimeData.Enabled = true;
serialPort1.PortName = "COM29";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
serialPort1.Open();
serialPort1.Write(buffer, 0, 1);
if (serialPort1.IsOpen)
{
Start.Enabled = false;
Stop.Enabled = true;
}
}
Here's the arduino code for reading the serial port
char incomingByte;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
if(Serial.available() > 0) {
incomingByte = Serial.read();
if(incomingByte=='!')
{
digitalWrite(13,HIGH);
delay(2000);
digitalWrite(13,LOW);
}
}
}
I am stuck on this for a long time.Any help is appreciated

Try to read the equivalent Ascii (33) using an int instead of a char. Also change the line
if(Serial.available() > 0)
To
if(Serial.available())
Some reference for you. Here

First of all, create a function where you open the serial port, and a function where you close the serial port. Your click handler startBit_Click opens the serial port, which means that if the handler is called again, the port is already open and an exception will be generated. (unless you have other code not shown that closes the serial port?)
Second, I am guessing you are connected to the arduino through the USB port?
If so, DtrEnable should be false (default), as you have no XON/XOFF software handshaking.
You should also examine the buffer in the debug window, set a breakpoint after the buffer is assigned the character and check that the character you send is indeed what you think it is.

Related

How to create a SerialPort.ReadLine() loop that breaks upon receiving data [duplicate]

This question already has an answer here:
Reading from serial port asynchronously using Async await method
(1 answer)
Closed 6 years ago.
I have a program which relies on data being sent through a serial port. I would like to monitor when data is sent through with SerialPort.ReadLine(). My problem is that I use check boxes which freeze when checked because the SerialPort.ReadLine() method blocks until it receives some data. I have also tried using ReadTimeout() but it did not work; perhaps because I did not use it correctly.
I've posted my code for this event below. What I would like this method to do is to monitor my serial port when the checkbox is checked, and then stop monitoring the serial port when I un-check the box. My current situation with my code is that it freezes upon checking the box. All help is appreciated.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Text = "Listening...";
using (SerialPort arduino = new SerialPort())
{
arduino.BaudRate = 9600;
arduino.PortName = comboBox1.Text;
arduino.Open();
string pipe = arduino.ReadLine(); //loop here
if (pipe == "S")
{
//System.Diagnostics.Process.Start("shutdown", "/f /r /t 0");
System.Windows.Forms.Application.Exit();
}
else
{
checkBox1.Text = "Start";
}
}
}
}
Consider using the SerialPort.DataReceived event instead of blocking.

Frozen windows form application with checkbox c#

Here is my situation, when I check the checkbox, my application freezes but still works. Which means that it is still able to recognize data sent through the serial port; for testing purposes it just exits the application.
If I comment out line 45 ("pipe = arduino.ReadLine();" see screenshot below) meaning that it no longer has to "ReadLine()", I am able to un-check the box. However now when I try to re-check the box, I get an error message saying "Access to the port 'COM5' is denied"
I assume that the code cannot continue because it is trying to "ReadLine()" when nothing has been sent through yet. However I do not have an explanation for being denied access to the COM port; rather than me trying to open the port when its already opened.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
SerialPort arduino = new SerialPort();
arduino.BaudRate = 9600;
arduino.PortName = comboBox1.Text;
string pipe;
if (checkBox1.Checked)
{
checkBox1.Text = "Listening...";
arduino.Open();
pipe = arduino.ReadLine();
if (pipe == "S\r")
{
//System.Diagnostics.Process.Start("shutdown", "/f /r /t 0");
System.Windows.Forms.Application.Exit();
}
else
{
checkBox1.Text = "Start";
}
}
}
The SerialPort class manages system resources and, when such sensitive objects are involved, the class usually implements the IDisposable interface to allow those system resource to be released immediately to the system.
Your code forgets to close the SerialPort, so, the next time your user action cause a call to this event handler the port is in use by your own first action.
Fortunately, there is an easy way to ensure proper closing and disposing of such objects and it is the using statement
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Text = "Listening...";
using(SerialPort arduino = new SerialPort())
{
arduino.BaudRate = 9600;
arduino.PortName = comboBox1.Text;
string pipe;
arduino.Open();
pipe = arduino.ReadLine();
if (pipe == "S\r")
{
//System.Diagnostics.Process.Start("shutdown", "/f /r /t 0");
//System.Windows.Forms.Application.Exit();
}
} // Here the port will be closed and disposed.
}
else
{
checkBox1.Text = "Start";
}
}

How to trigger Serial port data recieved event with bluetooth c#

I have device that can connect to my laptop via blue-tooth as COM5. The device has a Pulse sensor. I want to draw data coming from sensor to graph. However when i connected to COM5 the serialport_Datarecieved event is not triggered. I tried device using matlab. It takes and draws data but i cant get data in c#. I checked the connection status of device and it is ok. I tried to change DtrEnabled and RtsEnapled properties but not worked.
private void Form1_Load(object sender, EventArgs e)
{
cmbPortList.Items.AddRange(SerialPort.GetPortNames());
cmbPortList.Sorted = true;
cmbPortList.SelectedIndex = 0;
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
private void btnOpenPort_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = cmbPortList.Text;
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.Handshake = Handshake.None;
if (!serialPort1.IsOpen)
{
btnRun.Enabled = true;
serialPort1.Open();
}
}
catch (Exception ex)
{
serialPort1.Close();
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (serialPort1.BytesToRead > 0)
{
Thread.Sleep(50);
byte[] buffer = new byte[serialPort1.BytesToRead];
serialPort1.Read(buffer, 0, buffer.Length);
}
}
I cant read any data in buffer. There is led is flashing while device is not connected with via blue-tooth. So i am absolutely sure i connected to device.
Is problem about Bluetooth or code? Should i use another library to communicate blue tooth device?
I have read links below.
SerialPort fires DataReceived event after close
SerialPort not receiving any data
This may have less to do with the SerialPort and more to do with the way that Winforms threads are interacting with the serial port's background worker threads. See the solution to this for more info.
I think the designer of the circuit requests data from device with 's'. It must be about its protocol or hex code. I have found that code in matlab sample of circuit % Request Data fprintf(s,'s'); That's why i can read data when i use serialport.Write("Blast"); Also i tried all letters. Only 's' char triggers the event.

Serial Port simple

Hi i've got here a simple program, but it's not working properly.
When i receive 'A' on the serial port i set checkbox1, and when 'a' i unset checkbox1.
public partial class MainWindow : Window
{
public static SerialPort sp = new SerialPort();
public MainWindow()
{
InitializeComponent();
sp.BaudRate = 2400;
sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Prijem);
if (!sp.IsOpen)
sp.Open();
}
private delegate void UpdateUiTextDelegate(char text);
private void Prijem(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
char c = (char)sp.ReadChar();
Dispatcher.Invoke(DispatcherPriority.Send,
new UpdateUiTextDelegate(WriteData), c);
}
private void WriteData(char c)
{
if (c == 'A')
{
checkBox1.IsChecked = true;
}
else if (c == 'a')
{
checkBox1.IsChecked = false;
}
}
}
When the DataReceived event is thrown it is not guaranteed how many characters are within the buffer. So if you simply call ReadChar() you don't read the full content of the buffer. So if the characters are send quite fast it would be possible that you miss something, cause your event handler is called when two or more characters are within the buffer.
Also you should set ALL serial port properties and not just the baud rate. This is needed cause the serial port has no default state and will remain the last set option for each parameter. So if you use some terminal program to change some lesser used settings (like Xon/off, HW Handshake, StartBits, etc.) your program will simply use the same settings if you don't reset them to your desired values.
The SerialPort properties are not all set. Asign a port number.

How can I receive data from a PC to an Arduino?

I developed an application that sends data for an Arduino by the serial port, but I can't understand how I can receive it on the Arduino. I send a string by the serial port for the Arduino and the Arduino receives it, but it does not work in my code (on the Arduino, I receive a byte at a time).
Update: it's working ;)
The code in C# that sends data:
using System;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Ports;
pulic class senddata() {
private void Form1_Load(object sender, System.EventArgs e)
{
//Define a serial port.
serialPort1.PortName = textBox2.Text;
serialPort1.BaudRate = 9600;
serialPort1.Open();
}
private void button1_Click(object sender, System.EventArgs e)
{
serialPort1.Write("10"); //This is a string. The 1 is a command. 0 is interpeter.
}
}
The Arduino code:
I have Update the Code
#include <Servo.h>
Servo servo;
String incomingString;
int pos;
void setup()
{
servo.attach(9);
Serial.begin(9600);
incomingString = "";
}
void loop()
{
if(Serial.available())
{
// Read a byte from the serial buffer.
char incomingByte = (char)Serial.read();
incomingString += incomingByte;
// Checks for null termination of the string.
if (incomingByte == '0') { //When 0 execute the code, the last byte is 0.
if (incomingString == "10") { //The string is 1 and the last byte 0... because incomingString += incomingByte.
servo.write(90);
}
incomingString = "";
}
}
}
Some things which make my eyebrow raise:
serialPort1.Write("1");
This will write exactly one byte, the 1, but no newline and no trailing NUL-Byte.
But here you are waiting for an additional NUL byte:
if (incomingByte == '\0') {
You should use WriteLine instead of Write, and wait for \n instead of \0.
This has two side effects:
First: If there is some buffering configured, then there is a certain chance, than a new line will push the buffered data to the Arduino. To be certain you have to dig through the docs at MSDN.
Second: This makes your protocol ASCII-only. This is important for easier debugging. You can then use a plain terminal program like Hyperterm or HTerm (edit) or even the Serial Monitor in the Arduino IDE itself (edit) to debug your Arduino-Code without worrying about bugs in your C# code. And when the Arduino code works you can concentrate on the C# part. Divide et impera.
Edit: Another thing I noticed after digging out my own Arduino:
incomingString += incomingByte;
....
if (incomingByte == '\n') { // modified this
if(incomingString == "1"){
This will of course not work as expected, because the string will contain "1\n" at this point. Either you compare to "1\n" or move the += line after the if.
You could alternatively try using the Firmata library - it's a much better way of having standard firmware on the Arduino and managing it from .net
I believe, Firmata 2.0+ has support for I2C and servo control.
http://firmata.org/

Categories