Go Down

Topic: send byte array via com (Read 6560 times) previous topic - next topic

malin

Hi,


I want to send via com from PC(C# application) array of byte (100 elements in array), what is the best method for do this ?

I tray do it in loop, but something is wrong becuse when i read message from arduni i take result that when I send example arrayExampe[30]=220, i receive from arduino message that he has 3 elements 2,2,0.

here is code in PC:

Code: [Select]
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 WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
         
           byte[] arrByte= new byte[96]{1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,1, 16, 32, 55, 80, 110, 140, 190,190, 190, 190, 190, 190, 190, 190, 190,190, 190, 190, 190, 190, 190, 190, 190,190, 190, 190, 190, 190, 190, 190, 190,190, 190, 190, 190, 190, 190, 190, 190,190, 190, 190, 190, 190, 190, 190, 190, 190, 110, 90, 70, 55, 32, 16, 1, 1, 1, 1, 1, 1, 1, 1, 1};




           SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
           port.ReadBufferSize = 128;
           port.Open();
           string read;
           for (int i = 0; i < arrByte.Length; i++)
           {
               
               port.Write(arrByte[i].ToString());
               read = port.ReadLine();
               textBox1.Text = textBox1.Text + read+", " + "\r\n";
               port.Write(",");
             

           }
          port.Close();
       }
   }
}


Here us Arduino simple program:
Code: [Select]

byte incomingByte = 0;      // for incoming serial data

void setup() {
     Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}

void loop() {

     // send data only when you receive data:
     if (Serial.available() > 0) {
           // read the incoming byte:
           incomingByte = Serial.read();

           // say what you got:
           Serial.print("I received: ");
           Serial.println(incomingByte);
     }
}



Thanks for answers.

best regards
malin


Go Up