我已经搜索了几个帖子来尝试解决我的问题,但我无法做到。
因此,我的arduino通过串行发送字节流,其中包括:
字节 1, 字节 2, 字节 3, 字节4, 字节 5
字节 1 是"\r"字符字节2到5是传感器的值 = 字节2字节3字节4字节5
这是arduino代码:
byte buf[4];
buf[0] = SPI_RX_Buff[1];
buf[1] = SPI_RX_Buff[2];
buf[2] = SPI_RX_Buff[3];
buf[3] = SPI_RX_Buff[4];
Serial.write("r");
Serial.write(buf, sizeof(buf));
在python中,我读取每一行,当收到行"\r"时,我可以告诉它另一个值。这是python代码:
while (ser.inWaiting()==0): #hold until data is available
pass #do nothing
ret_char = ser.read(1)
print(ret_char)
蟒蛇输出:
b'r'
b'xff'
b'xfa'
b'x87'
b'xf1'
b'r'
b'xff'
b'xfb'
b'^'
b'Q'
b'r'
b'xff'
b'xff'
b'x92'
b'x06'
使用 C#,我无法阅读和使用它。这是我尝试使用的 C# 简单代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 2000000;
myport.PortName = "COM15";
myport.Open();
while (true)
{
Console.WriteLine(myport.ReadLine());
}
}
}
}
C# 输出:
#? ?
?n %
?B ?
?2
??
^?
6`
V?
??
?? ?
?
`
??
Q
???3
????
?? ?
? ?
???
我更改了 Console.WriteLine(myport.ReadLine((( for Console.WriteLine((byte(myport.ReadByte(((,这是新的输出:
0
1
134
50
13
0
1
98
210
13
0
1
71
243
13
0
有没有人知道我做错了什么,我怎样才能得到我的传感器的值:byte2byte3byte4byte5
谢谢。
像这样的事情怎么样:
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 2000000;
myport.PortName = "COM15";
myport.Open();
List<byte> bytes = new List<byte>();
while (true)
{
bytes.Clear();
while (true)
{
if (myport.BytesToRead == 0) continue;
int b = myport.ReadByte();
// This should never really happen because we checked
// BytesToRead above... but just in case
if (b < 0) continue; // -1 if end of stream
bytes.Add((byte)b);
if (b == 13) break; // 'r' in your data
}
// Process values any way you want here...
// At this point your values are stored in bytes[0...n]
List<String> values = new List<string>();
for (int i = 0; i < bytes.Count; i++)
{
values.Add(bytes[i].ToString());
}
Console.WriteLine($"Values: {String.Join(",", values)}");
}
}
我正在发布我的最终代码的样子,因为也许有人需要类似的东西。我从32位ADC获取数据,以2.5V为参考,并打印其值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 2000000;
myport.PortName = "COM15";
myport.Open();
List<byte> bytes = new List<byte>();
List<byte> forca = new List<byte>();
while (true)
{
bytes.Clear();
while (true)
{
if (myport.BytesToRead == 0) continue;
int b = myport.ReadByte();
if (b < 0) continue; // -1 if end of stream
if (b == 13) break; // 'r' in your data
bytes.Add((byte)b);
}
if(bytes.Count == 4)
{
byte[] UnsignedByteArray = bytes.ToArray();
sbyte[] SignedByteArray = Array.ConvertAll(UnsignedByteArray, b => unchecked((sbyte)b));
if (BitConverter.IsLittleEndian)
Array.Reverse(UnsignedByteArray);
Console.WriteLine(" "+BitConverter.ToInt32(UnsignedByteArray, 0) * (2.5 / (Math.Pow(2, 31))) * 1000);
}
}
}
}
}