您好,  [请登录] [QQ登录]  [支付宝登录[免费注册]

商品分类

分享到: 百度搜藏 搜狐微博 新浪微博 腾讯微博 QQ收藏 人人网 Facebook Twitter

移植VB维护程序到mobile下

发布日期:2011-05-19

         Windows CE现在在嵌入式系统应用已经很多了,以前使用EVC的开发多些,开发难度大周期长。VS2005退出后,CF.net2.0对于VB.NET支持很好,使将原来的基于windows平台的Visual Basic维护程序移植到CE上面提供呢可能,本文将就Visual Basic维护程序移植到CE上面做一个试验性质的开发。
  

         Visual Basic因为其界面开发快速,掌握难度低,在嵌入式系统的程序员中应用很广,比如说本人,以前经常做些单片机的开发,那么一些系统参数或规约的验证,就会随手写个Visual Basic程序验证下,以前是VB6.0,后来VB.net多些,VB的开发扩展性好,做界面简单快速,而且方便在调试中修改程序,在验证单片机的程序时是再方便不过了。
  

         最早的人机界面、维护参数基本是通过串口通讯,在计算机或便携笔记本上面显示,设置。随着嵌入式系统发展,用户要求越来越高,现在好多的设备上面要有显示参数和状态的液晶,对于一些高档设备,更是需要复杂的用户界面,如果能将设备的维护和状态显示程序移植到基于Windows CE的系统平台上面去,就会提供快速的软件开发,用户界面会更加美观,开发速度大幅提供,方便嵌入式系统工程师的开发工作,同时基于windows CE的PDA掌上电脑,方便携带,待机时间更加长,方便现场维护人员和分布式系统的设置和分析工作。
  

        讲原有的VB.net移植到winCE,比较简单,如果没有使用第三方的windows平台的控件,可以很简单的移植过去,窗体可以很简单的复制过去,代码基本是可以复用的,如果是VB6的程序,移植的难度会大些,窗体部分设计需要重新设置,VB.net的窗体界面和VB的区别很大,可以先按照老的程序的界面设置,在新的程序里面设置,代码方面的移植难度不大,基本的语句还是一样的,VB.NET支持界面上的元素可以全新创建,不用想VB那样,从窗口的控件数组里面派生了,而且vb.net可以在运行中定义事件的处理函数,这个对VB是很大的改进,是自动根据规约或界面设置进行处理变得方便快捷。
  

         对于嵌入式系统来说和外界的通讯主要有几个方面,串口(RS485、RS232),网络。网络方面的程序区别不是很大,串口方面,windows下面VB程序员一般是使用MSCOMM来进行通讯,微软在CF.NET2.0里面提供了新的串口开发支持SerialPort,比1.0和EVC更加快速。另外VB.NET支持多线程,使串口开发变得容易。

          下面提供一个我自己实现的串口多线程类。

         Imports System.IO.Ports

         Imports System.Threading

         '232串口操作类

         '2006 6 20 V1.0  by 赵力钊

         '使用说明

         '使用前调用 Init 退出程序或关闭串口使用 Close

         'SendDate 发送数据到串口 返回发送成功与否

         '判断ComStatus 是否为2 数据是否准备好 准备好 调用 ReadData 传空的BYTE数组(可以Redim的),返回数据长度

         Public Class RS232TXClass

         Shared m_SerialPort As New SerialPort

         Shared readThread As Thread = New Thread(AddressOf Read)
    Public ComSetting As String     '"9600,n,8,1"

         Public ComPort As Integer

         Public ComType As Integer        '硬件设置

         Dim strBaudRate As String

          Dim Parity As String

          Dim Handshake As Integer

           Dim DataBits As Integer

           Dim PortName As String

           Dim StopBits As Integer

          Shared _continue As Boolean

          Shared bRxLock As Boolean

          Shared iRxLen As Integer

          Shared iRxTime As Integer

          Shared bRxStatus As Byte

          Const READOK = 2

          Const READOUTTIME = 4

          Const READLOCK = 8

          Const COMOK = 1

          Const COMERROR = 0

         Const COMFREE = 16

         '输入函数 setting 串口设置如 9600,n,8,1  Type 握手协议 0 没有握手协议 Port 串口号

         Public Sub Init(ByVal Setting As String, ByVal Type As Integer, ByVal Port As Integer)

        ComSetting = Setting

        ComPort = Port

        ComType = Type

        ComInit()

    End Sub

    Sub ComInit()

        Dim iStart As Integer

        Dim iTemp As Integer

        Dim bDate() As Byte

        m_SerialPort = New System.IO.Ports.SerialPort()

        iStart = InStr(1, ComSetting, ",")

        strBaudRate = Mid(ComSetting, 1, iStart - 1)

        iTemp = InStr(iStart + 1, ComSetting, ",")

        Parity = Mid(ComSetting, iStart + 1, iTemp - iStart - 1)

        iStart = iTemp + 1

        iTemp = InStr(iStart, ComSetting, ",")

        DataBits = CInt(Mid(ComSetting, iStart, iTemp - iStart))

        iStart = iTemp + 1

        StopBits = CInt(Mid(ComSetting, iStart, Len(ComSetting) - iStart + 1))

        m_SerialPort.BaudRate = strBaudRate

        Select Case Parity

            Case "n"

                m_SerialPort.Parity = IO.Ports.Parity.None

            Case "N"

                m_SerialPort.Parity = IO.Ports.Parity.None

            Case "e"

                m_SerialPort.Parity = IO.Ports.Parity.Even

            Case "E"

                m_SerialPort.Parity = IO.Ports.Parity.Even

            Case "o"

                m_SerialPort.Parity = IO.Ports.Parity.Odd

            Case "O"

                m_SerialPort.Parity = IO.Ports.Parity.Odd

        End Select

        m_SerialPort.DataBits = DataBits

        Select Case StopBits

            Case 0

                m_SerialPort.StopBits = IO.Ports.StopBits.None

            Case 1

                m_SerialPort.StopBits = IO.Ports.StopBits.One

            Case 2

                m_SerialPort.StopBits = IO.Ports.StopBits.Two

        End Select

        Select Case ComType

            Case 0

                m_SerialPort.Handshake = IO.Ports.Handshake.None

            Case 1

                m_SerialPort.Handshake = IO.Ports.Handshake.RequestToSend

            Case 2

                m_SerialPort.Handshake = IO.Ports.Handshake.RequestToSendXOnXOff

            Case 3

                m_SerialPort.Handshake = IO.Ports.Handshake.XOnXOff

        End Select

        m_SerialPort.PortName = "COM" + CStr(ComPort)

        m_SerialPort.ReadTimeout = 500

        m_SerialPort.WriteTimeout = 500
        If m_SerialPort.IsOpen = True Then

            m_SerialPort.Close()

        End If

        m_SerialPort.Open()

        If m_SerialPort.IsOpen = True Then

            bRxStatus = COMOK

            ReDim bDate(2)

            ReadData(bDate)

            bRxLock = False

            readThread.Start()

        Else

            bRxStatus = COMERROR

        End If

        ' readThread.Join()
    End Sub

    Public Function ComStatus() As Byte

        ComStatus = bRxStatus

    End Function

    Function ReadData(ByRef bDate() As Byte) As Integer
        Dim bLen As Integer

        bLen = m_SerialPort.BytesToRead

        If bLen > 0 Then

            ReDim bDate(bLen)

            m_SerialPort.Read(bDate, 0, bLen)

            ReadData = bLen

        Else

            ReadData = 0

        End If

        bRxStatus = COMFREE

    End Function

    Public Function SendDate(ByVal bDateBuff() As Byte, ByVal iLen As Integer) As Boolean

        If bRxLock = False Then

            m_SerialPort.Write(bDateBuff, 0, iLen)

            bRxLock = True

            bRxStatus = READLOCK

            SendDate = True

        Else

            SendDate = False

        End If
    End Function

    Public Shared Sub Read()

        While (1)

            Thread.Sleep(50)

            Try
                If m_SerialPort.BytesToRead <> iRxLen Then

                    iRxLen = m_SerialPort.BytesToRead

                    iRxTime = 0

                    bRxStatus = READLOCK

                Else
                    If iRxLen > 0 Then

                        '收到数据

                        bRxStatus = READOK

                        bRxLock = False

                    Else
                        iRxTime = iRxTime + 1

                        If iRxTime > 10 Then

                            bRxStatus = READOUTTIME

                        End If

                        bRxLock = False

                    End If
                End If


            Catch ex As TimeoutException

                ' Do nothing

            End Try

        End While
    End Sub
    Public Sub Close()

        readThread.Abort()

        m_SerialPort.Close()
    End Sub

End Class
定义窗口变量

      Dim ComPort As New RS232TXClass

     启动后调用

     ComPort.Init("9600,n,8,1", 0, 1)

     在主窗口中,通过一个定时器事件,查看串口情况

     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim bDate() As Byte

        Dim iLen As Integer

        ReDim bDate(2)

        If ComPort.ComStatus = 2 Then

            iLen = ComPort.ReadData(bDate)

            If iLen > 2 Then
                ShowRevDate(bDate, iLen)

            End If

        End If

    End Sub

        下面实现一个windows的路灯维护程序移植到windows CE上面去的一些简单示例。
 

       这是原来VB上面的一个界面。

        这个是我移植到VB.net for Mobile的版本,因为在Mobile上面多窗口切换很麻烦,就作成分页的显示了。

     需要注意的是,因为Mobile上面的输入法启动后会遮盖一部分窗口,为了输入方面最后把需要输入的地方放到上面去,以免影响输入。这里我把两个显示性Label放到了后面。

      VB.NET的绘图和VB不是很一样,更加接近C++的绘图方式,但是只要不一些概念弄清楚,你会发现这种绘图方式使用更加方便,更加方便你对于GDI+的理解。

        VB.NET中可以使用 Dim bm As New Bitmap(238, 214) 直接建立一个位图,然后把绘制好的位图覆盖回去。

        绘制文字的时候,需要字体和画刷,象下面这样:

    ShowTextBrush = New SolidBrush(Color.Blue)

    mFont = New Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular)

    g.DrawString("HELLO", mFont, ShowTextBrush, 12, 82)

        绘制线的时候,需要画笔。

     Pen1 = New Pen(Color.Red)

     g.DrawLine(Pen1, OldX, OldY, NewX, NewY)

        填充图形的时候,需要画刷

   tempbrush = New SolidBrush(Color.FromArgb(192, 192, 255))

   g.FillRectangle(tempbrush, 0, 0, 238, 214)

        绘制扇形和弧形,VB.NET没有提供,但是我从网上找到了实现方法,用联系线和连续填充实现弧形和棒图的绘制。函数如下

   '=====================================================

         '绘制弧形

    '

    '    graphicsObject - Graphics 对象

    '    pen            - 画笔

    '    x,y            - 弧的圆心

    '    width          - 宽度 (X直径)

    '    height         - 高度 (Y直径)

    '    startAngle     - 起始角度

    '    sweepAngle     - 结束角度

    '

         Private Sub drawPie(ByVal graphicsObject As Graphics, ByVal pen As Pen, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal startAngle As Single, ByVal sweepAngle As Single)
        Dim xAngle(12) As Single

        Dim yAngle(12) As Single

        Dim angleIncrement As Single

        angleIncrement = (sweepAngle - startAngle) / 10

        Dim angle As Single

        angle = startAngle

        Dim i As Integer

        For i = 0 To 10
            xAngle(i) = x + (Math.Cos(angle * (Math.PI / 180)) * (width / 2))

            yAngle(i) = y + (Math.Sin(angle * (Math.PI / 180)) * (height / 2))

            angle += angleIncrement

        Next i

        xAngle(11) = x + (Math.Cos(sweepAngle * (Math.PI / 180)) * (width / 2))

        yAngle(11) = y + (Math.Sin(sweepAngle * (Math.PI / 180)) * (height / 2))

        Dim anglePoints(12) As Point

        anglePoints(0) = New Point(x, y)

        For i = 0 To 11

            anglePoints(i + 1) = New Point(CInt(xAngle(i)), CInt(yAngle(i)))

        Next


        graphicsObject.DrawPolygon(pen, anglePoints)

    End Sub

    '=====================================================

         '填充弧形

    '    graphicsObject - Graphics 对象

    '    solidBrush     - 画刷

    '    x,y            - 弧的圆心

    '    width          - 宽度 (X直径)

    '    height         - 高度 (Y直径)

    '    startAngle     - 起始角度

    '    sweepAngle     - 结束角度

    '

    Sub fillPie(ByVal graphicsObject As Graphics, ByVal solidBrush As SolidBrush, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal startAngle As Single, ByVal sweepAngle As Single)

        Dim xAngle(12) As Single

        Dim yAngle(12) As Single

        Dim angleIncrement As Single

        angleIncrement = (sweepAngle - startAngle) / 10

        Dim angle As Single

        angle = startAngle

        Dim i As Integer

        For i = 0 To 10
            xAngle(i) = x + (Math.Cos(angle * (Math.PI / 180)) * (width / 2))

            yAngle(i) = y + (Math.Sin(angle * (Math.PI / 180)) * (height / 2))

            angle += angleIncrement

        Next i

        xAngle(11) = x + (Math.Cos(sweepAngle * (Math.PI / 180)) * (width / 2))

        yAngle(11) = y + (Math.Sin(sweepAngle * (Math.PI / 180)) * (height / 2))

        Dim anglePoints(12) As Point

        anglePoints(0) = New Point(x, y)

        For i = 0 To 11

            anglePoints(i + 1) = New Point(CInt(xAngle(i)), CInt(yAngle(i)))

        Next
        graphicsObject.FillPolygon(solidBrush, anglePoints)

    End Sub

      本文为Windows Embedded征文比赛获奖文章。