hi guys ...
i have a problem with c# serial component !
i want to send data with atmega16 into pc receive that with c# and show on a textbox !
i use this code for c# :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace Serial_test
{
public partial class Form1 : Form
{
string strRecieve;
public Form1()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOpen_Click(object sender, EventArgs e)
{
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM6";
serialPort1.Open();
}
private void btnClosePort_Click(object sender, EventArgs e)
{
serialPort1.Close();
MessageBox.Show("PORT Closed", "OK", MessageBoxButtons.OK);
}
private void btnSend_Click(object sender, EventArgs e)
{
serialPort1.WriteLine(textBox1.Text);
}
private void DisplayText(object sender, EventArgs e)
{
textBox2.AppendText(strRecieve);
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
strRecieve = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}
and this is my avr source in atmel studio 6 its very simple and ork perfectly send 'a' in real term :
#include <avr/io.h>
#define F_CPU 7372800
#define BAUD 9600
#define MYUBRR (F_CPU/16/BAUD)-1
void USART_Init( unsigned int ubrr)
{
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
UCSRB = (1<<RXEN)|(1<<TXEN);
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned int data )
{
while ( !( UCSRA & (1<<UDRE)) );
UDR = data;
}
int main(void)
{
USART_Init(MYUBRR );
while(1){
USART_Transmit('a');
}
}
i know that my port com name of my device connected is COM6 allthing works perfectly when i open an standard terminal software like real term or hyper terminal ! i can receive my char ! and even receive my mpu6050 module data (because im work on this sensor), but when i open my own app in c# the port open succesfully i can send data to atmega16 without any problem but in receive this is all my problem ! " i cant receive anything" :( im so comfusing and i dont know what can i do... i use any example source and program in this website but nothing work for me.. !plz anybody can help me about this ?
note: im synchronized the baudrate in the 2 side of communicate! but i dont receive anything !
thx a lot.