Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created July 28, 2023 07:53
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/95dd85de3259fea2b148ca653b9421a2 to your computer and use it in GitHub Desktop.
Save dj1711572002/95dd85de3259fea2b148ca653b9421a2 to your computer and use it in GitHub Desktop.
C# SerialPort Recieve & Send  for GNSSRTK  F9P system
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
using System.IO;
using static System.Windows.Forms.LinkLabel;
using System.Diagnostics;
using System.Security.Policy;
using System.Reflection;
namespace STA23_Monitor_dgv_rev14
{
public partial class Form1 : Form
{
// Stopwatchクラス生成
Stopwatch sw = new System.Diagnostics.Stopwatch();
int i;
static int mcount, tset;
static string fnstrng;
static int dcount;
static string key;
static int stop_rcv;
static int itow0, itow0_1;
static long time0, time1, time2;
static int period;
static int ni;//CSV再生カウント
static int itowmiss;
static int ordererror;
static int mod;
static int disflag;
public struct rdata
{
public string[] lines;
public string[] sline;
public string[] sline1;
public string[] sline2;
public string[] sline3;
public string[] sline4;
public string itowB;
}
static rdata r;
public Form1()
{
InitializeComponent();
radioButton1.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.BaudRate = 115200;
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Handshake = Handshake.None;
serialPort1.PortName = portComboBox.Text;
serialPort1.Open();
sw.Start();
// LOG FILE NAME
//Filename Timestamp
DateTime dt = DateTime.Now;
string fn = dt.ToString($"{dt:MMddHHmmss}");
fnstrng = "c:/RTK_Log/mon_" + portComboBox.Text + "_" + fn + ".csv";
textBox3.Text = fnstrng;
}
public static bool isDataReceivedBusy;
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "STA23_Monitor_dgv_rev14";
tset = 0;
stop_rcv = 0;
key = " ";
label1.Text = "t1200_Mode";
period = 1200;
//
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
portComboBox.Items.Add(port);
}
if (portComboBox.Items.Count > 0)
{ portComboBox.SelectedIndex = 0; }
}
//
static async void Delay(int n)
{
await Task.Delay(n);//msec delay
}
delegate void DataDelegate(string sdata);
//=================printdata 内でデータ処理================================
private void printdata(string text)
{
//Debug.Print("Busy false=" + sw.ElapsedMilliseconds.ToString());
isDataReceivedBusy = false;
Debug.Print("[printdata IN]" + text);
if (textBox2.Text == "[s]" && label1.Text == "t1200_Mode")
{
richTextBox5.ScrollToCaret();
richTextBox5.AppendText(text);
}
//split itow check
if (text.Length > 3)
{
char[] c0 = text.ToCharArray();
if (c0[1] != '[' && c0[1] != '+')
{
ordererror = 1;
label3.Text = "ORDER ERROR:c0[1]=" + c0[1];
Debug.Print("ORDER ERROR:c0[1]=" + c0[1]);
}
else if (c0[1] == '[')
{
ordererror = 0;
if (c0[2] == 'B' && c0[3] == 'a')
{
string[] text1 = text.Split(',');
dcount++;
itow0_1 = itow0;
itow0 = Convert.ToInt32(text1[1]);
// Debug.Print("<<itow0-itow0_1=" + (itow0 - itow0_1).ToString() + ">>");
int sa = itow0 - itow0_1;
if (sa > 110 && sa < 130 )
{
label1.Text = "t120_Mode";
mod= 0;
Debug.Print("110<sa < 130:itow0=" + itow0.ToString() + ":mod=" + mod.ToString());
}
else if (sa > 1100)
{
mod = itow0%1200;
label1.Text = "t1200_Mode";
Debug.Print("sa > 1100:itow0=" + itow0.ToString() + ":mod=" + mod.ToString());
}
else
{
itowmiss++;
}
textBox1.Text = "itow=" + text1[1] + ":itowmiss=" + itowmiss.ToString() + ":dcount=" + dcount.ToString();
}
}
}
// Debug.Print("printdataIn="+sw.ElapsedMilliseconds.ToString());
// if (dcount % 10 == 0)
//{
// label2.Text = dcount.ToString();
// }
text = text.Remove(0, 1);
List<string> lines = new List<string>();
lines.Add(text);
File.AppendAllLines(@fnstrng, lines, Encoding.GetEncoding("shift-jis"));
if (ordererror == 0)
{
Debug.Print("**********************%mod CHECK: itow0%1200=" + (itow0 % 1200).ToString());
if (radioButton1.Checked == true && itow0 % 1200 == mod)
{
Debug.Print("******************richTextBox INN***********itow0=" + itow0.ToString());
richTextBox1.ScrollToCaret();
richTextBox2.ScrollToCaret();
richTextBox3.ScrollToCaret();
richTextBox4.ScrollToCaret();
richTextBox5.ScrollToCaret();
//richTextBox1.AppendText(text);
string[] text1 = text.Split('[');
// Debug.Print(">>>>>>>>>>>text1[0]=" + text1[0] + "text1[1]=" + text1[1]);
char[] c = text1[1].ToCharArray();
//Debug.Print("ifBRANCH:c[0]="+c[0]+ ":c[1]=" + c[1]+ ":c[2]=" + c[2]);
if (c[0] == 'm' || c[0] == 'p' || c[0] == 's')
{
richTextBox1.AppendText(text + "\r\n");
mcount++;
}
if (c[0] == 'B' && c[1] == 'a')
{
richTextBox2.AppendText(text + "\r\n");
}
if (c[0] == 'R' && c[1] == 'o')
{
richTextBox3.AppendText(text + "\r\n");
}
if (c[0] == 'B' && c[1] == 'N' || textBox2.Text == "[d]")
{
richTextBox4.AppendText(text + "\r\n");
}
if (c[0] == '<' || c[1] == '<')
{
richTextBox5.AppendText(text + "\r\n");
}
}
}
else if (textBox2.Text == "[d]")
{
richTextBox4.ScrollToCaret();
richTextBox4.AppendText(text);
}
//if (key != " ")
//{
// serialPort1.Write(key);
// key = " ";
//}
//Delay();
}
// -------------------Serial受信部--------------------------------------------------
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (checkBox2.Checked == false || disflag==0)
{
Debug.Print("DataRecieved=" + sw.ElapsedMilliseconds.ToString());
// フラグを設定する
isDataReceivedBusy = true;
string str = serialPort1.ReadLine();
DataDelegate a = new DataDelegate(printdata);
BeginInvoke(a, str);
}
else
{
serialPort1.DiscardInBuffer();
}
}
//-----------------------------------------------------------------------------------
//DISCARD
private void diset()
{
disflag = 1;
Delay(1000);
}
private void undis()
{
Delay(1000);
disflag = 0;
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.Close();
}
private void button4_Click(object sender, EventArgs e)
{
diset();
// textBox2.Text = "p";
Debug.Print("PUSH p");
if (isDataReceivedBusy == false)
{
serialPort1.Write("p");
key = "p";
textBox2.Text = "[p]";
undis();
}
}
// button "d" Click
private void button5_Click(object sender, EventArgs e)
{
diset();
//textBox2.Text = "d";
Debug.Print("PUSH d");
// if (serialPort1.IsOpen)
if (isDataReceivedBusy == false)
{
serialPort1.Write("d");
key = "d";
textBox2.Text = "[d]";
undis();
}
}
private void button6_Click(object sender, EventArgs e)
{
diset();
// textBox2.Text = "a";
Debug.Print("------------PUSH a");
//if (serialPort1.IsOpen)
Debug.Print("--------------aPushed=" + sw.ElapsedMilliseconds.ToString());
if (isDataReceivedBusy == false)
{
serialPort1.Write("a");
Debug.Print("-------------aSent=" + sw.ElapsedMilliseconds.ToString());
key = "a";
textBox2.Text = "[a]";
undis();
}
}
private void button2_Click_1(object sender, EventArgs e)
{
diset();
// textBox2.Text = "s";
Debug.Print("PUSH s");
//if (serialPort1.IsOpen)
if (!isDataReceivedBusy)
{
serialPort1.Write("s");
key = "s";
textBox2.Text = "[s]";
undis();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button8_KeyDown(object sender, KeyEventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
period = 120;
}
else
{
period = 1200;
}
}
private void button9_Click(object sender, EventArgs e)
{
diset();
Debug.Print("PUSH n");
//if (serialPort1.IsOpen)
if (!isDataReceivedBusy)
{
serialPort1.Write("n");
key = "n";
textBox2.Text = "[n]";
undis();
}
}
// 't' push
private void button8_Click(object sender, EventArgs e)
{
diset();
//label1.Text = "tPushed";
Debug.Print("------------tPushed=" + sw.ElapsedMilliseconds.ToString());
if (!isDataReceivedBusy)
{
serialPort1.Write("t");
Debug.Print("----------------tSent=" + sw.ElapsedMilliseconds.ToString());
key = "t";
//Delay();
textBox2.Text = "[t]";
if (tset <= 1)
{
tset = 10;
// label1.Text = "t120_Stop";
dcount = 0;
}
else
{
tset = 0;
// label1.Text = "t1200_Mode";
}
undis();
}
}
// READ CSV
private void button7_Click(object sender, EventArgs e)
{
ReadDialog(); ;
}
private void ReadDialog()
{
dgv1.Visible = true;
sw.Start();
string[] sline = new string[150000];
int irn = 0;
string sizes = null;
var swtch = new System.Diagnostics.Stopwatch();
//dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
//dgv1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
//dgv1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
//sourceBitmap_clear();
//OpenFileDialogクラスのインスタンスを作成
OpenFileDialog ofd = new OpenFileDialog();
//ダイアログを表示する
if (ofd.ShowDialog() == DialogResult.OK)
{
//richTextBox1.Clear();
int rowN = 0;
int i;
int fi = 0;
int ic = 0;
//OKボタンがクリックされたとき、選択されたファイルを読み取り専用で開く
textBox3.Text = ofd.FileName;
}
string alldata = File.ReadAllText(ofd.FileName);
// var wordList = alldata.Replace("\r\n", "\n").Split(new[] { //\n//, //\r// });
string[] alline = alldata.Split('[');//Header [で切取
Debug.Print("alline length=" + alline.Length);
for (int nj = 0; nj < alline.Length-10; nj++)
{
time1 = time0;
while (time0 - time1 < period)//Delay 1200msec
{
time0 = sw.ElapsedMilliseconds;
}
Debug.Print("allline[" + nj.ToString() + "] = " + alline[nj].ToString() + "time0=" + time0.ToString());
//Input TextBoxes
for (int ii = 0; ii < 10; ii++)
{
csvToTextBox(alline[nj]);
nj++;
}
}
}// ReadDialog
private void csvToTextBox(string text)
{
text= text.Replace(Environment.NewLine, "");
Debug.Print("csvToTextBox:text=" + text);
//---SPLIT ','
int POK=0;
if (text.Length > 10)
{
string[] text0 = text.Split(',');
Debug.Print("text0[0]=" + text0[0] + "text0[1]=" + text0[1]);
if (text0[0] == "Base]")
{
itow0_1 = itow0;
itow0 = Convert.ToInt32(text0[1]);
}
if (itow0 - itow0_1 > 1100)
{
POK = 1;
}
else if (itow0 - itow0_1 < 130 && itow0%1200==0)
{
POK = 1;
}
else
{
POK = 0;
}
}
if (radioButton1.Checked == true && text.Length>10 && POK==1)
{
richTextBox1.ScrollToCaret();
richTextBox2.ScrollToCaret();
richTextBox3.ScrollToCaret();
richTextBox4.ScrollToCaret();
richTextBox5.ScrollToCaret();
//richTextBox1.AppendText(text);
//string[] text1 = text.Split('[');
// Debug.Print(">>>>>>>>>>>text1[0]=" + text1[0] + "text1[1]=" + text1[1]);
char[] c = text.ToCharArray();//char配列にばらす
Debug.Print("c[0]=" + c[0] );
if (c[0] == 'm' || c[0] == 'p' || c[0] == 's')
{
Debug.Print(">>>>m]=" + text);
richTextBox1.AppendText(text);
mcount++;
}
if (c[0] == 'B' && c[1] == 'a')
{
string[] textB = text.Split(',');
Debug.Print("textB[0]=" + textB[0]);
Debug.Print("textB[1]=" + textB[1]);
Debug.Print("textB[2]=" + textB[2]);
Debug.Print("textB[3]=" + textB[3]);
Debug.Print("textB[4]=" + textB[4]);
Debug.Print("textB[5]=" + textB[5]);
Debug.Print("textB[6]=" + textB[6]);
Debug.Print("textB[7]=" + textB[7]);
Debug.Print("textB[8]=" + textB[8]);
Debug.Print("textB[9]=" + textB[9]);
string texts = textB[0] + textB[1] + "[" + textB[2] + "][" + textB[3] + "][" + textB[4] + "]" + textB[5] + textB[6] + textB[7] + textB[8] + textB[9];
richTextBox2.AppendText(texts);
}
if (c[0] == 'R' && c[1] == 'o')
{
string[] textR = text.Split(',');
Debug.Print("textR[0]=" + textR[0]);
Debug.Print("textR[1]=" + textR[1]);
Debug.Print("textR[2]=" + textR[2]);
Debug.Print("textR[3]=" + textR[3]);
Debug.Print("textR[4]=" + textR[4]);
Debug.Print("textR[5]=" + textR[5]);
Debug.Print("textR[6]=" + textR[6]);
Debug.Print("textR[7]=" + textR[7]);
string textr = textR[0] + textR[1] + "[" + textR[2] + "][" + textR[3] + "][" + textR[4] + "]" + textR[5].PadLeft(3,' ') + "[" + textR[6] + "]" + textR[7]+"\r\n" ;
richTextBox3.AppendText(textr);
}
if (c[0] == 'B' && c[1] == 'N' || textBox2.Text == "[d]")
{
richTextBox4.AppendText(text);
}
if (c[0] == '<' || c[1] == '<')
{
richTextBox5.AppendText(text);
}
}
else if (textBox2.Text == "[d]")
{
richTextBox4.ScrollToCaret();
richTextBox4.AppendText(text);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment