質問編集履歴
2
状況進展・追記2
test
CHANGED
File without changes
|
test
CHANGED
@@ -363,3 +363,301 @@
|
|
363
363
|
|
364
364
|
|
365
365
|
お力貸して頂けますと幸いですm(__)m
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
##追記2
|
370
|
+
|
371
|
+
`serialPort.ReadLine()`のコードで
|
372
|
+
|
373
|
+
The operation has timed-out
|
374
|
+
|
375
|
+
というエラーが出てしまいます。
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
今回、Unityから信号を送信するだけで受信は必要ないため、
|
380
|
+
|
381
|
+
`ReadLine()`のコードをコメントアウトしてみたところ、
|
382
|
+
|
383
|
+
エラーははかなくなったのですが、
|
384
|
+
|
385
|
+
`serialPort.Write()`を実行しても、COM6へ送信されていないような感じでした(電子錠が動作しない)
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
また、もうひとつ試みたこととして、
|
390
|
+
|
391
|
+
Windows10 & Unity5.x が 丁度シリアル通信のバグがある、
|
392
|
+
|
393
|
+
というような情報をいくつかみかけたため、Unity4.6もインストールしてみました。
|
394
|
+
|
395
|
+
ですが、状況は変わりませんでした。
|
396
|
+
|
397
|
+
https://twitter.com/kohack_v/status/855849304099041281
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
どのようにすると、UnityからCOMポートへ送信できますでしょうか?
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
引き続き、アドバイス頂けますと幸いです。
|
406
|
+
|
407
|
+
何卒宜しくお願い致します。
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
▽少し変更しました。(`serialPort.ReadTimeout`を追加等しています。)
|
412
|
+
|
413
|
+
```C#
|
414
|
+
|
415
|
+
using UnityEngine;
|
416
|
+
|
417
|
+
using System.Collections;
|
418
|
+
|
419
|
+
using System.IO.Ports;
|
420
|
+
|
421
|
+
using System.Threading;
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
public class SerialHandler : MonoBehaviour
|
426
|
+
|
427
|
+
{
|
428
|
+
|
429
|
+
public delegate void SerialDataReceivedEventHandler(string message);
|
430
|
+
|
431
|
+
public event SerialDataReceivedEventHandler OnDataReceived = delegate { };
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
public string portName = "COM6"; //各自のマイコンのCOMポート
|
436
|
+
|
437
|
+
public int baudRate = 9600;
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
private SerialPort serialPort_;
|
442
|
+
|
443
|
+
private Thread thread_;
|
444
|
+
|
445
|
+
private bool isRunning_ = false;
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
private string message_;
|
450
|
+
|
451
|
+
private bool isNewMessageReceived_ = false;
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
private string _on = "relay on 0";
|
456
|
+
|
457
|
+
private string _off = "relay off 0";
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
void Awake()
|
462
|
+
|
463
|
+
{
|
464
|
+
|
465
|
+
Open();
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
void Update()
|
472
|
+
|
473
|
+
{
|
474
|
+
|
475
|
+
if (isNewMessageReceived_)
|
476
|
+
|
477
|
+
{
|
478
|
+
|
479
|
+
OnDataReceived(message_);
|
480
|
+
|
481
|
+
//Debug.Log(message_); //受信したデータの確認表示用
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
void OnDestroy()
|
490
|
+
|
491
|
+
{
|
492
|
+
|
493
|
+
Close();
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
private void Open()
|
500
|
+
|
501
|
+
{
|
502
|
+
|
503
|
+
serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
|
504
|
+
|
505
|
+
serialPort_.ReadTimeout = 20;
|
506
|
+
|
507
|
+
serialPort_.Open();
|
508
|
+
|
509
|
+
serialPort_.NewLine = "\n";
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
isRunning_ = true;
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
thread_ = new Thread(Read);
|
518
|
+
|
519
|
+
thread_.Start();
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
private void Close()
|
526
|
+
|
527
|
+
{
|
528
|
+
|
529
|
+
isRunning_ = false;
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
if (thread_ != null && thread_.IsAlive)
|
534
|
+
|
535
|
+
{
|
536
|
+
|
537
|
+
thread_.Join();
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
if (serialPort_ != null && serialPort_.IsOpen)
|
544
|
+
|
545
|
+
{
|
546
|
+
|
547
|
+
serialPort_.Close();
|
548
|
+
|
549
|
+
serialPort_.Dispose();
|
550
|
+
|
551
|
+
}
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
private void Read()
|
558
|
+
|
559
|
+
{
|
560
|
+
|
561
|
+
while (isRunning_ && serialPort_ != null && serialPort_.IsOpen)
|
562
|
+
|
563
|
+
{
|
564
|
+
|
565
|
+
try
|
566
|
+
|
567
|
+
{
|
568
|
+
|
569
|
+
//message_ = serialPort_.ReadLine(); //ここでエラーがでる
|
570
|
+
|
571
|
+
isNewMessageReceived_ = true;
|
572
|
+
|
573
|
+
}
|
574
|
+
|
575
|
+
catch (System.Exception e)
|
576
|
+
|
577
|
+
{
|
578
|
+
|
579
|
+
Debug.LogWarning(e.Message);
|
580
|
+
|
581
|
+
}
|
582
|
+
|
583
|
+
}
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
public void Write(string message)
|
590
|
+
|
591
|
+
{
|
592
|
+
|
593
|
+
try
|
594
|
+
|
595
|
+
{
|
596
|
+
|
597
|
+
serialPort_.Write(message);
|
598
|
+
|
599
|
+
}
|
600
|
+
|
601
|
+
catch (System.Exception e)
|
602
|
+
|
603
|
+
{
|
604
|
+
|
605
|
+
Debug.LogWarning(e.Message);
|
606
|
+
|
607
|
+
}
|
608
|
+
|
609
|
+
}
|
610
|
+
|
611
|
+
|
612
|
+
|
613
|
+
public void RelayOnBtnPush()
|
614
|
+
|
615
|
+
{
|
616
|
+
|
617
|
+
Debug.LogWarning("RelayOnBtnPush()");
|
618
|
+
|
619
|
+
|
620
|
+
|
621
|
+
try{
|
622
|
+
|
623
|
+
Debug.Log("serialPort_ = " + serialPort_);
|
624
|
+
|
625
|
+
serialPort_.Write("relay on 0");
|
626
|
+
|
627
|
+
}
|
628
|
+
|
629
|
+
catch (System.Exception e){
|
630
|
+
|
631
|
+
Debug.Log(e.Message);
|
632
|
+
|
633
|
+
Debug.LogWarning("NO WRITE"); //エラーはでていないようです
|
634
|
+
|
635
|
+
}
|
636
|
+
|
637
|
+
finally {
|
638
|
+
|
639
|
+
Debug.LogWarning("FINISH WRITE");
|
640
|
+
|
641
|
+
}
|
642
|
+
|
643
|
+
}
|
644
|
+
|
645
|
+
|
646
|
+
|
647
|
+
public void RelayOffBtnPush()
|
648
|
+
|
649
|
+
{
|
650
|
+
|
651
|
+
Debug.LogWarning("RelayOnBtnPush()");
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
serialPort_.Write("relay off 0");
|
656
|
+
|
657
|
+
}
|
658
|
+
|
659
|
+
|
660
|
+
|
661
|
+
}
|
662
|
+
|
663
|
+
```
|
1
状況進展・追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -325,3 +325,41 @@
|
|
325
325
|
|
326
326
|
|
327
327
|
ここまでの長文を読んでいただき、ありがとうございます。
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
##追記
|
332
|
+
|
333
|
+
どこで`serialPrort_`がnullになっているのか確認してみたところ、
|
334
|
+
|
335
|
+
newされる行でエラー(下記)がでており、生成できていないためにnullになっていることがわかりました。
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
初めからあったエラーなのですが、
|
340
|
+
|
341
|
+
`PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows.`と出てしまいます。
|
342
|
+
|
343
|
+
「System.IO.Portsが現在はwindowsだけしかサポートされていません」、という意味と思いますので、
|
344
|
+
|
345
|
+
ビルドプラットフォームを、Universal Windows Platform に変更したのですが、
|
346
|
+
|
347
|
+
エラーが消えない状況です。
|
348
|
+
|
349
|
+
unity再起動も試みましたが、変わりませんでした。
|
350
|
+
|
351
|
+

|
352
|
+
|
353
|
+
> PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows.
|
354
|
+
|
355
|
+
System.IO.Ports.SerialPort..ctor (System.String portName, System.Int32 baudRate, System.IO.Ports.Parity parity, System.Int32 dataBits, System.IO.Ports.StopBits stopBits) (at <9eedbed1e64b4c2d97edd8d4a1e07964>:0)
|
356
|
+
|
357
|
+
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort..ctor(string,int,System.IO.Ports.Parity,int,System.IO.Ports.StopBits)
|
358
|
+
|
359
|
+
SerialHandler.Open () (at Assets/Scripts/SerialHandler.cs:47)
|
360
|
+
|
361
|
+
SerialHandler.Start () (at Assets/Scripts/SerialHandler.cs:26)
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
お力貸して頂けますと幸いですm(__)m
|