Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am trying to talk to an Aglient device via C# through a RS232 connection on my PC with XP OS and I am having trouble. I can send a command, such has a query (*IDN?), and I never get a response in my GUI. However, if I open hyperterminal the device IDN shows up in hyperterminal like it should have showed up in my GUI. From my C# GUI, I can sent a cammand to the device to change voltage level and the device changes voltage level as I can see it on the device display. However, when I send a query to the device, I expect to see data in the input buffer that was sent by the device, but the input buffer is always empty. I have connect more than one device and I have yet to read any data from the input buffer. I can write to the output buffer and read the correct reponse from the input buffer using hyperterminal and Labview. I can send the query via my C# GUI and read the correct response from the input buffer using hyperterminal and Labview. Is there a bug when reading the input buffer using C#?

What I have tried:

this is my code:
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;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        // Set the COM1 serial port to speed = 4800 baud, parity = odd,
        // data bits = 8, stop bits = 1.
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

        public Form1()
        {
            InitializeComponent();
            this.sendRead.Text = "Enter Commands or queries here";

            // set read time out to 2 seconds
            port.ReadTimeout = 2000;

            // open serial port
            port.Open();

            //Enable Event Handler
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        }

        private void sendRead_TextChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port
                port.WriteLine(sendRead.Text);

                //clear the text box
                sendRead.Text = "";
            }

            catch (System.Exception ex)
            {
                sendRead.Text = ex.Message;
            }
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int intBuffer;
            intBuffer = port.BytesToRead;
            byte[] byteBuffer = new byte[intBuffer];
            port.Read(byteBuffer, 0, intBuffer);
            this.Invoke(new EventHandler(DoUpDate));
        }

        private void DoUpDate(object s, EventArgs e)
        {
            sendRead.Text = port.ReadLine();
        }

        private void Form1_FromClosing(object sender, EventArgs e)
        {
            port.Close();
        }
    }
}
Posted
Updated 20-Jan-20 9:17am
v2

Try setting the DtrEnable[^] and RtsEnable[^] properties to true: it may be that the unit expects hardware handshaking that HyperTerminal is setting high.

But also try adding the data from your device to the string in your DoUpDate method:
private void DoUpDate(object s, EventArgs e)
        {
            sendRead.Text += port.ReadLine();
        }
Serial ports are slow devices - 9600 baud is less that 1000 characters per second - so you will very likely get a separate DataReceived event for each character. If the final one is a space or unprintable, you might not see anything useful in your text box.
 
Share this answer
 
Comments
Member 13430184 26-Sep-17 7:57am    
thank you very much, I get now values but those do not fit with the values displayed in screen for example the value in the display is 5 V and I get in my program 400E:00
OriginalGriff 26-Sep-17 8:28am    
For that, you need to read the manual: what you are receiving is probably the "Raw" AtoD converter value, which needs to be "scaled" to a screen value.
Member 13430184 26-Sep-17 9:25am    
I think the Serial port is slow, is there any Solution for this Problem?
Quote:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int intBuffer;
intBuffer = port.BytesToRead;
byte[] byteBuffer = new byte[intBuffer];
port.Read(byteBuffer, 0, intBuffer); //<--- YOU ARE READING (AND DISCARDING) DATA HERE
this.Invoke(new EventHandler(DoUpDate));
}

private void DoUpDate(object s, EventArgs e)
{
sendRead.Text = port.ReadLine(); //<--- YOU ARE TRYING TO READ AGAIN
}


The problem is you are reading and then discarding the read data inside the DataReceived handler. You should pass the read data to the DoUpdate method.
 
Share this answer
 
Comments
Member 13430184 26-Sep-17 6:55am    
it still does not work, I get an error in the device(Query INTERRUPTED
A command was received which sends data to the output buffer, but the
output buffer contained data from a previous command (the previous data is not
overwritten). The output buffer is cleared when power has been off, or after a *RST
(reset) command has been executed. ) in the device
C#
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

Comment and code disagree on speed and parity, which one is correct ?
Quote:
However, if I open hyperterminal the device IDN shows up in hyperterminal like it should have showed up in my GUI.

Using Hyper terminal you can check if your GUI app send command correctly to device.
 
Share this answer
 
Comments
Member 13430184 27-Sep-17 3:40am    
the comment is not correct, my problem is, I can not get the whole answer, I get only the end of the answer, I think the rs232 cable is slow to get the whole answer, do you have maybe a solution for this Problem ??

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