Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to use Struct that include Array in SerialPort Component?

$
0
0

I use visal studio 2010. I want to write data that the structure define by user to SerialPort.  The error message say that it cannt access protected level .( The message is in chinese. I am not sure that I translate the message).

My program code is following.

namespace Comm_Test
{

    [StructLayout(LayoutKind.Explicit,Size=5)]
    struct Suion
    {
       
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
        byte[] byte_Data;
       
        [FieldOffset(0)]
        public byte byte_St;

        [FieldOffset(1)]
        public float byte_Ft;
    }

    public partial class frm_SpTest : Form
    {
        
       public frm_SpTest()

      {
            InitializeComponent();

        }

...... Set SerialPort

......Open SerialPort

        private void btn_Transmit_Click(object sender, EventArgs e)
        {
           
            int int_Loop;
           

            //User define Structure
            Suion su_Tmp;

            float ft_PwmCycle;

            //start code
            su_Tmp.byte_St = 2;

            //Convert Textbox content to float
            ft_PwmCycle = float.Parse(tb_Transmit.Text.ToString());
            su_Tmp.byte_Ft = ft_PwmCycle;

            //start transmit the Structure Data
            for (int_Loop = 0; int_Loop < 5; int_Loop++)
            {
                //su_Tmp.byte_Data has something wrong
                sp.Write(su_Tmp.byte_Data, int_Loop, 1);
            }

        }

}

Please help me. Thanx.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>