Click here to Skip to main content
15,913,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Having trouble converting the characters into a string to get the bytes than use an array to send through serial port. (Ive made the changes suggested in solution 1 that I mistakenly missed). The issue is that the commands for the serial port translates perfectly when running a loopback test but does not communicate with the serial device. The command protocol is correct because when I take the output and plug it in hyperterminal, the command works with the device. Anybody with similar experience or issues, Ill appreciate the help.


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;
            }
        }
        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)
        {
            int length = pull_shelf_104.Length;
            byte[] send104 = new byte[length];
            Array.Copy(System.Text.Encoding.ASCII.GetBytes(pull_shelf_104), 0, send104, 0, length);
            return send104;
        }
        
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                byte[] data = WrapString(pull_shelf_104);
                serialPort1.Write(data,0,12);
            }
        }
        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));
        }
    }
}
Posted
Updated 10-Jul-15 7:43am
v3
Comments
Mehdi Gholam 10-Jul-15 13:26pm    
...and what is the problem?
Sergey Alexandrovich Kryukov 10-Jul-15 13:32pm    
Right... But I can see some, please see Solution 1.
—SA
Sergey Alexandrovich Kryukov 10-Jul-15 13:26pm    
And the problem is?.. (In fact, I can see at least one, but you have to describe your issues.)
—SA

1 solution

You did not explain what the problem is.

Basically, you correctly get ASCII from characters.
If you never change pull_shelf_104, it would be better to change its definition to
C#
readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}", STX, ETX);


I can see the syntax error in the call to WrapString, should be
C#
byte data = WrapString(pull_shelf_104);

You may have some other problems; I just explain on those I noticed.

Again, I want to remind you that, in many cases, you would need to execute all communications in separate thread(s). It might be not good that you do some communication in UI, some click handler.

—SA
 
Share this answer
 
v3
Comments
Member 11780461 10-Jul-15 13:47pm    
Ive made the changes that you suggested and edited the question for more clarity. Thanks for all your help as your suggestions gave me the correct translation but it seems that the problem is much deeper because the serial device still wont communicate even though the output from a loopback test delivers the correct commands. My next step will be to try in a separate thread will be new for me.
Sergey Alexandrovich Kryukov 10-Jul-15 13:51pm    
There are many things that depends on what happens on the other end of your RS-232 cable. You cannot control the behavior of that device, so you have to deal with it in certain ways. I don't know what is that, so I cannot help with it. I suggest you accept my answer formally, it is correct.
—SA
Member 11780461 10-Jul-15 14:06pm    
most definitely it is correct, thanks again
Sergey Alexandrovich Kryukov 10-Jul-15 14:20pm    
You are very welcome; good luck, call again.
—SA
Member 11780461 10-Jul-15 14:17pm    
If it makes a difference, the connection is made by a usb to serial cable. Also, when I connect to another computer using putty or hyperterminal none of the messages appear on either screens but when that other computer connects to my serial device machine, it communicates

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