Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication between c# and arduino

Tags:

c#

arduino

I'm trying to send some bytes to the Serial1 of my arduino MEGA. I'm sending this byte[] writebuffer = { 1, 2, 3, 4 }; but the output of Serial in arduino is 127 191 247 0.

I'm using a DB9, i have connected the GND to GND, Tx to Rx1 and Rx to Tx1 (connections from DB9 to arduino).

Here is my C# code:

SerialPort sepo = new SerialPort("COM6", 9600);
sepo.Open();
byte[] writebuffer = { 1, 2, 3, 4 };
sepo.Write(writebuffer, 0, writebuffer.Length);
sepo.Close();

And this is the arduino code:

void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
}
void loop()
{
  if(Serial1.available())
  {
     while(Serial1.available())
     {
        Serial.print((byte)Serial1.read());
     }
     Serial.println();
     Serial1.println("recibi datos");
  }
} 
like image 766
Clamari Avatar asked Apr 11 '16 17:04

Clamari


People also ask

What are the 2 types of serial communication?

There are two broad types of serial communication: synchronous and asynchronous. There are a very large number of different standards and protocols for serial communication, ranging from the very simple to the seriously complicated.

What is serial communication in computer?

Serial communication is a communication technique used in telecommunications wherein data transfer occurs by transmitting data one bit at a time in a sequential order over a computer bus or a communication channel. It is the simplest form of communication between a sender and a receiver.

What is example of serial communication?

Similarly there are several examples of Serial Communication Protocols such as CAN, ETHERNET, I2C, SPI, RS232, USB, 1-Wire, and SATA etc. In this article, the different types of Serial Communication Protocols will be discussed.


2 Answers

I will suggest you to close serial port before opening and check if it was open or not.

Also you should use ttl usart converter based on max232 or similar, or usb to serial converter based on ft232 or ch340. This is because arduino has 5V ttl serial port, while desktop has 12V port.

like image 187
Vladimir Tsykunov Avatar answered Oct 09 '22 15:10

Vladimir Tsykunov


A straight PC Serial to Arduino connection is not possible because voltages between arduino and PC are different, it won't work. I'm using a FTDI now and it's working perfectly.

like image 43
Clamari Avatar answered Oct 09 '22 17:10

Clamari