Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Serial communication problem


  • Please log in to reply
1 reply to this topic

#1 Nemanja

Nemanja

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 August 2013 - 05:04 PM

Hello everyone,

recently i bought jy-mcu usb to serial adapter for arduino, thinking that i could connect it to my netduino.

but i got some problems. Netduino sends and receives data, but only zeros. When pc sends data to netduino it receives only zeros, it also happend when netduino sends data to the computer.

I think the number of bytes that are send and receives is ok because when i send one letter from pc to netduino, he only receive one byte, when send two letter there are two bytes (two "0"s) and so on....

 

 

this is my code for netduino:
-----------------------------------------------------------------------------------------------------------------

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO;
using System.IO.Ports;

namespace NetduinoApplication9
{
  public class Program
  {
  static SerialPort port;
  public static void Main()
  {
  // write your code here
  port  = new SerialPort(SerialPorts.COM1, 9600, Parity.None,8 , StopBits.One);
  byte[] txtBuff = System.Text.Encoding.UTF8.GetBytes(t);
  port.ReadTimeout = 0;
  port.Open();
  port.DataReceived += new SerialDataReceivedEventHandler(Data_Received);
  OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
  for (int i = 0; i < 3; i++)
  {
  led.Write(true);
  Thread.Sleep(250);
  led.Write(false);
  Thread.Sleep(250);
  }
  Thread.Sleep(Timeout.Infinite);
 

 
  }

  static void Data_Received(object sender, SerialDataReceivedEventArgs e)
  {
  Thread.Sleep(100);
  byte[] b = new byte[1];
  while (port.BytesToRead > 0)
  {
  port.Read(b, 0, b.Length);
  port.Write(b, 0, b.Length);
  }
  }

  }
}

-----------------------------------------------------------------------------------------------------------------------------

 

 

and code for pc app:

------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication6
{
  public partial class Form1 : Form
  {

static SerialPort comport;

  public Form1()
  {
  InitializeComponent();
  }



  private void button1_Click(object sender, EventArgs e)
  {
// .Write("LED=1r");
  comport.Write("a");
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  //com6 = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
  comport = new SerialPort("COM6", 9600 , Parity.None, 8, StopBits.One);
  comport.Open();
  comport.DataReceived += new SerialDataReceivedEventHandler(Data_Received);
  }

  private void button2_Click(object sender, EventArgs e)
  {
  comport.Write("LED=0r");
  }

  private void button3_Click(object sender, EventArgs e)
  {
  comport.WriteLine("3r");
  }

  static void Data_Received(object sender, SerialDataReceivedEventArgs e)
  {
  // wait a little for the buffer to fill
  System.Threading.Thread.Sleep(100);

  // create an array for the incoming bytes
  byte[] bytes = new byte[comport.BytesToRead];
  // read the bytes
comport.Read(bytes, 0, bytes.Length);
  // convert the bytes into a string
  string line = System.Text.Encoding.UTF8.GetString(bytes);
  }
  }
}

-------------------------------------------------------------------------------------------------------------------------------------

 

adapter is connected with netduino like this

adapter netduino

TX   RX(D0)

RX   TX(D1)

  GND GND

 

there are also 3.3V, VBUS, DTR pins on adapter and i'm not really sure what they mean.

 

any advice would be appreciated.

Nemanja.
 



#2 Yanoch

Yanoch

    New Member

  • Members
  • Pip
  • 3 posts

Posted 28 August 2013 - 10:55 AM

Try using System.Text.Encoding.ASCII instead of System.Text.Encoding.UTF8 in the neduino app and in the pc app.

 

Also, in the netduino app this:

 

byte[] b = new byte[1];

should be:

 

byte[] b = new byte[port.BytesToRead];






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.