###前提・実現したいこと
Visual C++ Windows フォームアプリケーションでシリアル通信を行うためのテストプログラムを作成しています。
受信を行う機能を実装中に以下の問題が発生しました。
###発生している問題・エラーメッセージ
エラーメッセージは出ていません。 また送信機能は動いているのですが受信機能の方が全く動いておらず受信が出来ない状態で困っています。 また動作の様子を見ているとどうやら受信のイベントがまずもって動いていないようです。
###該当のソースコード
Visual
1 2using System; 3using System.Collections.Generic; 4using System.ComponentModel; 5using System.Data; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12namespace シリアル通信{ 13 14 public partial class Form1 : Form{ 15 public Form1(){ 16 InitializeComponent(); 17 } 18 19 private void start_button_Click(object sender, EventArgs e) 20 { 21 try{ 22 if (!SerialPort1.IsOpen){ 23 //ポートの設定 24 SerialPort1.PortName = "COM1"; 25 SerialPort1.BaudRate = 9600; 26 27 //ポートを開く 28 SerialPort1.Open(); 29 status.Text = "接続済み"; 30 } 31 } 32 catch (Exception ex){ 33 MessageBox.Show("エラー\nポートが開けません"); 34 } 35 } 36 37 private void stop_button_Click(object sender, EventArgs e){ 38 try{ 39 if (SerialPort1.IsOpen){ 40 //ポートを閉じる 41 SerialPort1.Close(); 42 status.Text = "未接続"; 43 } 44 } 45 catch (Exception ex){ 46 MessageBox.Show("エラー\nポートが閉じれません"); 47 } 48 } 49 50 51 private void send_button_Click(object sender, EventArgs e){ 52 if (SerialPort1.IsOpen){ 53 //送信する 54 SerialPort1.Write(send_box.Text); 55 SerialPort1.Write("\r\n"); 56 } 57 } 58 59 60 private void SerialPort1_DataReceived(object sender, EventArgs e){ 61 try{ 62 label2.Text = "受信"; 63 string data = SerialPort1.ReadExisting(); 64 reed_box.Text = data; 65 } 66 catch (Exception ex){ 67 MessageBox.Show("エラー\n読み込めません"); 68 } 69 } 70 71 } 72} 73
###試したこと
引数を変更してみたりイベントが動いているのか調べてみたりはしてみましたが効果はありませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
開発環境 Visual Studio 2017
開発言語 Visual C++ Windows フォームアプリケーション
その他 シリアル通信に用いているのはWindowsフォーム標準のSerialPortクラス
これC++じゃない。C#だ。
回答1件
あなたの回答
tips
プレビュー