Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have send this query send


but problem is this
i have use C# code this query
n send then no output result receive for terminal.

plz solve

my code is


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;  //add this,represents a serial port resource


namespace Serial_Comunication
{
    public partial class Form1 : Form
    {
        SerialPort myserial = new SerialPort(); //create of serial port

        public Form1()
        { 
            InitializeComponent();
            getportnames();                   //load all port names in this computer
          
          
        }

      
        public void Init()
        {
           if (myserial.IsOpen )  //if port is  open 
            { 
               myserial.Close();  //close port
            }
          myserial.PortName = comboBoxPort.Text;                                           //selected name of port
          myserial.Parity     = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text);   //selected parity 
          myserial.StopBits   = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopB.Text);//selected stopbits
          myserial.DataBits   =   int.Parse(comboBoxDataB.Text);                           //selected data bits
          myserial.BaudRate   = int.Parse(comboBoxBaudR.Text);                             //selected baudrate
          myserial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);//received even handler
        }
        public void getportnames()         
        {
            string[] portnames = SerialPort.GetPortNames(); //load all names of  com ports to string 
            comboBoxPort.Items.Clear();                     //delete previous names in combobox items 
            
            foreach (string s in portnames)                 //add this names to comboboxPort items
            { 
                comboBoxPort.Items.Add(s);
            }

           if (comboBoxPort.Items.Count > 0)   //if there are some com ports ,select first 
            {
               comboBoxPort.SelectedIndex = 0;
            }
           else
            { 
               comboBoxPort.Text = "No COM Port "; //if there are no com ports ,write No Com Port
            }
        }
        public  void  transmit()    
        {
            Init();                 //init parameters of serial comunication before every touch button "send"
            try
            {
                myserial.Open();        //open serial port
                myserial.Write(richTextBoxSend.Text); //write string  to serial port from rich text box 


            }
            catch
            {
                MessageBox.Show("com port is not available"); //if there are not is any COM port in PC show message
            }
        }

        public void DataReceivedHandler( object sender,SerialDataReceivedEventArgs e)
        {
          string indata=  myserial.ReadExisting();  //read data to string 
          Invoke(new Action(() => richTextBoxReceive.Text += indata)); //invoke method use for write receive data to richtextbox 
        }
        private void buttonSend_Click(object sender, EventArgs e) // send button  event
        { 
            transmit();     //transmit data
        }
        private void buttonReload_Click(object sender, EventArgs e)//reload button event ,most useful if you use virtual COM port e.g FTDI,Prolific 
        { 
            getportnames(); //get all port names connected to this computer
        }
    }
}


What I have tried:

sending(STX and ETX) in C#
Posted
Updated 26-Jun-16 2:01am
v2

If you are trying to send data including ASCII control codes such as STX and ETX, then you really need to be very careful about what else you are sending.
When you send Text from a C# app:
C#
myserial.Write(richTextBoxSend.Text);
What you send is in Unicode, which is a "bigger" character set, and where the characters can be trasnmitted as two bytes. If your other end equipment is expecting STX / ETX, then it's likely that it also expects ASCII data, and Unicode may well confuse the issue.
So convert your data first, and send it all as byte values instead of character:
C#
private byte[] STX = new byte[] { 0x02 };
private byte[] EXT = new byte[] { 0x03 };
...
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(richTextBoxSend.Text);
    myserial.Write(STX, 0 , 1);
    myserial.Write(bytes, 0, bytes.Length);
    myserial.Write(ETX, 0, 1);
 
Share this answer
 
v2
Just prefix the string to be send with "\x02" and append "\x03" or send these single characters before and after sending the string.

BTW:
Why you are initialising and opening the port inside your transmit function?
I would move the open call to the end of the Init() function and call that only once (e.g. when clicking on a provided Open button). Finally close the port in the destructor.
 
Share this answer
 
v2
Comments
OriginalGriff 19-Jun-16 5:57am    
Psst!
STX is 0x02, ETX is 0x03:
http://www.asciitable.com/index/asciifull.gif
Jochen Arndt 19-Jun-16 6:02am    
Uups.
Had enough coffee meanwhile. Must be something with my eyes (I had opened a control character table to check it - and failed).
OriginalGriff 19-Jun-16 6:23am    
I do the same thing! :laugh:
C#
var serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
serialPort.Open();
serialPort.Write(new byte[] {0x02}, 0, 1); // Send STX
serialPort.Write(new byte[] {0x03}, 0, 1); // Send ETX
 
Share this answer
 

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