0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その607

Last updated at Posted at 2025-08-02

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

ラズベリーパイ picoに、シリアルからコードを送り、実行せよ。

写真

image.png

サンプルコード


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 = @"
stack = []
def add():
    global stack
    top1 = stack.pop()
    top2 = stack.pop()
    v = top2 + top1
    stack.append(v)
def sub():
    global stack
    top1 = stack.pop()
    top2 = stack.pop()
    v = top2 - top1
    stack.append(v)
def mul():
    global stack
    top1 = stack.pop()
    top2 = stack.pop()
    v = top2 * top1
    stack.append(v)
def div():
    global stack
    top1 = stack.pop()
    top2 = stack.pop()
    v = top2 / top1
    stack.append(v)
def dup():
    global stack
    top1 = stack.pop()
    stack.append(top1)
    stack.append(top1)
def drop():
    global stack
    top1 = stack.pop()
def get():
    global stack
    top1 = stack.pop()
    print(top1)
def push(v):
    global stack
    stack.append(int(v))
def run(str):
    s = str.split(' ')
    for i in s: 
        if i == ""+"":
            add()
        elif i == ""-"":
            sub()
        elif i == ""*"":
            mul()
        elif i == ""/"":
            div()
        elif i == ""dup"":
            dup()
        elif i == ""drop"":
            drop()
        elif i == ""."":
            get()
        else:
            push(i)
run(""9 9 - 9 9 / ."")# 1
run(""9 9 / 9 9 / + ."")# 2
run(""9 9 + 9 + 9 / ."")# 3
run(""9 9 9 + 9 / dup + ."")# 4
run(""9 9 9 + 9 / dup + - ."")# 5
run(""9 dup 9 + 9 + 9 / - ."")# 6
run(""9 9 9 + 9 / - ."")# 7  
run(""9 9 9 drop 9 / - ."")# 8  
run(""9 9 - 9 * 9 + ."")# 9  
run(""9 9 / 9 dup 9 / + ."")# 10  
run(""9 9 9 + 9 / + ."")# 11  
run(""9 dup 9 9 + + 9 / + ."")# 12  
run(""9 9 9 + 9 / dup + + ."")# 13  
run(""9 dup 9 9 + 9 / dup + - + ."")# 14  
run(""9 dup dup 9 + 9 + 9 / - + ."")# 15  
";
		textBox1 = new TextBox();
		textBox1.Location = new Point(400, 20);
		textBox1.Multiline = false;
		textBox1.Size = new Size(300, 20);
		textBox1.Text = @"";
		textBox2 = new TextBox();
		textBox2.Location = new Point(400, 80);
		textBox2.TabIndex = 2;
		textBox2.Multiline = true;
		textBox2.ScrollBars = ScrollBars.Both;
		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;
		//Console.WriteLine(msg);
		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());
	}
}






実行結果


paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== 
=== 
stack = []
=== 
def add():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    top2 = stack.pop()
=== 
    v = top2 + top1
=== 
    stack.append(v)
=== 
def sub():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    top2 = stack.pop()
=== 
    v = top2 - top1
=== 
    stack.append(v)
=== 
def mul():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    top2 = stack.pop()
=== 
    v = top2 * top1
=== 
    stack.append(v)
=== 
def div():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    top2 = stack.pop()
=== 
    v = top2 / top1
=== 
    stack.append(v)
=== 
def dup():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    stack.append(top1)
=== 
    stack.append(top1)
=== 
def drop():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
def get():
=== 
    global stack
=== 
    top1 = stack.pop()
=== 
    print(top1)
=== 
def push(v):
=== 
    global stack
=== 
    stack.append(int(v))
=== 
def run(str):
=== 
    s = str.split(' ')
=== 
    for i in s: 
=== 
        if i == "+":
=== 
            add()
=== 
        elif i == "-":
=== 
            sub()
=== 
        elif i == "*":
=== 
            mul()
=== 
        elif i == "/":
=== 
            div()
=== 
        elif i == "dup":
=== 
            dup()
=== 
        elif i == "drop":
=== 
            drop()
=== 
        elif i == ".":
=== 
            get()
=== 
        else:
=== 
            push(i)
=== 
run("9 9 - 9 9 / .")# 1
=== 
run("9 9 / 9 9 / + .")# 2
=== 
run("9 9 + 9 + 9 / .")# 3
=== 
run("9 9 9 + 9 / dup + .")# 4
=== 
run("9 9 9 + 9 / dup + - .")# 5
=== 
run("9 dup 9 + 9 + 9 / - .")# 6
=== 
run("9 9 9 + 9 / - .")# 7  
=== 
run("9 9 9 drop 9 / - .")# 8  
=== 
run("9 9 - 9 * 9 + .")# 9  
=== 
run("9 9 / 9 dup 9 / + .")# 10  
=== 
run("9 9 9 + 9 / + .")# 11  
=== 
run("9 dup 9 9 + + 9 / + .")# 12  
=== 
run("9 9 9 + 9 / dup + + .")# 13  
=== 
run("9 dup 9 9 + 9 / dup + - + .")# 14  
=== 
run("9 dup dup 9 + 9 + 9 / - + .")# 15  
=== 

=== 

=== 

1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9
10.0
11.0
12.0
13.0
14.0
15.0
>>> 
>>> 

以上。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?