Arduino按钮控制VB6形状填充颜色

Arduino按钮控制VB6形状填充颜色,arduino,vb6,Arduino,Vb6,我正在创建一个程序,如果我在Arduino中单击按钮,VB6中的形状填充颜色将变为红色,如果我再次单击按钮,填充颜色将变为绿色。我在读取Arduino发送到VB6的串行数据时遇到问题 这是我的Arduino代码: int pbuttonPin = 7;// push button int LED = 8; // LED int val = 0; // push value from pin 2 int lightON = 0;//light status int pushed = 0;/

我正在创建一个程序,如果我在Arduino中单击按钮,VB6中的形状填充颜色将变为红色,如果我再次单击按钮,填充颜色将变为绿色。我在读取Arduino发送到VB6的串行数据时遇到问题

这是我的Arduino代码:

int pbuttonPin = 7;// push button

int LED = 8; // LED

int val = 0; // push value from pin 2

int lightON = 0;//light status

int pushed = 0;//push status

void setup() 
{
Serial.begin(9600);

pinMode(pbuttonPin, INPUT_PULLUP); 

pinMode(LED, OUTPUT);

digitalWrite(LED, HIGH);

}

void loop()
 {

  val = digitalRead(pbuttonPin);// read the push button value


  if(val == HIGH && lightON == LOW){

    pushed = 1-pushed;


    delay(100);

  }    

  lightON = val;

      if(pushed == HIGH)
     {
        Serial.print("Color Red\n");


        digitalWrite(LED, LOW); 


        delay(100);
       
      }
else
      {
        Serial.print("Color Green\n");


        digitalWrite(LED, HIGH);


        delay(100);
      }
   
}
这是我的VB6代码

Private Sub Form_Load()

    With MSComm1
        .CommPort = 8
        .Settings = "9600,N,8,1"
        .Handshaking = comRTS
        .RTSEnable = True
        .DTREnable = True
        .RThreshold = 1
        .SThreshold = 1
        .InputMode = comInputModeText
        .InputLen = 0
        .PortOpen = True
    End With
    
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

    If MSComm1.Input = "Color Red" Then
        Shape1.FillColor = vbRed
        Shape1.FillStyle = vbSolid
    End If

    If MSComm1.Input = "Color Green" Then
        Shape1.FillColor = vbGreen
        Shape1.FillStyle = vbSolid
    End If

End Sub

非常感谢您在即将到来的

中提供帮助。无法保证来自串行端口的数据不会被分解为多个部分,因此您应该实施一个缓冲区来跟踪接收到的所有部分。另外,由于
MSComm1.Input
,您的第二个
If
语句将永远不包含任何数据。您应该读取数据一次并将其存储在变量中。下面是一些实现此功能的代码:

Dim m_sBuffer As String

Private Sub Form_Load()

    ' Initialize Serial Port
    With MSComm1
        .CommPort = 8
        .Settings = "9600,N,8,1"
        .Handshaking = comRTS
        .RTSEnable = True
        .DTREnable = True
        .RThreshold = 1
        .SThreshold = 1
        .InputMode = comInputModeText
        .InputLen = 0
        .PortOpen = True
    End With
    
    ' Initialize FillStyle
    Shape1.FillStyle = vbSolid
        
    ' Clear buffer
    m_sBuffer = ""
    
    ' Start timer
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()
    Dim sReceivedData As String
    
    ' Read data from serial port
    sReceivedData = MSComm1.Input
    
    ' Append Received Data to buffer
    m_sBuffer = m_sBuffer & sReceivedData
    
    ' Check buffer content
    Select Case m_sBuffer
        Case "Color Red"
            Shape1.FillColor = vbRed
            m_sBuffer = "" ' Clear buffer
        Case "Color Green"
            Shape1.FillColor = vbGreen
            m_sBuffer = "" ' Clear buffer
    End If

End Sub

除了上面提到的,我首先要完全关闭握手(comNone)。我也会使用更简单的消息,“R”和“G”如何?这样,就不会出现缓冲区被破坏的问题,所以解析将更简单、更可靠。即使您有很多按钮要处理,您也可以使用每个状态(开/关)的不同字符来处理多达63个按钮。此外,我认为我应该将切换逻辑移到VB6端,这样您最多可以支持126个单独的按钮(超过整个PC键盘)。我还建议使用
OnComm
事件并处理
MSComm
控件的
CommEvent
属性为
comEvReceive
的情况,而不是使用
计时器。这将使形状颜色立即改变,而不是等待计时器事件。我完全同意埃蒂安使用OnCom事件-我没有注意到逻辑是用计时器轮询的。这并不重要,但这也将使CPU负载减少一点。