阿杜诺 + C#。我尝试将字符串转换为 int,但出现有关 DateTime 类型的错误

Arduino + C# . I try to convert a string to int and I have an error about DateTime type

也许我不太会说出发生了什么,但我会尽力而为(抱歉我的英语不好)。

Arduino 代码:

 //Setup message bytes
    byte inputByte_0;
    byte inputByte_1;
    byte inputByte_2;


int pin = 9;

void setup() 
{
  Serial.begin(9600);
}

void loop() {

  if (Serial.available()> 2) 
  {
    inputByte_0 = Serial.read();delay(10);    
    inputByte_1 = Serial.read();delay(10);      
    inputByte_2 = Serial.read();delay(10);   
  }
  //Check for start of Message
  if(inputByte_0 == 4)
  {       
       //Detect Command type
       switch (inputByte_1) 
       {
         case 8://Set Port Identification Number, this case is only to find the correct port in order to connect.
             switch (inputByte_2)
            {
              case 16:
                Serial.print("I'M ARDUINO");
                break;
              case 32: 
                  Serial.print(digitalRead(x)); // Here I send via serial the value of x pin. 1/0 and receive in C# as a String. The "receiving" is handled by a class named "communicator" which I show it below.
                  break;

            }
        } 
        //Clear Message bytes
        inputByte_0 = 0;
        inputByte_1 = 0;
        inputByte_2 = 0;
  }
}

所以这是从 Arduino 获取消息的 class "communicator"。

namespace testtemp
{
    public class communicator
    {
        public string port = "";
        static SerialPort currentPort;
        public Boolean connect(int baud, string recognizeText, byte paramone, byte paramtwo, byte paramthree)
        {
            try
            {

                byte[] buffer = new byte[3];
                buffer[0] = Convert.ToByte(paramone);
                buffer[1] = Convert.ToByte(paramtwo);
                buffer[2] = Convert.ToByte(paramthree);

                int intReturnASCII = 0;
                char charReturnValue = (Char)intReturnASCII;
                string[] ports = SerialPort.GetPortNames();
                foreach (string newport in ports)
                {
                    currentPort = new SerialPort(newport, baud);
                    currentPort.Open();
                    currentPort.Write(buffer, 0, 3);
                    Thread.Sleep(200);
                    int count = currentPort.BytesToRead;
                    string returnMessage = "";   // Here I see that the message is a STRING so I wonder what is wrong.
                    while (count > 0)
                    {
                        intReturnASCII = currentPort.ReadByte();
                        returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
                        count--;
                    }

                    currentPort.Close();
                    port = newport;
                    if (returnMessage.Contains(recognizeText))
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }


        public string message(byte paramone, byte paramtwo, byte paramthree)
        {
            try
            {
                byte[] buffer = new byte[3];
                buffer[0] = Convert.ToByte(paramone);
                buffer[1] = Convert.ToByte(paramtwo);
                buffer[2] = Convert.ToByte(paramthree);
                currentPort.Open();
                currentPort.Write(buffer, 0, 3);
                int intReturnASCII = 0;
                char charReturnValue = (Char)intReturnASCII;
                Thread.Sleep(200);
                int count = currentPort.BytesToRead;
                string returnMessage = "";
                while (count > 0)
                {
                    intReturnASCII = currentPort.ReadByte();
                    returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
                    count--;
                }
                currentPort.Close();
                return returnMessage;
            }
            catch (Exception e)
            {
                return "Error";
            }}}}

现在我有了主要 class,我想在其中使用来自 Arduino 的值,来自 x 引脚的值。该值由函数调用提供:comport.message(4,8,32);

这是代码:

namespace testtemp
{
    public partial class tempreaderform : Form
    {
        public tempreaderform()
        {
            InitializeComponent();
        }

        communicator comport = new communicator();
        Boolean portConnection = false;
       Int32 red_light1;

        private void button1_Click(object sender, EventArgs e)
        { 
            if (comport.connect(9600, "I'M ARDUINO", 4, 8, 16))
            {
                label1.Text = "Connection Successful - Connected to  "+comport.port;
                portConnection = true;
                tempreader.Start();
            }
            else
            {
                label1.Text = "Not connected . . . ";
                portConnection = false;
                tempreader.Stop();
            }
        }

        private void tempreader_Tick(object sender, EventArgs e)// Here I have a timer that is called every 100ms to read that 
        {
            red_light1 = Convert.ToInt32(comport.message(4, 8, 32));// Here I get this error:"when converting a string to datetime parse the string to take the date before putting each variable"
I have nothing to do with datetime type. I don't understand why I get this error. 

            label3.Text = comport.message(4, 8, 32); When I put that value in a label everything works fine. I see that value oscilating from 0 to 1 and viceversa.
            // Here I have a timer that is called every 100ms to read that value.
        }

        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics rectangle1 = groupBox1.CreateGraphics();
            Graphics ellipse1 = groupBox1.CreateGraphics();

            Brush color = new SolidBrush(Color.DimGray);

            Brush red_on = new SolidBrush(Color.Red);
            Brush red_off = new SolidBrush(Color.DarkRed);

            Pen pen = new Pen(Color.Black, 3);


            if (label3.Text == "1")
            {
                ellipse1.FillEllipse(red_on, 123, 78, 24, 24);
            }
            else ellipse1.FillEllipse(red_off, 123, 78, 24, 24);
             // Here I want to color an ellipse depending on the value of red_light1 and doesn't work because I never see "red_light1" as true.
}}}

很抱歉问了这么长的问题,这可能不是解释得最好的问题。如果有人不明白我的 question/code.

中的内容,我会尽快回复

我开始怀疑错误出在这里:

returnMessage = returnMessage + Convert.ToChar(intReturnASCII);

这里为什么要转为char?如果 intReturnAscII 不是您所期望的,那可能会产生副作用。此外,您的 returnMessage 是一个字符串,因此将其视为一个字符串。使用

returnMessage = returnMessage + intReturnASCII.ToString();

相反。 还有这个:

int intReturnASCII = 0;
char charReturnValue = (Char)intReturnASCII;

完全没有必要,因为您从不对 charReturnValue 做任何事情。您只需分配 NULL,然后让它逗留。

red_light1 = Convert.ToInt32(comport.message(4, 8, 32));

方法 comport.message() returns 一个字符串,然后将其转换为一个整数。这适用于字符串例如“123”,但您的 Arduino 草图正在传递二进制数据(参见 Convert.ToChar(intReturnASCII))。字节码 48 到 57 映射到十进制数 0 到 9 并且可以转换。然而,其他字节码无法转换为整数。

换句话说,Convert.ToInt32("123")有效,但Convert.ToInt32("abc")无效。

如果您正在寻找一种从 Windows 设备与 Arduino 板通信的简单方法,我建议您看看 SolidSoils4Arduino。它是一个支持串行、Firmata 和 I2C 通信的库。它还提供了自动查找串行端口连接的方法,并且它也支持 Mono。