tags:

views:

47

answers:

1

Hello,

I'm trying to make a simple application to test the RS422 communications with another computer. Using the RS232 interfaces this program is working smoothly, but with the RS422 is not working, as there is one computer that can't send. To complex the scenario a little bit more, I can communicate through RS422 using a HyperTerminal.

Here is the code:

public partial class MainForm : Form
{
    private SerialPort m_port;

    public MainForm()
    {
        InitializeComponent();
        m_list.Items.AddRange(SerialPort.GetPortNames());

        m_port = new SerialPort();
        m_port.BaudRate = 9600;
        m_port.DataBits = 8;
        m_port.Parity = Parity.None;
        m_port.StopBits = StopBits.One;
        m_port.Handshake = Handshake.None;
        m_port.Encoding = new ASCIIEncoding();

        m_port.ReceivedBytesThreshold = 1;
        m_port.DataReceived += DataReceivedEvent;
    }

    ~MainForm()
    {
        if (m_port != null)
            m_port.Close();
    }

    private void openClick(object sender, EventArgs e)
    {
        m_port.Close();
        m_port.PortName = (string)m_list.SelectedItem;

        try
        {
            m_port.Open();
            m_buttonSend.Enabled = true;
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void ButtonSendClick(object sender, EventArgs e)
    {
        m_port.WriteLine(m_testBox.Text);
    }

    private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
    {
        Invoke(new EventHandler(DoUpdate));
    }

    private void DoUpdate(object s, EventArgs e)
    {
        m_receivedText.Text += m_port.ReadLine();
    }
}

Any help or experience with this technology is appreciated. Thanks!

EDIT

There is a lot of differences between the trace with Portmon of Hyperterminal and the .NET component. There is one of the lines that got my attention as it reefers to the wait mask of the port interruption IOCTL_SERIAL_SET_WAIT_MASK.

With HyperTerminal:

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RLSD ERR

With the .NET SerialPort component

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING     

Anybody knows how to change the mask from the component? This is getting deep...

A: 

Finally there was a problem in the initialitation and another one with the blocking ReadLine call. RTS and DTS must be enabled.

Here is the code

using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace ComPlay
{
    public partial class MainForm : Form
    {
            private SerialPort m_port;
            private byte [] m_buffer = new byte[10];

            public MainForm()
            {
                    InitializeComponent();
                    m_list.Items.AddRange(SerialPort.GetPortNames());
                    m_list.SelectedIndex = 0;

                    m_port = new SerialPort(SerialPort.GetPortNames()[0],9600,Parity.None,8,StopBits.One);   
                    m_port.Handshake = Handshake.None;
                    m_port.RtsEnable = true;
                    m_port.DtrEnable = true;

                    m_port.DataReceived += DataReceivedEvent;
                    m_port.PinChanged += PinChangedEvent;
            }

            ~MainForm()
            {
                    if (m_port != null)
                            m_port.Close();
            }

            private void openClick(object sender, EventArgs e)
            {
                    if (m_port.IsOpen)
                            m_port.Close();

                    m_port.PortName = (string)m_list.SelectedItem;

                    try
                    {
                            m_port.Open();
                            m_buttonSend.Enabled = true;
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                            MessageBox.Show(ex.Message);
                    }
            }

            private void ButtonSendClick(object sender, EventArgs e)
            {
                    byte [] r_bytes = Encoding.ASCII.GetBytes(m_testBox.Text);
                    m_port.Write(r_bytes,0,r_bytes.Length);
            }

            private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
            {
                    Invoke(new EventHandler(DoUpdate));
            }

            private void DoUpdate(object s, EventArgs e)
            {
                    m_port.Read(m_buffer, 0, m_buffer.Length);
                    m_receivedText.Text += Encoding.ASCII.GetString(m_buffer); 
            }

            private void PinChangedEvent(object sender, SerialPinChangedEventArgs args)
            {

            }
    }
}

The important thing to begin transmitting was to change this

IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:80000000 Replace:80000040 XonLimit:1024 XoffLimit:1024

to this

IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:80000001 Replace:80000040 XonLimit:1024 XoffLimit:1024

activating RTS and DTR.

yeyeyerman