简体   繁体   English

Arduino无法从串行读取?

[英]Arduino Unable To Read From Serial?

I'm working on some bluetooth-based arduino to c# (on computer) communication. 我正在研究一些基于蓝牙的arduino与C#(在计算机上)的通信。 A lot of the code I've been using was taken from examples, but here's the current situation. 我一直在使用的许多代码均来自示例,但这是当前情况。

When I send messages from my arduino to my computer, by writing to serial, they show up--I generally need to include a newline character, but that's not a big deal. 当我从arduino发送消息到计算机时,通过写串行消息,它们就会显示出来-我通常需要包含换行符,但这没什么大不了的。

However, when I send messages to my arduino, it never even acknowledges that it got them. 但是,当我向arduino发送消息时,它甚至从未承认它收到了它们。 Any ideas on why this might be? 关于这可能是什么的任何想法? Here's the relevant code. 这是相关的代码。

(Note, I'm using an Arduino uno, and a basic serial bluetooth modem). (请注意,我正在使用Arduino uno和基本的串行蓝牙调制解调器)。

char inChar; // Where to store the character read

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

void loop()
{
  if (Serial.available() > 0)
  {
      inChar = Serial.read();
      Serial.write("processing message...\n");
  }
}

Nothing is ever written to serial in this program, indicating that it never sees that it has stuff to read. 在此程序中,什么也没有写入串行,这表明它从不认为自己有东西可以读取。

EDIT: Forgot to post the C# code. 编辑:忘记发布C#代码。 Whoops. 哎呦。

    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;

    _serialPort.Open();
    _continue = true;
    readThread.Start();


    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format(message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

The arduino code as it is will only read one byte from serial and continue the flow. 照原样的arduino代码将仅从串行读取一个字节并继续执行流程。 You might want to replace your if statement with a while statement like so: 您可能想要用while语句替换if语句,如下所示:

while (Serial.available() > 0)
  {
      inChar[i] = Serial.read();
      i++;
  }
Serial.write("processing message...\n");

This will put the received message in a char array. 这会将收到的消息放入char数组中。 (You'd also need to declare inChar as an array eg - char inChar[8]; ) (您还需要将inChar声明为数组,例如-char inChar [8];

Other than that it should work unless there is something wrong with your C# code which I can't test right now but it looks fine. 除此之外,它应该可以工作,除非您的C#代码有问题,我现在无法测试,但看起来还不错。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM