Using the serial port of the arduino pro mini with c#

Hello there,
I would like to know if there is a way to send 8 bytes with c# to the serial port and also if it will read them immediately?

The code I was thinking about goes like this in the c#:

for(int i=0;i<8;i++)
myport.Write(buffer.ToString());
/// my serial port = myport

so would the next command will work for receiving in my serial port the whole 8 bytes?:
char data[8] = Serial.readBytes():

I would like to know either if can I blink few leds in the same time for about 2 seconds without coding them in the same place for example:

this is what I don't want to happen:
loop()
{
digitalWrite(13,HIGH);
digitalWrite(10,HIGH);
delay(2000);
digitalWrite(13,LOW);
digitalWrite(10,LOW);
delay(2000);
}

So in the example above 2 leds will blink simultaneously but it's not good for me this way!!
Im using 8 bits after all so I have 256 different options (each bit of the 8 represent a led) and I have an array in my c# program which represent '1' or '0' as 8 bits in the array for example array[8]={1,0,0,1,0,1,1,1,0};
So those '1' and '0' values change randomaly and I want the leds which each number value represent 1 of them to blink twice for 2 sec ON 2 sec OFF if their value is '1' and they will blink 7 times for 100 mili seconds if their value is '0'!!

loop()
{
digitalWrite(13,HIGH);
delay(2000);
digitalWrite(13,LOW);
delay(2000);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
}
the form above doesn't makes the leds to blink together and I have no idea how to make it to happen if it even posibble...

In other words - the delay is interrupting the synchronization between the blinking leds so I can't figure out how to blink them in the same time the way I would like them to blink as I mentioned before.
Writing a configuration for each form is too long (255 options after all) and it is not effective at all!!

Please help me and I hope you will understand my needs because they are too difficult to explain and I couldn't find a solution by myself....

Do a search for 'serial input basics' and 'multiple things at the same time'.

And you can use C# to send the dats; check the SerialPort class.

Note that there is no guarantee that all bytes arrive 'at the same time'; it's possible that you receive e.g. two bytes followed by six; or whatever.

Please use code tags. The forum software has mangled your code.

Look for the example "blink without delay" in your Arduino examples folder. It contains the answer you seek.

sterretje:
Do a search for 'serial input basics' and 'multiple things at the same time'.

And you can use C# to send the dats; check the SerialPort class.

Note that there is no guarantee that all bytes arrive 'at the same time'; it's possible that you receive e.g. two bytes followed by six; or whatever.

I had a bug while posting the problem you read in teh begining before answering to me

please check again my problem and see if you understand or can help me to solve it

Have a look at Serial Input Basics. The method in the third example will be most reliable. There is also a parse example.

...R

Robin2:
Have a look at Serial Input Basics. The method in the third example will be most reliable. There is also a parse example.

...R

Thank you very much for answering to me!
I read your topic but I couldn't find a solution for my questions.
I hope you can answer me for the next questions:

  1. If I want to send to the serial port 8 bytes which presents '1' or '0' as chars each of them , and I want the arduino to read each char and then turning on my leds depend on the value , will the next program will work?

void loop()
{
int i;
char data;
for(i=6;i<13;i++)
{
data=Serial.read();
if (data=='1')
digitalWrite(i,HIGH);
else
if (data=='0')
digitalWrite(i,LOW);
delay(30);
}

the data I'm sending is from c# using visual studio and the configure goes like this:
I have an array a[] which made from integer so I'm using ToString() to make the integer as a char and send it to the serial port.

myport.Open();
for (x = 0; x < 8; x++)
{
myport.Write(a(x).ToString());
Thread.Sleep(10); /// 10 mili seconds delay
}
myport.Close();

So when x=0 I send a[0] as a char , if it's 1 then the serial port will have the ASCII code of 1 in them, if it's 0 the serial port will have the ASCII code of 0 in them.
Now the arduino should serial read the byte right? when i=6 it will read the serial which he will get from a[0] as ASCII but when its a[1]? then the serial should be changed into the serial which a[1] is sending (or maybe not? I don't realy udnerstand how it is working look my third question below), so will the serial from before will be deleted? will the new serial from a[1] will change or interrupt the serial from a[0]? will they work synchronized and when i=6 he will read the whole serial number of a[0] ,and after it is over when i=7 he will read the whole serial number of a[1] and etc without any problems?
I've setup the baud for 9600 both in the arduino and in the c# program.

  1. if(Serial.available()>0)
    should I use it before the Serial.read() function?
    how it helps me? I don't understand 100% what it's actually doing...

  2. Can you give me a good or an better explaination about how the serial ports are working? I can't find a smooth and fast good answer for this question no matter where im searching it for.

Thanks you very much , I hope you will answer me as fast as possible and help me to understand better how to work with the arduino!

will the next program will work?

No. It assumes, incorrectly, that all 8 bytes will be available to be read, on every pass through loop().

In general, loop() might iterate 100,000 times before 8 bytes arrive.

The HardwareSerial class, of which Serial is an instance, has an available() method. Learn what it does.

      delay(30);

If there is serial data to be read, there is no point in sticking your head in the sand. If there isn't, sticking your head in the sand won't make serial data appear.

the data I'm sending is from c# using visual studio

No, you aren't. C# is a programming language. Visual Studio is a program development environment. The APPLICATION that you developed, using C# in Visual Studio can send data to the serial port. C# can not. Visual Studio can not.

Opening the serial port resets the Arduino. You need to have a nap before assuming that it is OK to send data to the serial port.

so will the serial from before will be deleted?

Serial data from before what? Since opening the serial port resets the Arduino, the likely answer is yes, any data that was in the input buffer will be lost.

should I use it before the Serial.read() function?

Absolutely.

Can you give me a good or an better explaination about how the serial ports are working? I can't find a smooth and fast good answer for this question no matter where im searching it for.

The best way to learn is to experiment.

PaulS:
No. It assumes, incorrectly, that all 8 bytes will be available to be read, on every pass through loop().

In general, loop() might iterate 100,000 times before 8 bytes arrive.

The HardwareSerial class, of which Serial is an instance, has an available() method. Learn what it does.

      delay(30);

If there is serial data to be read, there is no point in sticking your head in the sand. If there isn't, sticking your head in the sand won't make serial data appear.
No, you aren't. C# is a programming language. Visual Studio is a program development environment. The APPLICATION that you developed, using C# in Visual Studio can send data to the serial port. C# can not. Visual Studio can not.

Opening the serial port resets the Arduino. You need to have a nap before assuming that it is OK to send data to the serial port.
Serial data from before what? Since opening the serial port resets the Arduino, the likely answer is yes, any data that was in the input buffer will be lost.
Absolutely.
The best way to learn is to experiment.

Thank you very much for the little advices and help,
Unfortunately it's not enough information for me to move on to the next step , I'm still stuck , but at least now I think I can get out of it some how after few hours but it's too long!

I would like to know what do you mean by taking a "nap" before I assume that it is OK to send data to the serial port?
After all my project is in the communication stage and how can I communicate without using the serial port or even understand as much as posibble how they are working?
I don't see what is the problem with the delay in each loop? as you said, if it is happening 100K times a second , then it will interrupt my whole program as much as the baud is going too slow on 9600...
It won't make the whole data I want to get from the serial port , also the serial port will get messed up which isn't actually happend to me in my first experiment but it did happend in my second one!
Also I would like to know how the Serial.available() function will help in my case?
From what I udnerstood until now , if it is bigger than "0" which doesn't even make sense because I didn't saw any explanation why to use 0? how the 0 is helping me to check if there is available Serial? and what does it means?
So anyway from what I udnerstodo about the function aboev - if it is bigger than 0 than it will give a premission to read new data , but if form my c# program I'm sending a byte and the sketch program didn't finished to use it and than the c# program will send another data on the serial than there will be a mistaken data/information bits in my project because as you said, the next serial delete the last serial (by using a buffer)... that what I understood from this function and if it is true than it is not helpfull at all and usless thing that will make a whole wrong information and errors!!!
As much as I know to use more than 1 serial I need to connect another usb to the arduino so I need more than 1?
You need to be more specific if you are willing to help me.

All I can tell you is that my program was working fine , while you said "No. It assume, incorrectly..." but when I combined it with another program and bigger It still worked but not as I wanted to:
Instead turning the leds in the exact formation , they turned on randomaly every try , but after 8 times turning the leds , they all turned on eventually so something is messed up and I can't find what!
If you are saying the opposite to what happen to me and also give me some information which doesnt realy specific to help me than I can't get the full out of it.

Thank you again , I will try to work with what you said here but at least I would like to have more information about my last 3 questions

I could liek to know what do you mean by taking a "nap" before I assume that it is OK to send data to the serial port?b

When you open the serial port, the Arduino resets. That takes time. You need to wait until the Arduino is ready to receive serial data.

There are several ways to do this. The simplest is Thread.sleep() for some time.

Once the Arduino has reset, and is ready to read serial data, you need to make sure that there is data to read before trying to read it. Either do nothing until Serial.available() returns a value greater than one, and then read one byte, or do nothing until Serial.available() returns 8 or more, then read all 8 bytes.

Nitayp1:
I don't see what is the problem with the delay in each loop? as you said, if it is happening 100K times a second , then it will interrupt my whole program as much as the baud is going too slow on 9600...
It won't make the whole data I want to get from the serial port , also the serial port will get messed up which isn't actually happend to me in my first experiment but it did happend in my second one!
Also I would like to know how the Serial.available() function will help in my case?
From what I udnerstood until now , if it is bigger than "0" which doesn't even make sense because I didn't saw any explanation why to use 0? how the 0 is helping me to check if there is available Serial? and what does it means?
So anyway from what I udnerstodo about the function aboev - if it is bigger than 0 than it will give a premission to read new data , but if form my c# program I'm sending a byte and the sketch program didn't finished to use it and than the c# program will send another data on the serial than there will be a mistaken data/information bits in my project because as you said, the next serial delete the last serial (by using a buffer)... that what I understood from this function and if it is true than it is not helpfull at all and usless thing that will make a whole wrong information and errors!!!
As much as I know to use more than 1 serial I need to connect another usb to the arduino so I need more than 1?
You need to be more specific if you are willing to help me.

Yes, it's easy for the regulars (including myself) to forget that we had to learn everything too.

First you need to understand the concept that the Arduino, despite being a very low-powered computer, is thousands and thousands of times faster than you. In the time it takes for your eye nerves to detect that a light has switched on, the Arduino has executed a few thousand instructions, which may be checking inputs, doing calculations, making decisions and setting outputs.

So a good Arduino program uses this speed to its advantage. It can check those inputs, outputs or whatever several thousand times per second. One of the inputs it is looking for is "Have any new serial characters arrived for me to process?" That's what Serial.available() does. It tells you how many serial characters have arrived. There's usually zero, sometimes there's one, sometimes there might be 20, depending on exactly what's being transmitted.

There is a buffer inside the Arduino. It can store 64 or more characters (depending on which Arduino you are using.) So just send data as fast as you can from C# and try to process it as quickly as you can. If your Arduino processing takes a long time, for some reason, then there may be many characters in the buffer. Until you hit that magic 64, no characters will be lost, forgotten and they even stay in their proper order.

When you get this speed concept, you will actually find that Serial is relatively slow. You can do a lot of processing in between each character arriving.

Nitayp1:
the data I'm sending is from c# using visual studio and the configure goes like this:
I have an array a[] which made from integer so I'm using ToString() to make the integer as a char and send it to the serial port.

myport.Open();
for (x = 0; x < 8; x++)
{
myport.Write(a(x).ToString());
Thread.Sleep(10); /// 10 mili seconds delay
}
myport.Close();

So when x=0 I send a[0] as a char , if it's 1 then the serial port will have the ASCII code of 1 in them, if it's 0 the serial port will have the ASCII code of 0 in them.

Does not compile in VS2012 :wink: You need to use square brackets for array indexing; but I guess you know that.

Question is why you convert an integer to ascii first?

You want to control 8 LEDs if I understand it correctly. Store the info in a single byte and send that to the Arduino.

// single byte data buffer
byte[] buffer = new byte[1];
// set to 0 (just to make sure)
buffer[0] = 0;
// simulate some data
buffer[0] |= 0xAA;
// send to Arduino
port.Write(buffer, 0, 1);

At the Arduino side, you simply read the received byte, do some logical processing and set the LEDs based on the result.

// array defining the 8 pins that the LEDs are connected to
int ledpins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0)
  {
    byte data = Serial.read();

    // process the 8 bits
    for (int cnt = 0; cnt < 8; cnt++)
    {
      // bit is high
      if ((data & (0x01 << cnt)) == (0x01 << cnt))
      {
        digitalWrite(ledpins[cnt], HIGH);
      }
      // bit is low
      else
      {
        digitalWrite(ledpins[cnt], HIGH);
      }
    }
  }
}

Note:
If you need to control more than 8 LEDs, you need a different approach.

Nitayp1:
Thank you very much for answering to me!
I read your topic but I couldn't find a solution for my questions.

From a quick glance at the length of the subsequent replies I think it will be far more productive if you use my Serial Input Basics to receive your data.

Then you can concentrate your resources on figuring out how to use the data you have received to make your LEDs do what you want.

...R