Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Arduino canot control computer.

Arduino canot control computer.

Scheduled Pinned Locked Moved C#
helpcsharpsharepointhardwareworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15493823
    wrote on last edited by
    #1

    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...");

    OriginalGriffO 1 Reply Last reply
    0
    • M Member_15493823

      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...");

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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 the jb_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!

      "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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups