-
-
Save dj1711572002/d3437ee44dba63105f26ed022372c24f to your computer and use it in GitHub Desktop.
C# GNSS RTK SerialMonitor rev15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_rev15 | |
{ | |
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; | |
static long lookt; | |
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; | |
//lookt = sw.ElapsedMilliseconds; | |
} | |
public static bool isDataReceivedBusy; | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
this.Text = "STA23_Monitor_dgv_rev15"; | |
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; } | |
//ComboBox1 | |
comboBox1.Items.Add("30"); | |
comboBox1.Items.Add("60"); | |
comboBox1.Items.Add("120"); | |
comboBox1.Items.Add("180"); | |
comboBox1.Items.Add("240"); | |
comboBox1.Items.Add("300"); | |
comboBox1.SelectedIndex = 1; | |
} | |
// | |
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")); | |
long nowt = sw.ElapsedMilliseconds; | |
int combot = Convert.ToInt32(comboBox1.SelectedItem)*1000; | |
Debug.Print("++++++nowt="+nowt.ToString()+"lookt=" + lookt.ToString() + "combot=" + combot.ToString()); | |
if (nowt - lookt > combot) | |
{ | |
label4.Text = " "; | |
} | |
if (ordererror == 0) | |
{ | |
Debug.Print("**********************%mod CHECK: itow0%1200=" + (itow0 % 1200).ToString()); | |
if (radioButton1.Checked == true && itow0 % 1200 == mod && nowt-lookt<combot) | |
{ | |
int tt = combot/1000-(int)(nowt - lookt) / 1000; | |
label4.Text=tt.ToString()+"sec"; | |
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(); | |
} | |
private void ToolStripButton10_Click(object sender, EventArgs e) | |
{ | |
ToolStripButton tsb = (ToolStripButton)sender; | |
//チェック状態を反転させる | |
tsb.Checked = !tsb.Checked; | |
} | |
// -------------------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(); | |
} | |
} | |
private void button10_Click(object sender, EventArgs e) | |
{ | |
lookt = sw.ElapsedMilliseconds; | |
} | |
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
} | |
private void button11_Click(object sender, EventArgs e) | |
{ | |
Environment.Exit(0x8020); | |
} | |
// '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(); | |
//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[] del = { "\r\n" }; | |
//string[] arr = str.Split(del, StringSplitOptions.None); | |
string[] alline = alldata.Split(del, StringSplitOptions.None);//Header [で切取 | |
Debug.Print("alline len=" + alline.Length); | |
// time0 = sw.ElapsedMilliseconds; | |
for (int nj = 0; nj < alline.Length ; nj++) | |
{ | |
// } | |
Debug.Print(">>>>allline[" + nj.ToString() + "] = " + alline[nj].ToString() + "time0=" + time0.ToString()); | |
//richTextBoxへ10行分書き込み[m][Base][Rover]{BNO]1-6{MAG] | |
int n10=0; | |
while (n10<10) | |
{ | |
csvToTextBox(alline[nj%(alline.Length)]); | |
nj++; | |
n10++; | |
} | |
} | |
}// ReadDialog | |
private void csvToTextBox(string text) | |
{ | |
text= text.Replace(Environment.NewLine, ""); | |
Debug.Print("csvToTextBox:text=" + text); | |
//Debug.Print("******itow0%1200=" + (itow0 % 1200).ToString()+":mod="+mod.ToString()); | |
if (radioButton1.Checked == true && text.Length>1 ) | |
{ | |
//Debug.Print("INN******itow0%1200=" + (itow0 % 1200).ToString() + ":mod=" + mod.ToString()); | |
richTextBox1.ScrollToCaret(); | |
richTextBox2.ScrollToCaret(); | |
richTextBox3.ScrollToCaret(); | |
richTextBox4.ScrollToCaret(); | |
richTextBox5.ScrollToCaret(); | |
if(text.IndexOf("[m]")>=0 || text.IndexOf("[p]") >= 0 || text.IndexOf("[s]") >= 0) | |
{ | |
Debug.Print(">>>>m]=" + text); | |
richTextBox1.AppendText(text+"\r\n"); | |
mcount++; | |
} | |
Debug.Print("IndexOf[Base]=" + text.IndexOf("[Base]".ToString())+":text="+text); | |
if(text.IndexOf("Base")>=0) | |
{ | |
richTextBox2.AppendText(text + "\r\n"); | |
} | |
if(text.IndexOf("Rover")>=0) | |
{ | |
richTextBox3.AppendText(text + "\r\n"); | |
} | |
Debug.Print("IndexOf BNO=" + text.IndexOf("BNO").ToString() + ":text=" + text); | |
if (text.IndexOf("BNO")>=0) | |
{ | |
Debug.Print("==============INN BNO============="); | |
richTextBox4.AppendText(text + "\r\n"); | |
} | |
if (text.IndexOf("[m]") >= 0) | |
{ | |
textBox2.Text = "[m]"; | |
} | |
if (text.IndexOf("<<") >= 0) | |
{ | |
richTextBox5.AppendText(text + "\r\n"); | |
} | |
if (text.IndexOf("[s]")>=0) | |
{ | |
textBox2.Text = "[s]"; | |
} | |
if (text.IndexOf("[p]") >= 0) | |
{ | |
textBox2.Text = "[p]"; | |
} | |
if (text.IndexOf("d") >= 0) | |
{ | |
textBox2.Text = "[d]"; | |
} | |
if (textBox2.Text == ("[d]")) | |
{ | |
richTextBox4.ScrollToCaret(); | |
richTextBox4.AppendText(text+"\r\n"); | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment