如何通过usb从c#向arduino(c)发送数据。 [英] How to send data to arduino (in c) through usb from c#.

查看:262
本文介绍了如何通过usb从c#向arduino(c)发送数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个arduino uno board(下次不会犯同样的错误)运行一个c程序,在那里我有一些变量需要改变,这取决于我用c#编写的主程序中发生的事情。通信是一种方式,因为响应来自其他地方,我只想发送开始+地址+数据然后停止包。类似



开始1 01 1然后停止0.(也许0 00 0)



问题是我是否值得尝试了解我的USB驱动程序如何通信,或者是否可以以其他方式发送数据?这是我的uni项目的最后一部分,所以它很紧急,我正在寻找最简单的解决方案(usb听起来不像其他问题:S ..但是当我想呈现它时看起来很好......)



对于我来说,棘手的部分是为任何通信类型的arduino编写c(与广告中不同的是,大多数示例都是使用它自己的语言编写的)和为usb写c#。 (我写了一个有效的串行发送器,认为我会切断电缆并以这种方式发送数据......但我不相信即使在300波特率下它也能正常工作......)



或者我的最终想法是将内容保存到txt文档中并使用GoBetwino从中读取...这并不理想,因为我的应用程序确实需要尽可能快,加上我只能想象arduino需要轮询文档中的更改...再次不理想....所以如果有人知道如何从一个(表单)应用程序发送4个字节到我的arduino ......请大喊!

So I have an arduino uno board (no won't make the same mistake next time) running a c program, in there I have some variables that needs changing depending on what's happening in my main program that's written in c#. The communication is one way, as the response comes from somewhere else, I'd just like to send a "start + address + data" then "stop" packages. something like

to start 1 01 1 then to stop 0. (maybe 0 00 0)

The question is does it worth for me to try and understand how my usb drivers will communicate, or would it be possible to send the data in another way? It's the last part of my uni project so it's kind of urgent, im looking for teh simplest solution possible (usb didn't sound like it from others questions :S.. but it looks good when i want to present it...)

For me the tricky part is both writing the c for the arduino with any communication type ( as unlike it was advertised really most examples are written using it's own language) and writing c# for usb. ( I wrote a serial sender that works, thinking that I'll chop up a cable and send data that way... but i'm not convinced that that could work even at 300 baud rate...)

or my final idea was to save stuff into a txt document and use GoBetwino to read from it... that's not ideal as my application would really need to be as fast as possible, plus i can only image that the arduino would need to poll for changes in the doc... again not ideal.... so if anyone knows slightly more about how to send 4 bytes from a (form) application to my arduino ... please shout!

推荐答案

最好的方法是通过串行连接,因为所有Arduinos都有串行接口。您可以通过COM端口获得USB转串口转换器并与电路板通信。这已经在网络上记录了数千次。并且,是的,它几乎可以在任何速度下工作。
The best way to do it is over a serial connection as all Arduinos have serial interfaces. You can get a USB-to-Serial converter and talk with the board over a COM port. That's been documented thousands of times all over the web. And, yes, it works over just about any speed.


我假设你使用的是Windows。



插入你的Arduino使用USB线连接到PC。打开设备管理器。寻找端口(COM和LPT)。在那里你会发现你的Arduino被识别出来了。我的是COM3。看看端口设置​​我的是9600,8,无,1,无。



现在使用此代码:



http://playground.arduino.cc/Csharp/SerialCommsCSharp [ ^ ]



只要你的端口设置为9600这将起作用 - 如果不修改此行的波特率C#代码:



I assume you are using Windows.

Plug in your Arduino to PC using your usb cable. Open Device Manager. Look for ports(COM & LPT ). There you will find your Arduino identified. Mine is COM3. Look at "Port Settings" mine are 9600,8,none,1,none.

Now use this code:

http://playground.arduino.cc/Csharp/SerialCommsCSharp[^]

As long as your port is set to 9600 this will work - if not modify the C# code to your baud rate at this line:

currentPort = new SerialPort(port, 9600);


这篇关于如何通过usb从c#向arduino(c)发送数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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