如何接收和使用从蓝牙到C#的整数? [英] How to receive and using integers from Bluetooth to C#?

查看:90
本文介绍了如何接收和使用从蓝牙到C#的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Arduino将数字发送到我的笔记本电脑,而视觉工作室则使用数字并根据执行某项任务所需的数字。问题是我不断收到一条消息,说它无法将int类型转换为System.IO.Ports.SerialPorts。
如何围绕这个进行操作?

使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;
使用System.Diagnostics;使用System.Drawing
;
使用System.IO.Ports;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;

namespace bluetooth_test
{
public partial class Form1:Form
{
int count = 0;
public Form1()
{
InitializeComponent();
serialPort1.PortName =" COM5" ;;
serialPort1.BaudRate = 9600;

尝试
{
if(!serialPort1.IsOpen)
serialPort1.Open();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
serialPort1.DataReceived + = new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
this.Width = 800;
this.Height = 600;
}

private void DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
// throw new NotImplementedException();
try
{

SerialPort spl =(SerialPort)sender;
Console.Write(" Data" + count +"" + spl.ReadLine()+" \ n");
count ++;
if(spl = 1){}
}
catch(Exception ex)
{

}
}


}

}




Deon Hargrove

解决方案

嗨Deon,


问题是由于以下代码:

尝试
{
SerialPort spl =(SerialPort)sender;
Console.Write(" Data" + count +"" + spl.ReadLine()+" \ n");
count ++;
if(spl = 1)//由于此代码
{}
}


 

'sql'是 SerialPort 类型的一个实例,1是一个数字,所以你不能在等号的两边比较它们,请检查它。


你想在这做什么?


问候,


Frankie


I am using Arduino to send numbers to my laptop, and visual studios to take the numbers and depending on what number it is to perform a certain task. The problem is I keep getting a message saying it cannot convert type int to System.IO.Ports.SerialPorts. How can I maneuver around this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bluetooth_test
{
    public partial class Form1 : Form
    {
        int count = 0;
        public Form1()
        {
            InitializeComponent();
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 9600;
            
            try
            {
                if (!serialPort1.IsOpen)
                    serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceived);
            this.Width = 800;
            this.Height = 600;
        }

        private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // throw new NotImplementedException();
            try
            {
               
                SerialPort spl = (SerialPort)sender;
                Console.Write("Data" + count + " " + spl.ReadLine() + "\n");
                count++;
                if (spl = 1) { }
            }
            catch (Exception ex)
            {

            }
        }

        
    }
    
}


Deon Hargrove

解决方案

Hi Deon,

The problem is due to the following code:

    try
    {
        SerialPort spl = (SerialPort)sender;
        Console.Write("Data" + count + " " + spl.ReadLine() + "\n");
        count++;
        if (spl = 1)//due to this code
        { }
    }

 

The 'sql' is an instance of the SerialPort type, and 1 is a number, so you can't compare them on both sides of the equal sign, please check it.

What do you want to do here?

Regards,

Frankie


这篇关于如何接收和使用从蓝牙到C#的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆