C# AVR字符和C字节不同
我编写了一个微型C应用程序,它将字节写入COM端口:C# AVR字符和C字节不同,c#,avr-gcc,C#,Avr Gcc,我编写了一个微型C应用程序,它将字节写入COM端口: SerialPort serialPort = new SerialPort { PortName = "COM6", BaudRate = 57600, DataBits = 8, Parity = Parity.None, StopBits = StopBits.One, Handshake = Handshake.None }; serialPort.DataReceived += de
SerialPort serialPort = new SerialPort
{
PortName = "COM6",
BaudRate = 57600,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One,
Handshake = Handshake.None
};
serialPort.DataReceived += delegate
{
var read = serialPort.ReadByte();
Console.WriteLine("Recv: {0}", read);
Trace.WriteLine(read);
};
serialPort.Open();
for (Byte i = 0; i < 250; i++)
{
Console.WriteLine("Send: {0}", i);
serialPort.Write(new[] {i}, 0, 1);
Thread.Sleep(250);
}
所以它上升到127,然后它突然是正确的
不知何故,字节值之间有128个差异,但我无法控制它
编辑:经过更多的测试后,我注意到:
Send: 0 (0)
Recv: 128 (10000000)
Send: 1 (1)
Recv: 129 (10000001)
Send: 2 (10)
Recv: 130 (10000010)
Send: 3 (11)
Recv: 131 (10000011)
Send: 4 (100)
Recv: 132 (10000100)
Send: 5 (101)
Recv: 133 (10000101)
我似乎认为补码与C和AVR C不同。我在项目中遇到了类似的问题,但我对C和VB是新手,这与ASCI和ASCII有关。你的AVR代码很好 我在VB中所做的: Dim字节_conv10作为字节 字节0=Pg comPort.Writebyte_conv,0,10 就你而言: Dim字节_conv1作为字节 字节0=i
comPort.Writebyte_conv,0,1解决方案是,我对串行端口使用了错误的速度,因此数据包无法正确到达。您似乎也没有收到126和127的任何回复,或者只是编辑问题?您确定,您的设置在两侧都正确吗?IE 8位/奇偶校验?@Damien_不相信你是对的,这些值没有得到回复。我在互联网上找到了另一台设备mega644的随机手册,但在这台设备上,你必须在UCSR0C中设置USBS0以获得1个停止位。如果它是零,它在找2。你的也一样吗?事实上我现在才这么做,但不幸的是没有改变。
Send: 0
Recv: 128
Send: 1
Recv: 129
Send: 2
Recv: 130
Send: 3
Recv: 131
Send: 4
Recv: 132
Send: 5
Recv: 133
Send: 6
Recv: 134
Send: 7
Recv: 135
Send: 8
Recv: 136
Send: 9
Recv: 137
Send: 10
Recv: 138
Send: 11
Recv: 139
Send: 12
Recv: 140
Send: 13
Recv: 141
Send: 14
...
Recv: 250
Send: 123
Recv: 251
Send: 124
Recv: 252
Send: 125
Recv: 253
Send: 126
Send: 127
Send: 128
Recv: 128
Send: 129
Recv: 129
Send: 130
Recv: 130
Send: 131
Recv: 131
Send: 132
Recv: 132
Send: 133
Recv: 133
Send: 134
Recv: 134
Send: 135
Recv: 135
Send: 136
Recv: 136
Send: 137
Recv: 137
Send: 138
Recv: 138
Send: 139
Recv: 139
Send: 140
Recv: 140
Send: 0 (0)
Recv: 128 (10000000)
Send: 1 (1)
Recv: 129 (10000001)
Send: 2 (10)
Recv: 130 (10000010)
Send: 3 (11)
Recv: 131 (10000011)
Send: 4 (100)
Recv: 132 (10000100)
Send: 5 (101)
Recv: 133 (10000101)