1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

processing > Serial受信してみる / echo server

Last updated at Posted at 2016-07-13
動作環境
Processing 3.1.1
Windows 8.1 pro (64bit)

シリアル通信を受信する実装をしてみた。

code

参考 http://yoppa.org/tau_bmaw13/4790.html
関連 https://processing.org/reference/libraries/serial/Serial.html

sketch_160713serialComm
import processing.serial.*;

Serial myPort;

void setup()
{
  size(200,200);
  frameRate(60);
  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600); // COM1
  myPort.bufferUntil('\n');
}

void draw()
{
    
}

void serialEvent(Serial myPort) { 
   String mystr = myPort.readStringUntil('\n');
   mystr = trim(mystr);
   println(mystr);
}

実行環境

  • シリアル受信側: processingを動かすPC (Windows 8.1pro)
  • シリアル送信側: TeraTermで送信する (Windows 7 pro)

上記の2つの端末をシリアルのケーブル(クロス)で接続する。
こちらではVMイメージ2つにてnamed pipeを使っての動作をした。

送信側のTeraTermでは「設定 > 端末」にて[CR]を[CR+LF]に変更する。LFを終端として認識するため。

実行

  1. 上記のスケッチを動かす
  2. 送信側のTeraTermで任意の文字(例:aaa)を入力して[Enter]を押す

processingのコンソールに以下のように表示される。

COM1 COM2 COM3 COM4
aaa

echo server

echo serverとする場合は、write()を使う。
https://processing.org/reference/libraries/serial/Serial_write_.html

Syntax serial.write(src)
src String: data to write

import processing.serial.*;

Serial myPort;

void setup()
{
  size(200,200);
  frameRate(60);
  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600); // COM1
  myPort.bufferUntil('\n');
}

void draw()
{
    
}

void serialEvent(Serial myPort) { 
   String mystr = myPort.readStringUntil('\n');
   mystr = trim(mystr);

   // echo back
   String ret = mystr + "\r\n";
   myPort.write(ret);

   println(mystr);
}
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?