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

Uart, unable to read the received Characters


  • Please log in to reply
14 replies to this topic

#1 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 December 2012 - 06:46 AM

Hi I am new to netduino platform. I experiment the environment for a couple of days. In my final project I want to use the device to transfer data between the serial ports to external devices. I managed to program the device using the following instructions: Power * Attach 7.5V-12V power to pin 24 (top-right corner just right of notch at top). And from your power supply, attach ground to pin 23. Programming RS232: cut the end off of an RS232 cable and wire the RX, TX, DTR, and GND wires to pins 1, 2, 3, and 4. In Visual Studio, set the deployment to Serial instead of USB and select your COM port. All that work fine. I even managed to flash the device to firmware NetduinoMini_Firmware_4.2.0.1_RS232, so uart ports behaves well. The steps I follow: 1. Deploy the application from VSstudio2010. (all fine) 2. Test that it works (debugging), all fine. 3. Power off the netduino mini, Close the VS studio. 4. Power up the netduino mini. 5. Wait the mini to boot and open a terminal application (TeraTerm or Real Term) 6. Send Keystrokes from Terminal application. The peculiar behavior starts here…. a. The netduino application enters the event handler SerialDataReceivedEventHandler b. The led flashes. c. I can send hardcoded data back to the PC terminal application, from netduino but… The problem is that there are no data to netduino receive buffer, the .BytesToRead always return zero In the attached excample allways Bytes Readed=0 Could someone help me. Thank you in advance. The Code... using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoMini; using System.IO.Ports; using System.Text; namespace NetduinoMiniApplication3 { public class Program { static bool LedValue; static OutputPort Led = new OutputPort(Cpu.Pin.GPIO_Pin12, false); static SerialPort PCSerial=new SerialPort(SerialPorts.COM2, 1200, Parity.None, 8, StopBits.One); public static void Main() { Thread.Sleep(5000); //To catch up for programming PCSerial.DataReceived += new SerialDataReceivedEventHandler(PCSerialPortDataReceived); PCSerial.Open(); LedValue = true; while (true) { Thread.Sleep(2050); // sleep for 2500ms } } static void PCSerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { //PCSerial.DataReceived -= new SerialDataReceivedEventHandler(PCSerialPortDataReceived); System.Threading.Thread.Sleep(100); // wait a little for the buffer to fill int Len=PCSerial.BytesToRead; string s = "Bytes Readed" + Len.ToString() +'\r'+'\n'; PCSerial.Write(Encoding.UTF8.GetBytes(s), 0, s.Length); Led.Write(LedValue); LedValue = !LedValue; //PCSerial.DataReceived += new SerialDataReceivedEventHandler(PCSerialPortDataReceived); } } }

#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 12 December 2012 - 10:43 AM

maybe the 100msec sleep isent good.
also, are you sure you didnt mess up the portnames? for me com1 is uart, com2 is rs232
i did it like:

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (port.BytesToRead > 0)
            {
                int tmp = port.ReadByte();

                // print tmp
            }
        }

i added the byte i read to an array and got my data this way.

//edit: also try another data rate, 9600 or higher, but from the baudrate defines.

#3 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 December 2012 - 11:16 AM

NooM Thank you for the Reply. I have tried this with no delay and the system is not working. As you can see in the code: string s = "Bytes Readed" + Len.ToString() +'\r'+'\n'; PCSerial.Write(Encoding.UTF8.GetBytes(s), 0, s.Length); I can see the "Bytes Readed0" to my terminal, so I am using the correct port. and also the event fires correctly (I can see the blinking led) I also tried the ReadByte(), but it seems that is doing a blocking read an d the program not proceeds. Do you use mini? If yes have you ever managed to communicate with COM2 pins(1,2) The problem is not to the specific device, because I have tested 3 devices. (even with older firmware)

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 12 December 2012 - 12:17 PM

The problem is not to the specific device, because I have tested 3 devices. (even with older firmware)

The point is that COM1 and COM2 work on different voltage levels. Which COM port on your Mini did you use?
COM1 (pins 1 to 4) work at -12V to 12V
COM2 (pins 11, 12) work at 0V to 3.3V

What does the device want with which you try to communicate?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 12 December 2012 - 12:30 PM

yes i have a mini and i communicated with another netmf device per uart (COM1) - i use COM2 for programming/debugging (pin 1,2) it worked like a charm. i dont have the code anymore cos it was just for testing - it worked in some days i can show you another one, iam going to stream display data from my pc to my netduino mini (for display/rgb matrix - the mini is really slow when using PutPixel) //edit nothing todo with the mini itself, its a great device, mf is slow :P

#6 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 December 2012 - 12:59 PM

The point is that COM1 and COM2 work on different voltage levels. Which COM port on your Mini did you use?
COM1 (pins 1 to 4) work at -12V to 12V
COM2 (pins 11, 12) work at 0V to 3.3V

What does the device want with which you try to communicate?

Hello Stefan


In my netduino mini the port that I manage to send data from mini to PC, is COM2 (with levels -12V to 12V, pins 1 to 4 see code).

Please inform me.

I use the (-12V +12V) port (pin 1..4), lets say COM2 (since this port is the one that worked to my code)

So Com2 is the port that I deploy and debug the application.
I also want to communicate with the PC throught the same port.
This is not possible since debugging and application use the same port.

Here is the question....
After downloading the code to the mini (COM2), I power OFF/ON the mini. Could you please inform me if after that the device enters to debug mode or not. I "supposed", till now, that when the mini does not find VS2010 to communicate it executes the program with no debug features.

And finally I want to use both serial ports (I will shift the levels of pins 11,12 )
in my project. Is it possible to put the mini in a mode that it does not start debuging or waiting for programming (I disconnected the pin 3 - DTR ???)

Sotiris

#7 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 14 December 2012 - 03:36 AM

when you disconnect dtr it dont gets into bootloader mode as soon as you power it on.
i strongly suggest not using the same port for programming than you do for communication.

some serial reading code
ok the code looks messy, but works like a charm (so far, iam now not sending loads of data, its 8-9 bytes, once every while!)

feel free to make it look good :)

#8 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 14 December 2012 - 09:53 AM

i tested around more, and as expected when sending data faster it gets messed up. anyone ideas to this? i could use hardware flow control, but it would need pins wich are hard to spent on the mini. currently iam reading into software flow control, wich seems to be ok too. i also try to make kind of a stream/packet system, so it sorts it out automatically if data from the next packet gets into the one before.

#9 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 14 December 2012 - 12:19 PM

i tested around more, and as expected when sending data faster it gets messed up.

anyone ideas to this?

i could use hardware flow control, but it would need pins wich are hard to spent on the mini.
currently iam reading into software flow control, wich seems to be ok too.
i also try to make kind of a stream/packet system, so it sorts it out automatically if data from the next packet gets into the one before.



Dear All

I have tested the system by downloading the code either by RS232 (+/-12V) or TTL (by using NetduinoMini_Firmware_4.2.0.1_RS232 or NetduinoMini_Firmware_4.2.0.1_TTL).

The system behaves fantastic when using the free serial port (not the one for debugging/downloading) and also can transmit data with the other debugging downloading port.


Whenever I tried to read data from (debugging/downloading) port the system misbehaved.
1. The Received event behaves well
2. The received data:
- Either nothing received to the buffer
- Or small parts of the data arrived to the buffer of the netduino.


1. Since in my project I want 2 serial ports (small baud rates up to 2400), I will give a try for a virtual serial port. By your experience, has the system enough processing power?
2. The bootloader or the Framework use the downloading/debugging serial port?
3. Is it possible to disable from framework/bootloader the usage of serial port? Because the system even it is equipped by two serial ports can only use for reading only one.

I just want to convert received bytes to/from Manchester encoding to drive ac coupled transmit/receive lines.

Thanks

#10 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 17 December 2012 - 10:25 AM


1. Since in my project I want 2 serial ports (small baud rates up to 2400), I will give a try for a virtual serial port. By your experience, has the system enough processing power?


After second thoughts, the above it is not possible since it requires under millisecond timer resolution for baud rates 1200bps, for just one sample and without voting.

#11 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 17 December 2012 - 11:38 AM

iam using 115200 and its fine, when i send a lot of data i get problems reading it, but for like < 100 bytes its fine

#12 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 21 December 2012 - 06:50 AM

Dear All I finally manage to read from RS232-debug/download port. The problem seems to be the DataReceived event mechanism. By using the event mechanism from programming/debugging port the event fires but there are no data to read xxx. BytesToRead allways==0 In the following code I manage to transfer /receive data between the two ports. static SerialPort SerialRS232 = new SerialPort(SerialPorts.COM2, 1200, Parity.None, 8, StopBits.One); static SerialPort SerialTTL = new SerialPort(SerialPorts.COM1, 1200, Parity.None, 8, StopBits.One); public static void Main() { // In order to catchup debug / download Thread.Sleep(5000); SerialTTL.DataReceived += new SerialDataReceivedEventHandler(OtherSerialPortDataReceived); SerialRS232.Open(); SerialTTL.Open(); while (true) { //read from SerialRS232 and Write to SerialTTL int Len = SerialRS232.BytesToRead; if (Len>0) { byte[] b = new byte[Len]; SerialRS232.Read(b, 0, Len); SerialTTL.Write(b, 0, Len); } } } static void OtherSerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { while (SerialTTL.BytesToRead > 0) { int Len = SerialTTL.BytesToRead; byte[] b = new byte[Len]; SerialTTL.Read(b, 0, Len); SerialRS232.Write(b, 0, Len); } } Merry Christmas

#13 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 21 December 2012 - 07:29 AM

i still suggest using a higher speed!

and make it look like this: serial tutorial

you are also reading in your while loop and in the event, dont do this.

also, dont open the port for deployment.

#14 smilios

smilios

    New Member

  • Members
  • Pip
  • 7 posts

Posted 21 December 2012 - 08:04 AM

i still suggest using a higher speed!

and make it look like this: serial tutorial

you are also reading in your while loop and in the event, dont do this.

also, dont open the port for deployment.


1. The speed must be so low because of the requirements...
2. Is there any problem reading from different buffers/serial ports in while and event.

#15 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 21 December 2012 - 08:45 AM

yes, cos it doesent make sense, i guess the while loop is faster than the event, so it reads the data before the event can do. you should du it as the tutorial from my link. it will work than i guess. it worked for me.




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.