2

I am using a serial port application to send a command with Unicode control characters and am having no success. When I send the same command in hyperterminal or Putty the communication works so I am curious if these two programs write out an array of bytes or Unicode characters or even a string. If I mimic what they do, I might have success... Also I noticed that the serial port property I pulled out of the toolbox in Visual Studio was listed as using a different COM port that I write in my code, is this why I'm getting no response or does my code override that? When conducting loopback tests, I get the output of the correct command with the control characters as well so I cant figure out what I'm doing wrong. This is .NET 2.0 Framework FYI

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SimpleSerial
{
    public partial class Form1 : Form
    {
        string RxString;
        public Form1()
        {
            InitializeComponent();
        }
        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;
            serialPort1.ReadTimeout = 500; 
            serialPort1.WriteTimeout = 500;
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
        const char STX = '\u0002';
        const char ETX = '\u0003';
        readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}" , STX, ETX);
        private byte[] WrapString(string pull_shelf_104)
        {
            return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104);
        }
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                byte[] data = WrapString(pull_shelf_104);
                serialPort1.Write(data, 0, data.Length);
            }
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }
        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }
    }
}
member5536
  • 19
  • 3
  • 9
  • You open com-port to send/receive bytes. What are those bytes (are they in some encoding, etc.) it depends. Putty works in terminal mode (?), so it expects to receive ASCII characters from modem. Regarding com-ports, oh yeah, big surprise, they can be anything on different PC, means you shouldn't hardcode `"com3"`. You can change com-port name in Device Manager, but this doesn't eliminate com-port identification issue. – Sinatr Jul 13 '15 at 14:05
  • `byte[] ackData = Encoding.ASCII.GetBytes(string here);` try this to convert starings to raw bytes. – hypheni Jul 13 '15 at 14:07
  • You are focusing on the wrong problem. There are not a lot of serial port devices that use Handshake.None. When you use that, it is up to you to set the RtsEnable and DtrEnable properties to *true*. If you don't then the device won't send anything. – Hans Passant Jul 13 '15 at 14:24
  • Where is SerialPort1 being initialised? Rather than giving Port name, you should get the port from list of available ports, then you know for sure that the port exist. Serial transfer simple bytes, you can encode to whatever the encoding you like as long as you encode and decode using same format. – Jegan Jul 13 '15 at 14:27
  • http://stackoverflow.com/questions/16215741/c-sharp-read-only-serial-port-when-data-comes/16216052#16216052 see this example. – Jegan Jul 13 '15 at 14:32
  • Ok I will adjust the COM port selection after getting the ports and Ill continue using the encoding I have. But for enabling RTS and DTS, isn't it necessary to have hardware flow control on? My protocol instructions from the serial device provider stated not to use flow control – member5536 Jul 13 '15 at 15:28

1 Answers1

0

The comments above helped me solve the issue, thank you for your help! I needed to enable both RTS and DTS in my code, listing the COM ports was not the issue although I'm sure it is in some cases. Here's my code for anyone with similar issues.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SimpleSerial
{
    public partial class Form1 : Form
    {
        string RxString;
        public Form1()
        {
            InitializeComponent();
        }
        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;
            serialPort1.RtsEnable = true;
            serialPort1.DtrEnable = true;
            serialPort1.ReadTimeout = 2000; 
            serialPort1.WriteTimeout = 2000;
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
        const char STX = '\u0002';
        const char ETX = '\u0003';
        readonly string pull_shelf_104 = string.Format("{0}01P00204##{1}" , STX, ETX);
        private byte[] WrapString(string pull_shelf_104)
        {
            return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104);
        }
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                byte[] data = WrapString(pull_shelf_104);
                serialPort1.Write(data, 0, data.Length);
            }
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }
        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }
    }
}
member5536
  • 19
  • 3
  • 9