私はxsheeのxbeeモジュールからいくつかの文字列を読みとろうとしました。 しかし、私のコードは、シリアルポートがイベントハンドラに達すると開いていないことを伝え続けます。どんな助けでも大いに評価されるでしょう。おかげstring display = myserial.ReadLine();
using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApplication2
{
class Program
{
public static SerialPort myserial = new SerialPort();
public string display;
static void Main(string[] args)
{
string[] ports = SerialPort.GetPortNames();
foreach (string p in ports)
{
Console.WriteLine(p);
}
SerialPort myserial = new SerialPort();
myserial.BaudRate = 9600;
myserial.Parity = Parity.None;
myserial.StopBits = StopBits.One;
myserial.DataBits = 8;
myserial.Handshake = Handshake.None;
myserial.RtsEnable = true;
myserial.DtrEnable = true;
myserial.ReadTimeout = 100000;
myserial.PortName = "COM3";
myserial.ReadTimeout = 10000;
myserial.DataReceived += new SerialDataReceivedEventHandler(DataRecievedHandler);
myserial.Open();
if (myserial != null)
{
if (myserial.IsOpen)
{
Console.WriteLine("connected");
}
}
Console.ReadLine();
}
static void DataRecievedHandler(object sender, SerialDataReceivedEventArgs e)
{
string display = myserial.ReadLine();
}
}
}
は全く意味がありません。それで、あなたが例外を受ける理由です。そのステートメントを削除します。このプログラムは*決して* Close()を呼び出す必要があります。どちらがうまくいけば、ファイナライザはそれを処理します。 –
ありがとう@HansPassantは今それを試みます。 – don
@HansPassantはそれをやっても同じ例外が出ます – don