Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Sending a command to a serial device using this program. It is suppose to send <stx>01P00104##<etx> with the ascii control characters at the beginning and end (stx,etx). When performing the loopback test on my serial cable, I get exactly what I needed but when I connect to my serial device I get no response. Weird thing is, When I copied and pasted the output from the loopback test into hyperterminal and putty, the command sent perfectly to the device which means the command is correct and the cable works fine. Any ideas why? Anything to do with how its being translated or write out time??? Im using a usb to serial cable, does the code need to cater to this?


C#
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
    {
        // Add this variable
        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.Open();
            if (serialPort1.IsOpen)
            {
                serialPort1.WriteTimeout = 500;
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }

        private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                var content = new List<byte>();
                content.Add(2);
                content.AddRange(Encoding.ASCII.GetBytes("01P00104##"));
                content.Add(3);
                byte[] buffer = content.ToArray();
                serialPort1.Write(buffer, 0, buffer.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 textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // If the port is closed, don't try to send a character.
            if (!serialPort1.IsOpen) return;

            // If the port is Open, declare a char[] array with 1 element.
            char[] buff = new char[1];

            // Load element 0 with the key character.
            buff[0] = e.KeyChar;

            // Send the 1 character buffer.
            serialPort1.Write(buff, 0, 1);

            // Set the KeyPress event as handled so the character won't
            // display locally. If you want it to display, omit the next line.
            e.Handled = false;
        }

        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));
        }


     }
}
Posted
Updated 9-Jul-15 10:09am
v2
Comments
[no name] 28-Jul-15 21:16pm    
Check you flow control (XON/XOFF or RTS/CTS pin) and DTR pin setting. Some devices ignore input unless the DTR pin is active.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900