[英]How to send serial data from Arduino to Visual Studio C# with zedgraph and vice versa simultaniously
[英]How to send a integer value to arduino from visual studio C#
我研究了一個有關使用Visual Studio C#控制機器人的項目。 我想控制一個與Track位置連接的arduino步進電機。 但是我不通過串行端口將跟蹤值作為整數發送給arduino。 我可以發送字符或字符串值。 我想將每個磁道值發送到arduino,以控制步進電機。
在C#端,您可以使用如下代碼:
Byte[] bytes = BitConverter.GetBytes(1234); //1234-sample 32 bit int
注意字節序,在此示例中,bytes [0]將是最低有效字節,因此最好從end開始發送此數組。
在Arduino方面,您可以逐字節獲取此數組,然后通過左移將其組裝回int,例如:
tmp_long|=getbyte(); //got first byte of int
tmp_long<<=8;
tmp_long|=getbyte(); //got second byte of int
tmp_long<<=8;
tmp_long|=getbyte(); //
tmp_long<<=8;
tmp_long|=getbyte(); //
//請記住,int在C#中為32位,而在Arduino Uno中為16位,因此您需要在此處輸入長類型。
或者,您可以輸入typedef union,然后逐字節填充它,如下所示:
typedef union _WORD_VAL
{
unsigned long Val;
unsigned char v[4];
} WORD_VAL;
WORD_VAL myData;
myData.v[0]=getbyte(); //got first byte of int
myData.v[1]=getbyte();
myData.v[2]=getbyte();
myData.v[3]=getbyte();
unsigned long data=myData.Val; //got assembled in back
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.