[問題] StreamSocket類別放在成員變數可行嗎?
會這麼問..
是因為手邊一支可運行的程式,
收到uart的訊號後會立即調用StreamSocket區域變數,
開socket將資料丟出,完成後關閉。
但是僅僅只是將StreamSocket改為成員變數,
將new及其他connectAsync..等相關函式拿掉,沒有進行任何操作,
在本來程式運行一開始時的開串列埠時,就會崩潰...
有任何能協助我的地方嗎? 謝謝喔:)
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.128.217
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1513909459.A.014.html
推
12/22 10:37,
7年前
, 1F
12/22 10:37, 1F
→
12/22 10:38,
7年前
, 2F
12/22 10:38, 2F
System.NullReferenceException...
但是獲取串口的那段代碼並沒有任何修改,
--
string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);
int i = dis.Count;
DeviceInformation entry = (DeviceInformation)dis[0];
clientSocket = new StreamSocket();
HostName serverHost = new HostName(ServerIPAddress);
await clientSocket.ConnectAsync(serverHost, ServerPort);
try
{
serialPort = await SerialDevice.FromIdAsync(dis[0].Id);
if (serialPort == null) return;
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 115200;
serialPort.Parity = SerialParity.None;
serialPort.StopBits =SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
ReadCancellationTokenSource = new CancellationTokenSource(); //Listen();
--
可能會在
var dis = await DeviceInformation.FindAllAsync(aqs);
int i = dis.Count;
DeviceInformation entry = (DeviceInformation)dis[0];
clientSocket = new StreamSocket();
這其中的每一行都有可能crash..
是我哪裡用錯了嗎?謝謝
※ 編輯: james999 (220.132.128.217), 12/22/2017 11:44:00
→
12/22 13:03,
7年前
, 3F
12/22 13:03, 3F
※ 編輯: james999 (220.132.128.217), 12/22/2017 13:33:40
→
12/22 13:40,
7年前
, 4F
12/22 13:40, 4F
→
12/22 16:07,
7年前
, 5F
12/22 16:07, 5F
→
12/22 16:07,
7年前
, 6F
12/22 16:07, 6F
→
12/22 16:55,
7年前
, 7F
12/22 16:55, 7F
→
12/22 16:55,
7年前
, 8F
12/22 16:55, 8F
→
12/22 16:56,
7年前
, 9F
12/22 16:56, 9F
→
12/22 16:57,
7年前
, 10F
12/22 16:57, 10F
→
12/22 16:57,
7年前
, 11F
12/22 16:57, 11F
→
12/22 17:02,
7年前
, 12F
12/22 17:02, 12F
→
12/22 17:02,
7年前
, 13F
12/22 17:02, 13F
推
12/22 17:58,
7年前
, 14F
12/22 17:58, 14F
→
12/23 12:53,
7年前
, 15F
12/23 12:53, 15F
問題解決了,不要把new StreamSocket和開com port的程式寫在一塊就..沒事!!
具體等有空再查查,謝謝各位^^
※ 編輯: james999 (36.230.202.136), 12/23/2017 13:22:53
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章