Last active
August 10, 2019 06:02
-
-
Save Tachibana446/fdc623fd9119bff9c1659acc32b1285c to your computer and use it in GitHub Desktop.
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
byte[] CheckCmd = new byte[] { 0xF0, 0x01, 0x00 }; | |
SerialPort Port = null; | |
foreach (var name in SerialPort.GetPortNames()) | |
{ | |
var p = new SerialPort(name, 19200, Parity.None, 8, StopBits.One); | |
p.Open(); | |
p.DtrEnable = true; | |
p.RtsEnable = true; | |
p.ReadTimeout = 100; | |
p.Write(CheckCmd, 0, CheckCmd.Length); | |
Int32 result = p.ReadByte(); | |
if (result == 2) | |
{ | |
portName = name; | |
Port = p; | |
break; | |
} | |
else | |
{ | |
p.Close(); | |
} | |
} |
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
// COMポートにボーレート19,200、8-N-1でデータを送信する。ポート名は適宜探すこと。 | |
var p = new SerialPort("COM5", 19200, Parity.None, 8, StopBits.One); | |
p.Open(); | |
p.DtrEnable = true; | |
p.RtsEnable = true; | |
p.ReadTimeout = 100; | |
// 回転の強さ 0~100(%) | |
int value = 100; | |
// 逆回転したければ0x80を足して最上位ビットを1にすればよい | |
// value = value | 0x80; | |
byte[] data = new byte[] { 0x02, 0x01, (byte)value }; | |
p.Write(data, 0, data.Length); | |
p.Close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment