Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Last active April 12, 2023 23:50
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/2a0e9e81c3921266c8fe0d28db72ad49 to your computer and use it in GitHub Desktop.
Save dj1711572002/2a0e9e81c3921266c8fe0d28db72ad49 to your computer and use it in GitHub Desktop.
C# SerialTest Recive & Send flag
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;
namespace STA23_SerialTest_rev02
{
public partial class Form1 : Form
{
int i;
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();
}
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();
}
public static bool isDataReceivedBusy ;
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "STA23_Serial_Test_rev02";
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
portComboBox.Items.Add(port);
}
if (portComboBox.Items.Count > 0)
portComboBox.SelectedIndex = 0;
}
delegate void DataDelegate(string sdata);
//=================printdata 内でデータ処理================================
private void printdata(string text)
{
Debug.Print("[printdata IN]"+text);
richTextBox1.AppendText(text);
if (text.IndexOf('[') > 0)
{
r.lines = text.Split('[');
Debug.Print("lines=" + r.lines[1]);
if (r.lines.Length > 0)
{
r.sline = r.lines[1].Split(',');
Debug.Print("sline=" + r.sline[1]);
if (r.sline.Length>0)
{
if (r.sline[0] == "m]" || r.sline[0] == "p]" || r.sline[0] == "s]")
{
r.sline1 = r.sline;
Debug.Print("sline1=" + r.sline1[1]);
inputtbox1(r.sline1);
textBox2.Text = "[" + r.sline1[0];
}
if (r.sline[0] == "Base]")
{
r.sline2 = r.sline;
Debug.Print("sline2=" + r.sline2[1]);
inputtbox2(r.sline2);
}
if (r.sline[0] == "Rover]")
{
r.sline3 = r.sline;
Debug.Print("sline3=" + r.sline3[1]);
}
if (r.sline[0] == "BNO]")
{
r.sline4 = r.sline;
Debug.Print("sline4=" + r.sline4[1]);
}
}
}
}
isDataReceivedBusy = false;
}
// -------------------Serial受信部--------------------------------------------------
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// フラグを設定する
isDataReceivedBusy = true;
string str = serialPort1.ReadLine();
DataDelegate a = new DataDelegate(printdata);
BeginInvoke(a, str);
}
//-----------------------------------------------------------------------------------
//[m]:md,flagsB,flagsR,pdopB,pdopR,haccB,haccR,miscount0,misflags,count_upB-count_upB0,tp,tp0
//[Base]:itowB,NYB,EXB,DZB,gSpeedB,headMotB)
//[Rover]:itowR,NYR,EXR,DZR,gSpeedR,mHead,bnHosei+eX[0],relLm);
//[BNO]:eX[0],eY[0],eZ[0],laX[0],laY[0],laZ[0],grX[0],grY[0],grZ[0]);
private void inputtbox1(string[] s)//[m]行入力 textBox3-11
{
textBox3.Text = s[1];//flagsB
textBox4.Text = s[2];//flagsR
textBox5.Text = s[5];//haccB
textBox6.Text = s[6];//haccR
textBox7.Text = s[7];//miscount
textBox8.Text = s[8];//misflags
textBox9.Text = s[9];//coutn_upB
}
private void inputtbox2(string[] s)//[Base]行入力 textBox3-11
{
textBox11.Text = s[1];//itowB
textBox12.Text = s[2];//NYB
textBox13.Text = s[3];//EXB
textBox14.Text = s[4];//DZR
textBox15.Text = s[5];//gSpeed
textBox16.Text = s[6];//headMotB
}
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");
}
}
private void button5_Click(object sender, EventArgs e)
{
//textBox2.Text = "d";
Debug.Print("PUSH d");
// if (serialPort1.IsOpen)
if (isDataReceivedBusy==false)
{
serialPort1.Write("d");
textBox2.Text = "[d]";
}
}
private void button6_Click(object sender, EventArgs e)
{
// textBox2.Text = "a";
Debug.Print("PUSH a");
//if (serialPort1.IsOpen)
if (isDataReceivedBusy==false)
{
serialPort1.Write("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");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment