Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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# :
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 :

Objective-C
/*
 * 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 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.
Posted
Updated 11-Sep-14 15:24pm
v2

1 solution

Where you open the port add the line:

C++
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);


http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx[^]

Use BeginInvoke()instead of Invoke().

You should examine the examples with SerialPort in the msdn.

This may be of help:

SerialComms.Manager - A serial communications plug-in for .NET[^]
 
Share this answer
 
Comments
Member 11075997 12-Sep-14 9:23am    
thx for your reply !
i do the same thing u said ! i use begininvoke instead invoke ! and add that line after i open the port ! but it doesnt work :'(
unfortunately this app doesnt work for me too :( ! it find my port name but i still cant receive anything :( ! i dont know how is going on here... why my source work good with another standard terminal app but in c# i have this problem ? :( !
Member 11075997 12-Sep-14 13:53pm    
isnt any suggestion guys ????? !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900