私はこのコードをc#に持っていますが、それは完全に機能し、正しい番号をリレーに送信してリレーをオン/オフします。リレーを制御するためにArduinoプログラムとマイクロコントローラーを使用しようとしていますが、リレーからの応答を得るのに苦労しています。私がチェックしているオプションの 1 つは、このプログラムがバイトを送信している形式 (バイナリ、ASCII 文字コード、ASCII 文字など) を確認して、他のプログラムでこれを模倣しようとすることです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
LightRelayBoard lrb = new LightRelayBoard("COM1");
lrb.WorkspaceLight(4);
Thread.Sleep(100);
lrb.Query();
Thread.Sleep(5000);
lrb.WorkspaceLight(0);
Thread.Sleep(100);
lrb.Query();
Console.Read();
}
}
public class LightRelayBoard
{
SerialPort sp;
byte[] relay1Off = new byte[2];
byte[] relay2Off = new byte[2];
byte[] relay3Off = new byte[2];
byte[] relay4Off = new byte[2];
byte[] relay1On = new byte[2];
byte[] relay2On = new byte[2];
byte[] relay3On = new byte[2];
byte[] relay4On = new byte[2];
byte[] relay1query = new byte[2];
int currentState = 0;
public LightRelayBoard(string portName) // constructor
{
relay1Off[0] = (byte)254;
relay1Off[1] = 0;
relay2Off[0] = (byte)254;
relay2Off[1] = 1;
relay3Off[0] = (byte)254;
relay3Off[1] = 2;
relay4Off[0] = (byte)254;
relay4Off[1] = 3;
relay1On[0] = (byte)254;
relay1On[1] = (byte)8;
relay2On[0] = (byte)254;
relay2On[1] = (byte)9;
relay3On[0] = (byte)254;
relay3On[1] = (byte)10;
relay4On[0] = (byte)254;
relay4On[1] = (byte)11;
relay1query[0] = (byte)254;
relay1query[1] = (byte)16;
sp = new SerialPort(portName, 9600);
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
try { sp.Open(); }
catch
{
Console.WriteLine("Couldn't open light relay board: port ({0})", portName);
}
for (int i = 0; i <= 4; i++)
{
WorkspaceLight(i);
Thread.Sleep(100);
}
for (int i = 3; i >= 0; i--)
{
WorkspaceLight(i);
Thread.Sleep(100);
}
}
public void WorkspaceLight(int command)
{
if (command > 4)
command = 4;
else if (command < 0)
command = 0;
if (command > currentState)
{
for (int i = (currentState + 1); i <= command; i++)
{
executeRelay(i, true); //turn on ith relay
//Console.WriteLine("{0} on", i);
}
currentState = command;
}
else if (command < currentState)
{
for (int i = currentState; i > command; i--)
{
executeRelay(i, false); //turn on ith relay// turn off ith relay
//Console.WriteLine("{0} off", i);
}
currentState = command;
}
return;
}
public void Query()
{
if (sp.IsOpen)
{
try
{
sp.Write(relay1query, 0, 2);
}
catch
{
}
}
}
private void executeRelay(int ch, bool dir)
{
byte[] send = relay1Off;
if (ch == 1 && dir == true)
send = relay1On;
else if (ch == 1 && dir == false)
send = relay1Off;
else if (ch == 2 && dir == true)
send = relay2On;
else if (ch == 2 && dir == false)
send = relay2Off;
else if (ch == 3 && dir == true)
send = relay3On;
else if (ch == 3 && dir == false)
send = relay3Off;
else if (ch == 4 && dir == true)
send = relay4On;
else if (ch == 4 && dir == false)
send = relay4Off;
if (sp.IsOpen)
{
try
{
sp.Write(send, 0, 2);
}
catch
{
}
}
return;
}
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp2 = (SerialPort)sender;
int numBytes = sp2.BytesToRead;
byte[] sp2buffer = new byte[numBytes];
sp2.Read(sp2buffer, 0, sp2.BytesToRead);
Console.WriteLine("DATA from sp: {0}", sp2buffer[0].ToString());
}
}
Arduinoコードは次のとおりです。
int ledPin = 13;
unsigned char a = B11111110;
unsigned char b = B00001000;
void setup()
{
pinMode(ledPin, OUTPUT);
// serial communication
Serial.begin(9600);
Serial2.begin(9600); //serial comms with relay board
}
void loop()
{
if(Serial.available()>0){ //available means it is receiving data
Serial2.print(a);
Serial.println(a); //see what Serial is printing on the screen
Serial2.print(b);
Serial.print(b);
}
if(Serial2.available()>0){ //check if serial 2 is recieving data
Serial.write("data");
}
else{
Serial.print("no data");
}
}