Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created March 10, 2024 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/cfae8b87e1c44b76ea05330f372a9b2e to your computer and use it in GitHub Desktop.
Save dj1711572002/cfae8b87e1c44b76ea05330f372a9b2e to your computer and use it in GitHub Desktop.
C# Console SerialReciving Monitor sendkey recieving
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STA24_Console_MB_SerialMon_rev00
{
internal class Program
{
public static void serialOpen(string comst)
{
}
static char key;
static string comst="COM16";
static void Main(string[] args)
{
Console.WriteLine("SerialPort default No="+comst+"(hit any key) or Change (KeyIn 'c')");
var inp = Console.ReadKey();//キーインしても表示しない
key = inp.KeyChar;
if (key == 'c')
{
Console.Write("Input COM No=");
comst = Console.ReadLine();
//int comn = Convert.ToInt32(comst);
}
//
SerialPort mySerialPort = new SerialPort(comst);
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Key In a:start ");
while (true)
{
key = ' ';
inp = Console.ReadKey();//キーインしても表示しない
key = inp.KeyChar;
if (key != ' ' && key!='c')
{
string skey = key.ToString();
mySerialPort.Write(skey);
}
}
//Console.WriteLine("Press any key to continue...");
//Console.WriteLine();
//Console.ReadKey();
//mySerialPort.Close();
/*
Console.WriteLine("Key In a start ");
var inp = Console.ReadKey();//キーインしても表示しない
key = inp.KeyChar;
if (key == 'a')
{
//string dataToSend = "Hello Serial";
//serialPort.Write(dataToSend);
// データ受信
}
Console.ReadLine();
*/
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
//Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment