0

我在 c# 中有这段代码,它运行良好,将正确的数字发送到继电器以打开/关闭继电器。我正在尝试使用 Arduino 程序和微控制器来控制继电器,并且正在努力从继电器中获得任何响应。我正在检查的选项之一是查看该程序以什么格式发送字节(二进制、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");
      }
}
4

2 回答 2

0

二进制只是表示数字的一种方式。ASCII 字符和字符代码将具有整数值作为字节。

在您的情况下没有“格式”。您正在直接设置各个字节。也许问题是为什么您的其他程序没有产生相同的结果?

于 2012-07-16T15:23:58.123 回答
0

一个字节可以保存从 0 到 255 的无符号值。除了解释它的含义之外,它没有与之关联的“格式” 。

于 2012-07-16T15:25:12.990 回答