c#和ATMEGA16之间串行通信的问题! [英] problem with serial communicate between c# and ATMEGA16!
问题描述
大家好......
我有c#serial组件的问题!
我想用atmega16将数据发送到pc接收到c#并在文本框中显示!
我将此代码用于c#:
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));
}
}
}
这是我在atmel的avr来源studio 6非常简单,ork完美地发送'a'实际值:
and this is my avr source in atmel studio 6 its very simple and ork perfectly send 'a' in real term :
/*
* GccApplication7.c
*
* Created: 7/12/2014 4:13:29 AM
* Author: arash
*/
#include <avr/io.h>
#define F_CPU 7372800// Clock Speed
#define BAUD 9600
#define MYUBRR (F_CPU/16/BAUD)-1
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned int data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
int main(void)
{
USART_Init(MYUBRR );
while(1){
USART_Transmit('a');
}
}
i知道我连接的设备的端口名称是COM6,当我使用时,一切正常打开像真实术语或超级终端这样的标准终端软件!我可以收到我的炭火!甚至接收我的mpu6050模块数据(因为我在这个传感器上工作),但是当我在c#中打开我自己的应用程序时,端口成功打开我可以将数据发送到atmega16而没有任何问题但是在收到这是我的全部问题! 我无法收到任何东西:(我很开心,我不知道我能做什么...我在这个网站上使用任何示例源和程序,但没有什么对我有用..!PLZ任何人都可以帮我这个吗?
注意:我在通信的第2方同步了波特率!但我没有收到任何东西!
thx很多。
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.
推荐答案
在那里打开端口添加以下行:
Where you open the port add the line:
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
http://msdn.microsoft.com/en-us/library/ system.io.ports.serialport.datareceived(v = vs.110).aspx [ ^ ]
使用Beg inInvoke()而不是Invoke()。
您应该在msdn中查看SerialPort的示例。
这可能会有所帮助:
SerialComms.Manager - 用于.NET的串行通信插件 [ ^ ]
这篇关于c#和ATMEGA16之间串行通信的问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!