如何在c#中顺利移动串口? [英] How to smooth moving from serial Port in c# ?

查看:65
本文介绍了如何在c#中顺利移动串口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里



i试图创造一个飞机的态度。



这里是我的串口,从陀螺读取。



它的工作正常,但我的画面移动缓慢而且我动作不顺畅。



我怎样才能顺利?



我第一次以十六进制发送Serial Like(Pitch Angle):68 04 00 01 05

,计时器= 80;

BaudRate = 9600;



i将会回复赞:68 07 00 81 00 04 62 EE



另外

68 04 00 04 08 Pitch,Roll,Heading和这段代码将会读取三个角度让它更慢!!! !!



如何让旋转顺畅???





Hi,there

i try to create a Airplane attitude.

here is my serial port, that read from gyro.

actully its work but, my picture is slow moving and im not smooth moving.

how can i make it smooth??

First time i send Serial Like (Pitch Angle) in hex: 68 04 00 01 05
with Timer = 80;
BaudRate = 9600;

i will recive Like : 68 07 00 81 00 04 62 EE

Also
68 04 00 04 08 for Pitch,Roll,Heading and this code that will read three Angle Make it More Slow !!!!!

How can i make rotation smooth ???


protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            // Calling the base class OnPaint
            base.OnPaint(paintEvnt);


            // Clipping mask for Attitude Indicator
            paintEvnt.Graphics.Clip = new Region(new Rectangle(0, 0, 300, 300));
            paintEvnt.Graphics.FillRegion(Brushes.Black, paintEvnt.Graphics.Clip);


            // Make sure lines are drawn smoothly
            paintEvnt.Graphics.SmoothingMode = SmoothingMode.HighQuality;

            // Create the graphics object
            Graphics gfx = paintEvnt.Graphics;
           
            // Adjust and draw horizon image
            RotateAndTranslate(paintEvnt, mybitmap1, RollAngle, 0, ptBoule, (double)(4 * PitchAngle), ptRotation, 1);

            RotateAndTranslate2(paintEvnt, mybitmap3, YawAngle, RollAngle, 0, ptHeading, (double)(4 * PitchAngle), ptRotation, 1);

            gfx.DrawImage(mybitmap2, 0, 0); // Draw bezel image
            gfx.DrawImage(mybitmap4, 75, 125); // Draw wings image


            Graphics g = CreateGraphics();
            if (serialPort1.IsOpen == true)
            {
                g.FillEllipse(Brushes.Green, 20, 350, 20, 20);
            }
            else

                g.FillEllipse(Brushes.Red, 20, 310, 20, 20);


        }

 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            // Obtain the number of bytes waiting in the port's buffer
            int bytes = serialPort1.BytesToRead;

            // Create a byte array buffer to hold the incoming data
             byte[] buffer = new byte[bytes];
            

            // Read the data from the port and store it in our buffer      
             serialPort1.Read(buffer, 0, bytes); //bytes
           
            Log(LogMsgType.Incoming, ByteArrayToHexString(buffer)); 
        
            if (bytes > 7 )//13
            {
                
                if (buffer[4] == 0)

                    PitchAngle = Convert.ToDouble(buffer[5]);
                else
                    PitchAngle = -1.0 * Convert.ToDouble(buffer[5]);

               
                Invalidate();
            }
    
        }

推荐答案

请参阅我对这个问题的评论。



另外,我可以给你另一个想法:我非常确定你可以通过使用WPF来大大提高平滑度和性能。并且不要做类似于您的Forms解决方案的手动渲染(这也是可能的)。试试3D图形。 WPF基于DirectX,本来就更快。







我差点忘了:用于连续端口通信,你当然需要使用一个单独的线程。当然,在这个主题中,您无法调用 Invalidate 或从此线程调用WPF UI方法/属性。您将需要使用UI线程调用机制( Invoke / BeginInvoke )来委托对UI线程的调用。请查看我过去的答案:

Control.Invoke( )与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



-SA
Please see my comments to the question.

Also, I can give you the alternative idea: I''m pretty much sure that you could greatly improve smoothness and performance by using WPF instead. And don''t do "manual" rendering analogous to your Forms solution (which is also possible). Try 3D graphics. WPF is based on DirectX, inherently faster.



I almost forgot: for serial port communications, you certainly need to use a separate thread. In this thread, of course, you won''t be able to call your Invalidate or call WPF UI methods/properties from this thread. You will need to use UI thread invocation mechanism (Invoke/BeginInvoke), to delegate the calls to UI thread. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA

这篇关于如何在c#中顺利移动串口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆