C#SerialPort无法从Arduino Micro接收

时间:2018-03-21 16:59:14

标签: c# arduino serial-port

尝试在.NET Framework 4.6.1 SerialPort和Arduino Micro之间进行通信,我可以成功地向Micro发送字节但无法接收。尝试回声测试,Arduino接收工作(和RX亮起)但TX失败 - 没有发送字节或错误。我已尽可能地将代码删除到Arduino每秒只写一个字符的地方,而C#app只读取一个循环,但没有接收到字节。 Micro TX指示灯不亮,Serial.write调用返回0.我打开/关闭了我能找到的所有各种流量控制/握手设置,没有任何区别。确认的奇偶校验/停止位等我可以在我的测试应用程序和RealTerm之间来回切换,一切都可以正常使用RealTerm,关闭它并启动C#SerialPort应用程序和nada。如何使SerialPort失败呢?

C#

class Program
{
    static void Main(string[] args)
    {
        readPacket();
    }

    private static void readPacket()
    {
        var spd_Port = new SerialPort("COM4", 115200);
        spd_Port.ErrorReceived += Spd_Port_ErrorReceived;
        spd_Port.Open();

        while (true)
        {
            while (spd_Port.BytesToRead > 0)
            {
                Console.WriteLine(spd_Port.ReadChar());
            }
            Thread.Sleep(10);
        }
    }

    private static void Spd_Port_ErrorReceived(Object sender, SerialErrorReceivedEventArgs e)
    {
        Console.WriteLine(e);
    }
}

Arduino的

void setup()
{
    // set all pins as inputs (except serial pins)
    init_pins();

    pinMode(ARD_LED, OUTPUT);
    digitalWrite(ARD_LED, HIGH);

    // Configure serial port
    Serial.begin(115200);
}

int i = 0;
void loop()
{
    int j = Serial.write(0x24);
    if (j)
    {
        digitalWrite(ARD_LED, i == 0 ? HIGH : LOW);
        i = (i + 1) % 2;
    }

    delay(1000);
}

1 个答案:

答案 0 :(得分:0)

请尝试启用DTRRTS

_serialPort.DtrEnable = true;
_serialPort.RtsEnable = true;

<强> 修改

根据gregsmi,它可以通过启用DTR或RTS

来工作