Serial communication between C # and arduino

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

I use DB9, I connected 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"); } } 
+5
source share
2 answers

I suggest you close the serial port before opening and check whether it was open or not.

You should also use a ttl usart converter based on max232 or similar, or usb for a serial converter based on ft232 or ch340. This is because arduino has a 5 V TTL serial port, and the desktop has a 12 V port.

+1
source

Direct connection of a PC with a serial connection to the Arduino is not possible, because the voltages between the arduino and the PC are different, this will not work. Now I am using FTDI and it works great.

+1
source

Source: https://habr.com/ru/post/1246884/


All Articles