我想从windows窗体向我的arduino发送数据。我遇到的问题是一次只能有一个程序访问该端口。
我在arduino中创建了一个程序,在我的电视上发送模拟视频分量信号。窗体应用程序将计算太空入侵者相似游戏的位置并将它们发送到序列。
以下是发送array
像素位置的c#表单代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Drawing;
namespace ArduinoGame
{
class SerialWriter
{
SerialPort sp;
public SerialWriter()
{
sp = new SerialPort();
sp.PortName = "COM3";
sp.BaudRate = 9600;
}
public void CloseWriter()
{
if (sp.IsOpen) sp.Close();
}
public void WritePixelPos(Point[] pixels)
{
try
{
sp.Open();
foreach (Point pixel in pixels)
{
sp.WriteLine(Convert.ToString(pixel.X + "," + pixel.Y);
}
}
catch (Exception ex)
{
}
}
}
}
现在在arduino IDE中,我想从序列中捕获每个句子,并在函数中使用该数据,但我无法访问它。
基本上,当arduino需要访问时,表单必须关闭串口,反之亦然。