locked
J# Serial Port Example RRS feed

  • Question

  • Hi, i'm a 17 year old student from South Africa with school level Java skills. At  school we're currently using Ready to Program, that is not capable of manipulating the serial port. I am using J# 2005 for my radio dispatcher project. Being a novice at programming, could anybody please post me an example of sending and receiving a couple of characters or a string via the serial port. 

    Any help will be appreciated...Thanx

    Thursday, February 22, 2007 9:04 PM

Answers

  •    Hola, espero que este ejemplo te ayude, creeme he pasado largo tiempo buscando algo simple y no lo he encontrado, por supuesto me costo mucho tiempo lograr este  ejemplo para el Serial Port, te mando todo el codigo, pero en esencia solo tienes Form1, button1, button2, textBox1, label1 y serialPort1.

    package SerialPortSample;

    import System.Collections.Generic.*;

    import System.Data.*;

    import System.Drawing.*;

    import System.ComponentModel.*;

    import System.Windows.Forms.*;

    /**

    * Summary description for Form1.

    */

    public class Form1 extends System.Windows.Forms.Form

    {

    private Button button1;

    private TextBox textBox1;

    private Label label1;

    private Button button2;

    private System.IO.Ports.SerialPort serialPort1;

    /**

    * Required designer variable.

    */

    private System.ComponentModel.IContainer components;

    public Form1()

    {

    //

    // Required for Windows Form Designer support

    //

    InitializeComponent();

    //

    // TODO: Add any constructor code after InitializeComponent call

    //

    }

    #region Windows Form Designer generated code

    /**

    * Clean up any resources being used.

    */

    protected void Dispose(boolean disposing)

    {

    if (disposing)

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    super.Dispose(disposing);

    }

    /**

    * Required method for Designer support - do not modify

    * the contents of this method with the code editor.

    */

    private void InitializeComponent()

    {

    this.components = new System.ComponentModel.Container();

    this.button1 = new System.Windows.Forms.Button();

    this.textBox1 = new System.Windows.Forms.TextBox();

    this.label1 = new System.Windows.Forms.Label();

    this.button2 = new System.Windows.Forms.Button();

    this.serialPort1 = new System.IO.Ports.SerialPort(this.components);

    this.SuspendLayout();

    //

    // button1

    //

    this.button1.set_Location(new System.Drawing.Point(151, 40));

    this.button1.set_Name("button1");

    this.button1.set_Size(new System.Drawing.Size(75, 23));

    this.button1.set_TabIndex(0);

    this.button1.set_Text("Send Data");

    this.button1.set_UseVisualStyleBackColor(true);

    this.button1.add_Click(new System.EventHandler(this.button1_Click));

    //

    // textBox1

    //

    this.textBox1.set_Location(new System.Drawing.Point(22, 42));

    this.textBox1.set_Name("textBox1");

    this.textBox1.set_Size(new System.Drawing.Size(100, 20));

    this.textBox1.set_TabIndex(1);

    //

    // label1

    //

    this.label1.set_AutoSize(true);

    this.label1.set_Location(new System.Drawing.Point(22, 97));

    this.label1.set_Name("label1");

    this.label1.set_Size(new System.Drawing.Size(66, 13));

    this.label1.set_TabIndex(2);

    this.label1.set_Text("no new data");

    //

    // button2

    //

    this.button2.set_Location(new System.Drawing.Point(151, 86));

    this.button2.set_Name("button2");

    this.button2.set_Size(new System.Drawing.Size(75, 23));

    this.button2.set_TabIndex(3);

    this.button2.set_Text("Clear Rx");

    this.button2.set_UseVisualStyleBackColor(true);

    this.button2.add_Click(new System.EventHandler(this.button2_Click));

    //

    // serialPort1

    //

    this.serialPort1.add_DataReceived(new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived));

    //

    // Form1

    //

    this.set_AutoScaleDimensions(new System.Drawing.SizeF(6F, 13F));

    this.set_AutoScaleMode(System.Windows.Forms.AutoScaleMode.Font);

    this.set_ClientSize(new System.Drawing.Size(253, 138));

    this.get_Controls().Add(this.button2);

    this.get_Controls().Add(this.label1);

    this.get_Controls().Add(this.textBox1);

    this.get_Controls().Add(this.button1);

    this.set_Name("Form1");

    this.set_Text("Form1");

    this.add_FormClosed(new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed));

    this.add_Load(new System.EventHandler(this.Form1_Load));

    this.ResumeLayout(false);

    this.PerformLayout();

    }

    #endregion

    String Data = new String("No se accedio");

    private void Form1_Load(Object sender, System.EventArgs e)

    {

    serialPort1.Open();

    }

    private void Form1_FormClosed(Object sender, FormClosedEventArgs e)

    {

    serialPort1.Close();

    }

    private void button1_Click(Object sender, System.EventArgs e)

    {

    serialPort1.WriteLine(textBox1.get_Text() );

    }

    private void serialPort1_DataReceived(Object sender,

    System.IO.Ports.SerialDataReceivedEventArgs e)

    {

    Data = serialPort1.ReadExisting();

    this.Invoke(new System.EventHandler(UpdateTextMsg));

    }

    private void UpdateTextMsg(Object sender, System.EventArgs e)

    {

    label1.set_Text(Data);

    }

    private void button2_Click(Object sender, System.EventArgs e)

    {

    label1.set_Text("");

    textBox1.set_Text("");

    }

    }

    Saturday, February 24, 2007 10:39 PM
  • Hi –

     

    Here are some references that can help you get started

    -          http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.aspx

    -          http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=204354&SiteID=1

    -          http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1157458&SiteID=1   

     

    Thanks

    Gaurav

    Friday, February 23, 2007 1:47 PM

All replies

  • For the life of me I haven't had much luck tracking down any good examples of using J# to use a serial port... given though how similar J# is to C#... you may want to start with the C# example on the MSDN page for the SerialPort class.
    Friday, February 23, 2007 12:54 AM
  • Hi –

     

    Here are some references that can help you get started

    -          http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.aspx

    -          http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=204354&SiteID=1

    -          http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1157458&SiteID=1   

     

    Thanks

    Gaurav

    Friday, February 23, 2007 1:47 PM
  •    Hola, espero que este ejemplo te ayude, creeme he pasado largo tiempo buscando algo simple y no lo he encontrado, por supuesto me costo mucho tiempo lograr este  ejemplo para el Serial Port, te mando todo el codigo, pero en esencia solo tienes Form1, button1, button2, textBox1, label1 y serialPort1.

    package SerialPortSample;

    import System.Collections.Generic.*;

    import System.Data.*;

    import System.Drawing.*;

    import System.ComponentModel.*;

    import System.Windows.Forms.*;

    /**

    * Summary description for Form1.

    */

    public class Form1 extends System.Windows.Forms.Form

    {

    private Button button1;

    private TextBox textBox1;

    private Label label1;

    private Button button2;

    private System.IO.Ports.SerialPort serialPort1;

    /**

    * Required designer variable.

    */

    private System.ComponentModel.IContainer components;

    public Form1()

    {

    //

    // Required for Windows Form Designer support

    //

    InitializeComponent();

    //

    // TODO: Add any constructor code after InitializeComponent call

    //

    }

    #region Windows Form Designer generated code

    /**

    * Clean up any resources being used.

    */

    protected void Dispose(boolean disposing)

    {

    if (disposing)

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    super.Dispose(disposing);

    }

    /**

    * Required method for Designer support - do not modify

    * the contents of this method with the code editor.

    */

    private void InitializeComponent()

    {

    this.components = new System.ComponentModel.Container();

    this.button1 = new System.Windows.Forms.Button();

    this.textBox1 = new System.Windows.Forms.TextBox();

    this.label1 = new System.Windows.Forms.Label();

    this.button2 = new System.Windows.Forms.Button();

    this.serialPort1 = new System.IO.Ports.SerialPort(this.components);

    this.SuspendLayout();

    //

    // button1

    //

    this.button1.set_Location(new System.Drawing.Point(151, 40));

    this.button1.set_Name("button1");

    this.button1.set_Size(new System.Drawing.Size(75, 23));

    this.button1.set_TabIndex(0);

    this.button1.set_Text("Send Data");

    this.button1.set_UseVisualStyleBackColor(true);

    this.button1.add_Click(new System.EventHandler(this.button1_Click));

    //

    // textBox1

    //

    this.textBox1.set_Location(new System.Drawing.Point(22, 42));

    this.textBox1.set_Name("textBox1");

    this.textBox1.set_Size(new System.Drawing.Size(100, 20));

    this.textBox1.set_TabIndex(1);

    //

    // label1

    //

    this.label1.set_AutoSize(true);

    this.label1.set_Location(new System.Drawing.Point(22, 97));

    this.label1.set_Name("label1");

    this.label1.set_Size(new System.Drawing.Size(66, 13));

    this.label1.set_TabIndex(2);

    this.label1.set_Text("no new data");

    //

    // button2

    //

    this.button2.set_Location(new System.Drawing.Point(151, 86));

    this.button2.set_Name("button2");

    this.button2.set_Size(new System.Drawing.Size(75, 23));

    this.button2.set_TabIndex(3);

    this.button2.set_Text("Clear Rx");

    this.button2.set_UseVisualStyleBackColor(true);

    this.button2.add_Click(new System.EventHandler(this.button2_Click));

    //

    // serialPort1

    //

    this.serialPort1.add_DataReceived(new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived));

    //

    // Form1

    //

    this.set_AutoScaleDimensions(new System.Drawing.SizeF(6F, 13F));

    this.set_AutoScaleMode(System.Windows.Forms.AutoScaleMode.Font);

    this.set_ClientSize(new System.Drawing.Size(253, 138));

    this.get_Controls().Add(this.button2);

    this.get_Controls().Add(this.label1);

    this.get_Controls().Add(this.textBox1);

    this.get_Controls().Add(this.button1);

    this.set_Name("Form1");

    this.set_Text("Form1");

    this.add_FormClosed(new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed));

    this.add_Load(new System.EventHandler(this.Form1_Load));

    this.ResumeLayout(false);

    this.PerformLayout();

    }

    #endregion

    String Data = new String("No se accedio");

    private void Form1_Load(Object sender, System.EventArgs e)

    {

    serialPort1.Open();

    }

    private void Form1_FormClosed(Object sender, FormClosedEventArgs e)

    {

    serialPort1.Close();

    }

    private void button1_Click(Object sender, System.EventArgs e)

    {

    serialPort1.WriteLine(textBox1.get_Text() );

    }

    private void serialPort1_DataReceived(Object sender,

    System.IO.Ports.SerialDataReceivedEventArgs e)

    {

    Data = serialPort1.ReadExisting();

    this.Invoke(new System.EventHandler(UpdateTextMsg));

    }

    private void UpdateTextMsg(Object sender, System.EventArgs e)

    {

    label1.set_Text(Data);

    }

    private void button2_Click(Object sender, System.EventArgs e)

    {

    label1.set_Text("");

    textBox1.set_Text("");

    }

    }

    Saturday, February 24, 2007 10:39 PM
  • see the same program in many languages (to control a Kaidan emCee controller [for turntables that rotate things]) at:

    http://www.mech.upatras.gr/~robgroup/teams/logic/demos/kaidan/index.html

    the Delphi, VB6 and J# versions use MS Comm ActiveX control, while the Java version uses the Sun Java COMM API

    also search CodeProject site for various examples on serial port access from .NET (easiest way is via using a third part library/driver like input32)

    Monday, February 26, 2007 7:36 PM
  • Hallo Reyes

    This example is exactly what i needed to get started.

    I want to thank you for all your trouble and time.

    Thanks again.

    Geoff

    Friday, March 2, 2007 9:19 PM
  • Hi

     

    I managed to run this code just once, then it never worked after that. The event handler just never kicks in even thou I can see the device is sending data (I can monitor with HyperTerminal). I needed a baud rate of 19200 instead of 9600. The even handler is not working in my own code either. I have tried moving things back and forth in different places but I just can't figure out what's wrong. Can this be a problem with the .Net Framework 3.0? (I'm a noob in MS programming, I usually program in C and on Linux, this is my first windows application).

     

    Code Snippet

    package Serial_Motor_Monitor_GUI;

    import System.Collections.Generic.*;

    import System.Data.*;

    import System.Drawing.*;

    import System.ComponentModel.*;

    import System.Windows.Forms.*;

    import System.IO.Ports.*;

    /**

    * Summary description for Form1.

    */

    public class MotorMonitor extends System.Windows.Forms.Form

    {

    private Label SerialData;

    /**

    * Required designer variable.

    */

    private System.ComponentModel.IContainer components;

    public System.IO.Ports.SerialPort serialPort1 = new System.IO.Ports.SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

    private Label PortMonitor;

    private GroupBox COMPortStats;

    private GroupBox groupBox1;

    private NotifyIcon notifyIcon1;

    private String Data = "";

    public MotorMonitor()

    {

    //

    // Required for Windows Form Designer support

    //

    InitializeComponent();

    //serialPort1.Open();

    //

    // serialPort1

    //

    //this.serialPort1.set_BaudRate(19200);

    this.serialPort1.add_DataReceived(new System.IO.Ports.SerialDataReceivedEventHandler(this.SetMonitorWindow));

    serialPort1.Open();

    //

    SerialData.set_Text("GAME OF FUTURE Team Motor Monitor V1.0 \n http://project.gameoffuture.com");

    PortMonitor.set_Text("BytesToRead: " + serialPort1.get_BytesToRead() + ", Port is Open: " + serialPort1.get_IsOpen() +"\n"

    + "Baud: " + serialPort1.get_BaudRate() + ", ");

    //

    // TODO: Add any constructor code after InitializeComponent call

    //

    }

    //public void SetMonitorWindow(String sData)

    //{

    // SerialData.set_Text(sData);

    //}

    public void SetMonitorWindow(Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

    {

    PortMonitor.set_Text("BytesToRead: " + serialPort1.get_BytesToRead() + ", Port is Open: " + serialPort1.get_IsOpen());

    try

    {

    Data = serialPort1.ReadExisting();

    if (String.Compare(Data, "") == 0)

    {

    SerialData.set_Text("No Data");

    }

    else

    {

    SerialData.set_Text(Data);

    }

    }

    catch (IllegalArgumentException ex)

    {

    SerialData.set_Text("Operation Exception");

    }

    }

    private void GOU_FormClosed(Object sender, FormClosedEventArgs e)

    {

    serialPort1.Close();

    }

    #region Windows Form Designer generated code

    /**

    * Clean up any resources being used.

    */

    protected void Dispose(boolean disposing)

    {

    if (disposing)

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    super.Dispose(disposing);

    }

    /**

    * Required method for Designer support - do not modify

    * the contents of this method with the code editor.

    */

    private void InitializeComponent()

    {

    this.components = new System.ComponentModel.Container();

    this.SerialData = new System.Windows.Forms.Label();

    this.PortMonitor = new System.Windows.Forms.Label();

    this.COMPortStats = new System.Windows.Forms.GroupBox();

    this.groupBox1 = new System.Windows.Forms.GroupBox();

    this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

    this.COMPortStats.SuspendLayout();

    this.groupBox1.SuspendLayout();

    this.SuspendLayout();

    //

    // SerialData

    //

    this.SerialData.set_AutoEllipsis(true);

    this.SerialData.set_AutoSize(true);

    this.SerialData.set_Location(new System.Drawing.Point(6, 16));

    this.SerialData.set_Name("SerialData");

    this.SerialData.set_Size(new System.Drawing.Size(35, 13));

    this.SerialData.set_TabIndex(0);

    this.SerialData.set_Text("label1");

    //

    // PortMonitor

    //

    this.PortMonitor.set_AutoSize(true);

    this.PortMonitor.set_Location(new System.Drawing.Point(8, 16));

    this.PortMonitor.set_Name("PortMonitor");

    this.PortMonitor.set_Size(new System.Drawing.Size(35, 13));

    this.PortMonitor.set_TabIndex(1);

    this.PortMonitor.set_Text("label1");

    //

    // COMPortStats

    //

    this.COMPortStats.get_Controls().Add(this.PortMonitor);

    this.COMPortStats.set_Location(new System.Drawing.Point(4, 190));

    this.COMPortStats.set_Name("COMPortStats");

    this.COMPortStats.set_Size(new System.Drawing.Size(283, 74));

    this.COMPortStats.set_TabIndex(2);

    this.COMPortStats.set_TabStop(false);

    this.COMPortStats.set_Text("COM Port Status:");

    //

    // groupBox1

    //

    this.groupBox1.get_Controls().Add(this.SerialData);

    this.groupBox1.set_Location(new System.Drawing.Point(4, 7));

    this.groupBox1.set_Name("groupBox1");

    this.groupBox1.set_Size(new System.Drawing.Size(283, 183));

    this.groupBox1.set_TabIndex(3);

    this.groupBox1.set_TabStop(false);

    this.groupBox1.set_Text("Message Monitor");

    //

    // notifyIcon1

    //

    this.notifyIcon1.set_Text("Motor Monitor GUI");

    this.notifyIcon1.set_Visible(true);

    //

    // MotorMonitor

    //

    this.set_AutoScaleDimensions(new System.Drawing.SizeF(6F, 13F));

    this.set_AutoScaleMode(System.Windows.Forms.AutoScaleMode.Font);

    this.set_ClientSize(new System.Drawing.Size(292, 266));

    this.get_Controls().Add(this.groupBox1);

    this.get_Controls().Add(this.COMPortStats);

    this.set_Name("MotorMonitor");

    this.set_Text("Form1");

    this.COMPortStats.ResumeLayout(false);

    this.COMPortStats.PerformLayout();

    this.groupBox1.ResumeLayout(false);

    this.groupBox1.PerformLayout();

    this.ResumeLayout(false);

    }

    #endregion

    }

     

    Can someone please kindly help me with this code. I can see the GUI pop up and i can trace the code to the point where I verified that probably this line or anything athat works with this is not working:

    Code Snippet
    this.serialPort1.add_DataReceived(new System.IO.Ports.SerialDataReceivedEventHandler(this.SetMonitorWindow));

     

    Cheers

     

    Ali E.

    Thursday, April 5, 2007 9:30 PM