通过串口发送和检索数据的问题 [英] problem with sending and retrieve data by serial port

查看:143
本文介绍了通过串口发送和检索数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello mates

我现在正在制作visual basic和arduino的源代码..

并通过串口相互通信

i已成功制作2个arduino代码..一个用于向vb发送数据..一个用于从vb

检索数据但问题是当我尝试将它组合时

并运行程序..我的arduino的系统挂起并且什么都不做

所以我该怎么办?需要一些建议



hello mates
im now making a source code of visual basic and also arduino..
and communicate with each other by serial port
i have successfully make 2 arduino code.. one is for sending data to vb.. and one is for retrieve data from vb
but the problem is when i try to combine it
and run the program.. my arduino's system hang and do nothing
so what should i do ? need some suggestion

void setup()
{
  pinMode(A0 && A1 && 2 && 3, INPUT);
 pinMode(12 && 13, OUTPUT);
 Serial.begin(9600); 
 while(Serial.available() <= 0) {
  establishContact();
 }
}

void loop()
{
  if (Serial.available() > 0)
  {
    int rdval = Serial.read();
    if(rdval == '11')
    {
      digitalWrite(13, 1);
    }
    delay(100);
  }
}
void establishContact() {
  int val1 = analogRead(A0);
  else if( val1 !=0 )
  {
   Serial.println("a"); 
  }
  delay(100);
}



for vb code




for vb code

Private Sub ReceivedText(ByVal [text] As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(d, New Object() {[text]})
        Else
            If [text] = "a" Then
                serialport1.write("11")
            End If
        End If
End Sub

推荐答案

以下是通过串行线路进行通信的简要清单:

- 你需要确保每一方都以相同的速度,启动/停止位数和流量控制进行通信。如果使用CTS / RTS等,则细节更棘手。这些细节必须正确设置。

- 如果任何一方的速度发生变化(即在初始化期间),可能会出现一些垃圾字符。您的设备间协议应该容忍这一点并丢弃不正常的字符/消息...特别是程序启动时的初始消息。

- 当每一方开始通信时,您的代码应该容纳另一方尚未准备就绪或消息可能被破坏或丢失的想法(见前一点)。不要只是发送消息并永远坐在等待循环中寻找响应。



还有很多其他要点可以做。这只是一个简短的清单。
Here is a brief checklist for communicating via serial lines:
- You need to make sure that each side is communicating with the same speed, number of start/stop bits, and flow control. If CTS/RTS etc. is used, the details are trickier. These details absolutely have to be setup correctly.
- If the speeds on either side change (ie. during initialization), some garbage characters are likely to show up. Your inter-device protocol should be tolerant of this and discard characters/messages that are out of context... Especially the initial messages when your program starts.
- When each side starts communicating, your code should accommodate the idea that the other side is not ready yet or that the message might get mangled or lost (see previous point). Don't just send a message and sit forever in a wait loop looking for a response.

There are a lot of other points that could be made. This is just a brief list.


这篇关于通过串口发送和检索数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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