-
-
Save dj1711572002/44806577833dada589f783748b72c751 to your computer and use it in GitHub Desktop.
C# Serial Recive '& Send RTK IMUdata LOGGING
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; | |
namespace STA23_Monitor_dgv_rev12 | |
{ | |
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 int itowmiss; | |
static int ordererror; | |
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_rev12"; | |
tset = 0; | |
stop_rcv = 0; | |
key = " "; | |
label1.Text = "t1200_Mode"; | |
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]") | |
{ | |
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_Stop"; | |
} | |
else if(sa>1100) | |
{ | |
label1.Text = "t1200_Mode"; | |
} | |
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 ) | |
{ | |
if (radioButton1.Checked == true && itow0 - itow0_1 > 1100 ) | |
{ | |
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("c[0]=" + Convert.ToInt32(c[0]) + "c[1]=" + Convert.ToInt32(c[1]) + "c[2]=" + c[2]); | |
if (c[0] == 'm' || c[0] == 'p' || c[0] == 's' ) | |
{ | |
richTextBox1.AppendText(text); | |
mcount++; | |
} | |
if ( c[0] == 'B' && c[1] == 'a') | |
{ | |
richTextBox2.AppendText(text); | |
} | |
if (c[0] == 'R' && c[1] == 'o') | |
{ | |
richTextBox3.AppendText(text); | |
} | |
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); | |
} | |
//if (key != " ") | |
//{ | |
// serialPort1.Write(key); | |
// key = " "; | |
//} | |
//Delay(); | |
} | |
// -------------------Serial受信部-------------------------------------------------- | |
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) | |
{ | |
if (stop_rcv == 0) | |
{ | |
Debug.Print("DataRecived=" + sw.ElapsedMilliseconds.ToString()); | |
// フラグを設定する | |
isDataReceivedBusy = true; | |
string str = serialPort1.ReadLine(); | |
DataDelegate a = new DataDelegate(printdata); | |
BeginInvoke(a, str); | |
} | |
} | |
//----------------------------------------------------------------------------------- | |
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) | |
{ | |
// textBox2.Text = "p"; | |
Debug.Print("PUSH p"); | |
if (isDataReceivedBusy==false) | |
{ | |
serialPort1.Write("p"); | |
key = "p"; | |
textBox2.Text = "[p]"; | |
} | |
} | |
// button "d" Click | |
private void button5_Click(object sender, EventArgs e) | |
{ | |
//textBox2.Text = "d"; | |
Debug.Print("PUSH d"); | |
// if (serialPort1.IsOpen) | |
if (isDataReceivedBusy==false) | |
{ | |
serialPort1.Write("d"); | |
key = "d"; | |
textBox2.Text = "[d]"; | |
} | |
} | |
private void button6_Click(object sender, EventArgs e) | |
{ | |
// 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]"; | |
} | |
} | |
private void button2_Click_1(object sender, EventArgs e) | |
{ | |
// textBox2.Text = "s"; | |
Debug.Print("PUSH s"); | |
//if (serialPort1.IsOpen) | |
if (!isDataReceivedBusy) | |
{ | |
serialPort1.Write("s"); | |
key = "s"; | |
textBox2.Text = "[s]"; | |
} | |
} | |
private void textBox1_TextChanged(object sender, EventArgs e) | |
{ | |
} | |
private void button7_Click(object sender, EventArgs e) | |
{ | |
textBox2.Text = " "; | |
} | |
private void button8_KeyDown(object sender, KeyEventArgs e) | |
{ | |
} | |
private void checkBox1_CheckedChanged(object sender, EventArgs e) | |
{ | |
if (checkBox1.Checked== true) | |
{ | |
stop_rcv = 1; | |
} | |
else | |
{ | |
stop_rcv = 0; | |
} | |
} | |
// 't' push | |
private void button8_Click(object sender, EventArgs e) | |
{ | |
//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"; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment