English 中文(简体)
Serial Port.BaseStream.BeginWrite & BeginRead questions
原标题:SerialPort.BaseStream.BeginWrite & BeginRead questions

我有一些问题试图把阿斯辛奇通信放在一个序列号上,希望得到一些帮助。 I m 仍在学习Async st,因此是gent:

 

我基本上需要确保,在通港前写明缓冲地带,而且所有数据都经过阅读,然后才能得到更多的数据,以避免包装上的碰撞。 不幸的是,我所写的微观控制器没有任何手法或手法,因此,我不得不用手法加以 mi弄。

 

 

public partial class Form2 : Form
{
    System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort();
    System.Timers.Timer tmrPolling = new System.Timers.Timer(200);
    static byte [] inputPacket = new byte[2] { (byte)255, (byte)0 }; // Byte to send to SerialPort when input polling timer is activated.

    AsyncCallback callback;
    IAsyncResult result;

    Form2()
    {
        InitializeComponent();
        port.PortName = "COM9";
        port.BaudRate = 38400;
        port.DataBits = 8;
        port.StopBits = System.IO.Ports.StopBits.One;
        port.Parity = System.IO.Ports.Parity.None;
        port.ReadTimeout = 1;
        port.ReceivedBytesThreshold = 5;
        port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
        callback = new AsyncCallback(ReadComplete);  
        port.ReadBufferSize = (byte)60;
        port.WriteBufferSize = (byte)60;
        port.Open();  
        tmrPolling.Elapsed += new System.Timers.ElapsedEventHandler(tmrPolling_Elapsed);
    }

    void WritePacket(byte[] packet)
    {
        result = port.BaseStream.BeginWrite(packet, 0, packet.Count(), callback, port); // writes the input packet to the SerialPort - needed to read the IO values
    }

    void ReadComplete(IAsyncResult result)
    {

    }


    void tmrPolling_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {  
        WritePacket(inputPacket); // writes the input packet to the SerialPort - needed to read the IO values
    }

    void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        port.BaseStream.EndWrite(result);
        // Reads the data coming in from the serial port and calls the thread safe delegate to update the values on the form.
        byte [] received = new byte[port.BytesToRead];
        result = port.BaseStream.BeginRead(received, 0, received.Count(), callback, port);
        port.BaseStream.EndRead(result);  
    }

    private void UpdateValues()
    {
        byte[] b = new byte[] { (byte)255, (byte)6, (byte)hsbPan.Value, (byte)vsbTilt.Value, (byte)0,  
        (byte)0, (byte)0, (byte)0, (byte)13, (byte)10 };
        WritePacket(b);
    }

    private void chkEnablePolling_CheckedChanged(object sender, EventArgs e)
    {  
        tmrPolling.Enabled = chkEnablePolling.Checked;
        test.Enabled = chkEnablePolling.Checked;
        vsbInputInterval.Enabled = chkEnablePolling.Checked;
        vsbInputInterval.Value = Convert.ToInt32(tmrPolling.Interval);
        txtInputInterval.Enabled = chkEnablePolling.Checked;
        txtInputInterval.Text = ((float)tmrPolling.Interval / ( float)1000).ToString() + " sec";
    }

    private void vsbInputInterval_Scroll(object sender, ScrollEventArgs e)
    {
        // adjusts the tick frequency of the Timer for input polling. 
        txtInputInterval.Text = ((float)tmrPolling.Interval / (float)1000).ToString() + " sec";
        tmrPolling.Interval = vsbInputInterval.Value;
    } 

    private void vsbTilt_Scroll(object sender, ScrollEventArgs e)
    {  
        UpdateValues();
    }

    private void hsbPan_Scroll(object sender, ScrollEventArgs e)
    {
        UpdateValues();
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签