C# Netduino plus 2, how to send RFID data to windows form listbox through UART? - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
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

C# Netduino plus 2, how to send RFID data to windows form listbox through UART?

windows forms netduino RFID UART C#

Best Answer Nevyn, 03 January 2014 - 03:48 PM

I would look at adding the UART code to the RFID something ike the following:

//UART Project codesusing System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using System.IO.Ports;namespace UART{    public class Program    {        static SerialPort serialPortUART = new SerialPort(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One);        private static void sendDataUsingCOMPort(string _string)        {            byte[] data = System.Text.Encoding.UTF8.GetBytes(_string + "n");            serialPortUART.Open();            serialPortUART.Write(data, 0, data.Length);            serialPortUART.Flush();            serialPortUART.Close();        }        public static void Main()        {            SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);            string id;            serialPort.Open();            while (true)            {                int bytesToRead = serialPort.BytesToRead;                if (bytesToRead > 0)                {                    // get the waiting data                    byte[] buffer = new byte[bytesToRead];                    serialPort.Read(buffer, 0, buffer.Length);                    // print out our received data                    id = new String(System.Text.Encoding.UTF8.GetChars(buffer));                    sendDataUsingCOMPort(id);                    Debug.Print(id);                }                Thread.Sleep(100); // wait a bit so we get a few bytes at a time…            }        }    }}

BTW - as I don't have a reader I have not had the chance to test this.

 

Also, this is not the most efficient way or reading from a serial port.  I'd suggest that you look at using interrupts and assembling a string.  You will need to work out when you have a "complete" buffer of data before tranmitting to then Windows app.

 

Hope this helps,

Mark

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 firefly123

firefly123

    New Member

  • Members
  • Pip
  • 7 posts

Posted 03 January 2014 - 03:14 PM

Solved



#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 03 January 2014 - 03:48 PM   Best Answer

I would look at adding the UART code to the RFID something ike the following:

//UART Project codesusing System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using System.IO.Ports;namespace UART{    public class Program    {        static SerialPort serialPortUART = new SerialPort(SerialPorts.COM2, 9600, Parity.None, 8, StopBits.One);        private static void sendDataUsingCOMPort(string _string)        {            byte[] data = System.Text.Encoding.UTF8.GetBytes(_string + "n");            serialPortUART.Open();            serialPortUART.Write(data, 0, data.Length);            serialPortUART.Flush();            serialPortUART.Close();        }        public static void Main()        {            SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);            string id;            serialPort.Open();            while (true)            {                int bytesToRead = serialPort.BytesToRead;                if (bytesToRead > 0)                {                    // get the waiting data                    byte[] buffer = new byte[bytesToRead];                    serialPort.Read(buffer, 0, buffer.Length);                    // print out our received data                    id = new String(System.Text.Encoding.UTF8.GetChars(buffer));                    sendDataUsingCOMPort(id);                    Debug.Print(id);                }                Thread.Sleep(100); // wait a bit so we get a few bytes at a time…            }        }    }}

BTW - as I don't have a reader I have not had the chance to test this.

 

Also, this is not the most efficient way or reading from a serial port.  I'd suggest that you look at using interrupts and assembling a string.  You will need to work out when you have a "complete" buffer of data before tranmitting to then Windows app.

 

Hope this helps,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 firefly123

firefly123

    New Member

  • Members
  • Pip
  • 7 posts

Posted 03 January 2014 - 07:58 PM

Solved



#4 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 04 January 2014 - 08:53 AM

I wonder if the spurious character at the start and end is to do with the fact that the serial port to the PC is opened and closed every time you want to send a message to the PC.

 

Try opening the com port to the PC at the start of the program (move the open at line 21 to around line 30) and remove the close from the method sendDataUsingCOMPort.

 

As to why you are getting the message twice, I think you will have to single step through the RFID code to see what logic is being executed.  As I say, I can't help there as I do not have a reader to test with.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#5 firefly123

firefly123

    New Member

  • Members
  • Pip
  • 7 posts

Posted 04 January 2014 - 11:20 AM

Solved







Also tagged with one or more of these keywords: windows forms, netduino, RFID, UART, C#

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.