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

Serialport.read


  • Please log in to reply
26 replies to this topic

#1 Jon M

Jon M

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationNorwood Young America, MN

Posted 16 July 2011 - 07:04 PM

I need help with serialport.read. I’m sending a serialport.write ("TSTG") out of a VB app to the netduino thru an xbee shield. I’m receiving the bytes but I’m having trouble with converting it back to a text. Will the final version have serialport.readline option.

Thanks,
Jon

    Private Sub Data_Received(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        Try
            Dim i As Integer
            Dim n As Integer = sPort.BytesToRead
            Dim buffer() = New Byte(n - 1) {}
            sPort.Read(buffer, 0, n)
            For count = 0 To (n - 1)
                DataIn = CStr(buffer(i))
                Debug.Print(DataIn)
                i = i + 1
            Next count
        Catch ex As Exception
            Debug.Print(ex.Message.ToString())
        End Try
    End Sub


#2 Joe Griffith

Joe Griffith

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 17 July 2011 - 09:59 PM

Here's a program that will accept a string from the serial port and turn the LED on or off

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports System.IO.Ports
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino
Imports System.Text
Imports Microsoft.VisualBasic.Constants

Module Module1

  Private WithEvents UART As SerialPort
  Private DataFromPC As String = ""
  Private LED As New OutputPort(Pins.ONBOARD_LED, False)

  Sub Main()
    UART = New SerialPort("COM1", 19200, Parity.Even, 8, StopBits.One)
    UART.Open()
    Thread.Sleep(Timeout.Infinite)
  End Sub

  Private Sub SerialIn() Handles UART.DataReceived
    Dim Length As Integer = UART.BytesToRead
    Dim Datax(Length) As Byte

    UART.Read(Datax, 0, Length)
    DataFromPC &= New String(Encoding.UTF8.GetChars(Datax))

    If DataFromPC.IndexOf(vbLf) >= 0 Then
      DataFromPC = DataFromPC.Trim.ToUpper
      If DataFromPC = "ON" Then
        LED.Write(True)
      ElseIf DataFromPC = "OFF" Then
        LED.Write(False)
      Else
      End If
      DataFromPC = ""

    End If

  End Sub

End Module


#3 Jon M

Jon M

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationNorwood Young America, MN

Posted 18 July 2011 - 12:23 AM

Here's a program that will accept a string from the serial port and turn the LED on or off


Thanks for the help Joe, VB2010 is a little different then VB6.

#4 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 19 July 2011 - 09:28 PM

Here's a program that will accept a string from the serial port and turn the LED on or off

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports System.IO.Ports
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino
Imports System.Text
Imports Microsoft.VisualBasic.Constants

Module Module1

  Private WithEvents UART As SerialPort
 

Which one of the Imports is supposed to have "SerialPort" ?
I get "Type 'SerialPort' is not defined."

Am I missing someting?
References, files or just my mind?

#5 Stefan

Stefan

    Moderator

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

Posted 20 July 2011 - 06:49 AM

Hi Bonzo, welcome to the boards :) It's quite confusing, but here it is. The reference is "Microsoft.SPOT.Hardware.SerialPort" But the imports line is "Imports System.IO.Ports" I know they're not named the same, but that's how it is.
"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

#6 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 20 July 2011 - 09:52 AM

Hi Bonzo, welcome to the boards :)

Thank you!
It's been a while since I followed boards like this, but this one looks promising!

The reference is "Microsoft.SPOT.Hardware.SerialPort"
But the imports line is "Imports System.IO.Ports"


Well, as you'll see from my screenshot;
Attached File  no_serialport.png   105.78KB   115 downloads
I have all of the imports, and I've also tried to use the entire reference but still nothing.

Might I have a corrupt install?
Are there any settings somewhere that I've missed?
(I think) I followed the instructions for installing the new SDK and firmware as found on this site.


EDIT:
Could it be that I'm using the Netduino Plus?

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 July 2011 - 10:05 AM

Hi Bonzo, Try typing: "System.IO.Ports.SerialPort" That should be the class you're looking for :) It's in the Microsoft.SPOT.Hardware.SerialPort.dll assembly--but the actual class is just "SerialPort." [You can also just type SerialPort without the full namespace before it, since you have the Imports statement up top.] Chris

#8 Stefan

Stefan

    Moderator

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

Posted 20 July 2011 - 10:34 AM

Hi Bonzo, Right-click on NetduinoPlusApplication1 in the Solution Explorer and select Add Reference. Then select Microsoft.SPOT.Hardware.SerialPort, press OK, and try again :)
"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

#9 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 20 July 2011 - 10:38 AM

Hi Bonzo,

Right-click on NetduinoPlusApplication1 in the Solution Explorer and select Add Reference. Then select Microsoft.SPOT.Hardware.SerialPort, press OK, and try again :)


That worked perfectly!

Ethernet + Events + Serialport = (near) infinite possibilities

Thanks a lot!

#10 liqdfire

liqdfire

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts

Posted 09 September 2011 - 01:26 AM

The easiest way to convert it back to text is

Dim message as new String(System.Text.Encoding.UTF8.GetChars(<BYTE ARRAY>))


#11 Paul Bates

Paul Bates

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationDearborn, Mi

Posted 27 September 2011 - 01:29 PM

Hi, What's the approach for this code to use it to use it a as a remote serial port for a PC? I want to have a COMx: on my PC, and remotely open it over ET on my LAN using one of the uarts on the netduion + Thanks, Paul

#12 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 September 2011 - 01:55 PM

Hi,
What's the approach for this code to use it to use it a as a remote serial port for a PC? I want to have a COMx: on my PC, and remotely open it over ET on my LAN using one of the uarts on the netduion +

Thanks,
Paul


Are you saying that you want to make your computer send data to COM5 and have it come out of the Netduino Plus's UART, using the LAN to carry the data?
If so, you'd need some kind of COM-to-IP software on your PC first, to make it send the data via LAN instead of a physical COM port.
After that, you'd want to combine the code in this thread with code that reads the data you sent via your LAN connection.

The Netduino Plus would essentially only become an RS232 extender that operates over IP.
If this is what you want, and only this, there are more suitable devices out there.

If not, check out the webserver examples as well, as they contain code for managing IP traffic.

Or did I read your question all wrong?

#13 Paul Bates

Paul Bates

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationDearborn, Mi

Posted 27 September 2011 - 02:00 PM


Or did I read your question all wrong?


You got it right. I want to read and write the port. I've seen examples where .NET is used on the pc and includes libraries to open com sessions from the netduion. Since my target environment is .net (I should have said that earlier), it seems like code on the netduion to read from one side and write to the other would do it. The code on the netduion does not need to think about the stream at all, just move it.

#14 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 September 2011 - 07:24 AM

You got it right. I want to read and write the port. I've seen examples where .NET is used on the pc and includes libraries to open com sessions from the netduion. Since my target environment is .net (I should have said that earlier), it seems like code on the netduion to read from one side and write to the other would do it. The code on the netduion does not need to think about the stream at all, just move it.


You can use just about anything to trigger the Netduino, that's no problem.
But are you trying to make the link "transparent" in a way that lets you run lets say HyperTerminal on the computer, open a connection on COM5 (or what ever number) and automagically talk to the device connected to the Netduino via LAN?
Or is there more to it than that?
Do you have other functionality planned as well?

#15 Paul Bates

Paul Bates

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationDearborn, Mi

Posted 28 September 2011 - 12:51 PM

You can use just about anything to trigger the Netduino, that's no problem.
But are you trying to make the link "transparent" in a way that lets you run lets say HyperTerminal on the computer, open a connection on COM5 (or what ever number) and automagically talk to the device connected to the Netduino via LAN?
Or is there more to it than that?
Do you have other functionality planned as well?


Hi. You have it right, I want to create a transparent serial port experience from the pc to one of the UARTs on the netduion across my LAN. I have an rs-232 based device that I want to operate remotely from the PC. There is software on the PC that is .net based that can be programmed (homeseer).

To your point, I have a number of other items to integrate with the netduio as well and conceptually I get how to do that based on the sample code and manuals. This one I did not get conceptually how to approach it from an end to end perspective.

I'm thinking there is already com: device provided by USB the netduion and simple code to be a bridge between the PC and the UART can be written.. is it that simple? But when its not USB but Ethernet connected, what provides the virutal serial port on PC side? .net

#16 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 September 2011 - 01:11 PM

I'm thinking there is already com: device provided by USB the netduion and simple code to be a bridge between the PC and the UART can be written.. is it that simple? But when its not USB but Ethernet connected, what provides the virutal serial port on PC side? .net


I just have to mention once more that other hardware might be more suitable, but I do understand the joy of a good challenge and learning experience so here goes;

Your solution would (probably) consist of three pieces of software and two pieces of hardware.

Hardware:
-Netduino Plus
-Device to be controlled

Software:
-Netduino Plus software
-Virtual COM
-Homeseer

You've got the hardware, and you've got 1 of 3 softwares.

The Netduino Plus software would have a rather simple function set:
-Receive a byte via LAN, send it to UART
-Receive a byte via UART, send it to LAN
This can almost be achieved by cutting/pasting large parts of the two example codes.

The Virtual COM is probably a bit harder.
It must create a virtual COM port that Homeseer can connect to.
I really don't have any idea how to solve that one, sorry.

Once that has been achieved, it's basically the Netduino software all over again...
-Receive a byte via LAN, send it to Virtual COM
-Receive a byte via Virtual COM, send it to LAN

If Homeseer can communicate via TCP/IP directly, there might be a simpler solution.
What kind of hardware is it you need to talk to?
I'm asking, to find out what kind of datatransfer we're talking about.

#17 Paul Bates

Paul Bates

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationDearborn, Mi

Posted 28 September 2011 - 02:55 PM

I hear you... and great questions. I have looked at lan based serial port redirectors. It has become chip based, reliable and cheap from China. However there will be multiple things going on at the location where the netduino+ will go. I will use netduino+'s IO pins for sensing and activating other unrelated devices, and I know how to proceed with those. I have one lan cable in the vacinity. It's about the swiss army knife capabilities of the netduino+; to be able to put one cheap device at the location to solve multipe needs. The serial device is a very simple X10 RF bridge, signals come to a receiver which has a 9600 bps connection via a db25 connector. When an x10 rf device transmits and is detected by the receiver, it creates a small (around 10) coherent set of characters that indicates what address transmitted and short command (on or off). These trasmissions are important, but very infrequent. I have a Homeseer plugin that looks for a com port on the PC to read those characters and takes it from there. I want to build a bridge, so to speak, from one end to the other And, to a certain degree, yes its the challenge of doing it. But there a number of different similar serial devices that could be attached the same way. There could be a lot of value in this capability beyond my needs. I brought up the regular, non-plus netduino usb as a way of saying I think i know how to do it via usb using the usb com port provided when using the regular (non plus) net duion. But if my actual "use case" is putting the netduino further from the computer, I have to use a netduino + and figure out how to add that capability via TCP/IP. Homeseer is built on windows .net, so yes the TCP/IP capability is there, I can leverage whatever .net brings to the table there. That seems to be where this discussion is leading. I appreciate your thoughts, its helping me sort out the "architecture" of this function.

#18 Steve_Exton

Steve_Exton

    New Member

  • Members
  • Pip
  • 1 posts

Posted 05 October 2011 - 03:30 AM

I have used this http://www.eterlogic...ducts.VSPE.html as a virtual serial port emulator on several machines and for several applications with some success. It can possibly handle the virtual com port on the PC side, and then you could make the netduino talk to this via the lan. I have found it to be quite a nice bit of software, and the 32 bit version is free (which is always a bonus!).

#19 JeroenG

JeroenG

    Member

  • Members
  • PipPip
  • 13 posts

Posted 13 November 2012 - 07:55 AM

Here's a program that will accept a string from the serial port and turn the LED on or off

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports System.IO.Ports
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino
Imports System.Text
Imports Microsoft.VisualBasic.Constants

Module Module1

  Private WithEvents UART As SerialPort
  Private DataFromPC As String = ""
  Private LED As New OutputPort(Pins.ONBOARD_LED, False)

  Sub Main()
    UART = New SerialPort("COM1", 19200, Parity.Even, 8, StopBits.One)
    UART.Open()
    Thread.Sleep(Timeout.Infinite)
  End Sub

  Private Sub SerialIn() Handles UART.DataReceived
    Dim Length As Integer = UART.BytesToRead
    Dim Datax(Length) As Byte

    UART.Read(Datax, 0, Length)
    DataFromPC &= New String(Encoding.UTF8.GetChars(Datax))

    If DataFromPC.IndexOf(vbLf) >= 0 Then
      DataFromPC = DataFromPC.Trim.ToUpper
      If DataFromPC = "ON" Then
        LED.Write(True)
      ElseIf DataFromPC = "OFF" Then
        LED.Write(False)
      Else
      End If
      DataFromPC = ""

    End If

  End Sub

End Module


This code does not work for me. When the code is loading the netduino gives always an error. I work with VB.net 2010

#20 Stefan

Stefan

    Moderator

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

Posted 13 November 2012 - 08:03 AM

This code does not work for me. When the code is loading the netduino gives always an error. I work with VB.net 2010

Which error? On what line? :)
"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




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.