概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
ラズベリーパイ picoに、シリアルからコードを送り、実行せよ。
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Threading;
class form1: Form {
private Button button1;
private Button button2;
private TextBox textBox0;
private TextBox textBox1;
private static TextBox textBox2;
private SerialPort port1;
form1() {
Text = "pico IDE";
ClientSize = new Size(800, 400);
button1 = new Button();
button1.Location = new Point(300, 340);
button1.Text = "setup";
button1.Click += new EventHandler(button1_Click);
button2 = new Button();
button2.Location = new Point(700, 50);
button2.Text = "send";
button2.Click += new EventHandler(button2_Click);
textBox0 = new TextBox();
textBox0.Location = new Point(30, 20);
textBox0.Multiline = true;
textBox0.Size = new Size(300, 300);
textBox0.Text = @"
from machine import Pin
from rp2 import PIO, StateMachine, asm_pio
@asm_pio(set_init = PIO.OUT_LOW)
def tic():
set(pins, 1)
set(pins, 0)
sm = StateMachine(0, tic, freq = 3000, set_base = Pin(25))
";
textBox1 = new TextBox();
textBox1.Location = new Point(400, 20);
textBox1.Multiline = false;
textBox1.Size = new Size(300, 20);
textBox1.Text = @"sm.active(1)";
textBox2 = new TextBox();
textBox2.Location = new Point(400, 80);
textBox2.Multiline = true;
textBox2.Size = new Size(380, 300);
textBox2.Text = @"";
port1 = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
port1.BaudRate = 115200;
port1.Parity = Parity.None;
port1.DataBits = 8;
port1.StopBits = StopBits.One;
port1.Handshake = Handshake.None;
port1.PortName = "COM3";
port1.Encoding = System.Text.Encoding.ASCII;
port1.NewLine = "\r\n";
port1.DtrEnable = true;
port1.RtsEnable = true;
port1.DataReceived += new SerialDataReceivedEventHandler(DataReceived_Handler);
port1.Open();
Controls.AddRange(new Control[] {
button1,
button2,
textBox0,
textBox1,
textBox2
});
}
private void button1_Click(object sender, EventArgs e) {
string s = "e";
byte[] array = Encoding.UTF8.GetBytes(s);
array[0] = 5;
string ctrl_e = Encoding.UTF8.GetString(array);
array[0] = 4;
string ctrl_d = Encoding.UTF8.GetString(array);
string msg;
msg = ctrl_e + textBox0.Text + "\r\n\r\n" + ctrl_d + textBox1.Text;
port1.WriteLine(msg);
}
private void button2_Click(object sender, EventArgs e) {
string msg;
msg = textBox1.Text;
port1.WriteLine(msg);
}
private delegate void SetTextDelegate(string msg);
private static void SetText(string msg) {
textBox2.Text = msg;
}
private void DataReceived_Handler(object sender, SerialDataReceivedEventArgs e) {
SerialPort sp = (SerialPort) sender;
string msg = sp.ReadExisting();
this.Invoke(new SetTextDelegate(SetText), new object[] { msg });
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。