Чтение последовательного порта в C#
Привет,
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#?
Что я уже пробовал:
это мой код.:
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(); } } }