Arduino canot control computer.
-
Hi, please help my. I am try 2 days to solve main problem. Arduino device send jb_ready and this IF function dont work:
if (jb_serial_read == "jb_ready")
{
MessageBox.Show("it work");
}What i am want: I am want control computer with arduino device. Because arduino device helo peoples with musculary dystrofi control computer. For control computer need working IF function. C# code:
using System;
using System.IO.Ports;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace kp71
{
internal class Ai
{
private string jb_serial_read;
private SerialPort Jb_serial;public void Start\_comunication\_with\_jbhid() { Jb\_serial = new SerialPort("COM3", 9600) { ReadTimeout = 1000, WriteTimeout = 1000, Encoding = Encoding.GetEncoding("iso-8859-1") }; Jb\_serial.DataReceived += Jb\_serial\_DataReceived; Jb\_serial.Open(); } public void Help\_enable() { if (jb\_serial\_read == "ok") { MessageBox.Show("it work"); } } private void Jb\_serial\_DataReceived(object sender, SerialDataReceivedEventArgs e) { Task jb\_hid = new Task(() => { try { SerialPort sp = (SerialPort)sender; jb\_serial\_read = Jb\_serial.ReadExisting(); } catch (Exception Error) { } }); jb\_hid.Start(); } private void Send\_data(string jb\_serial\_write) { Jb\_serial.WriteLine(jb\_serial\_write); } }
}
Arduino code:
#include #include SoftwareSerial mySerial(10, 11);
// Global digital reserved posts
#define cts 6
#define Watter_pump 7
#define Food_pump 8
#define Alarm 9// Global analog reserved posts
#define EMG A7;// CMD
String Command;
String Value;
String cmd;void setup() {
// Setup digital pinmode
pinMode(Alarm, OUTPUT);
pinMode(Food_pump, OUTPUT);
pinMode(Watter_pump, OUTPUT);
pinMode(cts, INPUT);// Serial budrate setup
Serial.begin(9600);
mySerial.begin(9600);// Default start string for pc
Serial.println("jbHID ready..."); -
Hi, please help my. I am try 2 days to solve main problem. Arduino device send jb_ready and this IF function dont work:
if (jb_serial_read == "jb_ready")
{
MessageBox.Show("it work");
}What i am want: I am want control computer with arduino device. Because arduino device helo peoples with musculary dystrofi control computer. For control computer need working IF function. C# code:
using System;
using System.IO.Ports;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace kp71
{
internal class Ai
{
private string jb_serial_read;
private SerialPort Jb_serial;public void Start\_comunication\_with\_jbhid() { Jb\_serial = new SerialPort("COM3", 9600) { ReadTimeout = 1000, WriteTimeout = 1000, Encoding = Encoding.GetEncoding("iso-8859-1") }; Jb\_serial.DataReceived += Jb\_serial\_DataReceived; Jb\_serial.Open(); } public void Help\_enable() { if (jb\_serial\_read == "ok") { MessageBox.Show("it work"); } } private void Jb\_serial\_DataReceived(object sender, SerialDataReceivedEventArgs e) { Task jb\_hid = new Task(() => { try { SerialPort sp = (SerialPort)sender; jb\_serial\_read = Jb\_serial.ReadExisting(); } catch (Exception Error) { } }); jb\_hid.Start(); } private void Send\_data(string jb\_serial\_write) { Jb\_serial.WriteLine(jb\_serial\_write); } }
}
Arduino code:
#include #include SoftwareSerial mySerial(10, 11);
// Global digital reserved posts
#define cts 6
#define Watter_pump 7
#define Food_pump 8
#define Alarm 9// Global analog reserved posts
#define EMG A7;// CMD
String Command;
String Value;
String cmd;void setup() {
// Setup digital pinmode
pinMode(Alarm, OUTPUT);
pinMode(Food_pump, OUTPUT);
pinMode(Watter_pump, OUTPUT);
pinMode(cts, INPUT);// Serial budrate setup
Serial.begin(9600);
mySerial.begin(9600);// Default start string for pc
Serial.println("jbHID ready...");Data receive events occur when a character arrives at the coms buffer, not when a message is complete. So when you call
ReadExisting
you will get a string containing all the characters received since the last call - and with modern processors being fast and serial coms being slow (9600 baud means a fastest data transfer rate of 9600 bits per second, which translates to around 960 characters per second at best) the most likely result is that each time it's called you will get one and only ever one character at a time. And since your code overwrites whatever was in thejb_serial_read
string each time the event happens, that means that you will never see the complete message. You need to buffer data, spot teh end of data, and feed it back up the chain as a message instead of character by characters."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!