开发者

Trouble getting a serial connection communicating between C# and C

I have a microprocessor running C code and a computer running C# code. I am trying to get them to communicate through a serial connection (System.IO.Ports.SerialPort). I have no problems connecting, rather problems communicating.

In C# I have code like this to send the string "rr\n":

mySeiralPort.WriteLine("rr");

In C (on the microcontroller), I have code such as this to receive the string:

gets(str);

Note that the STDIN stream is the serialport stream. Here's where things get interesting. As a test, we have a bit of code that causes an LED to开发者_如何学编程 light up if str is equal to "rr". This allows us to observe whether or not the microcontroller has received it as expected.

When using HyperTerminal, I can type "r", then type "r", then press enter -> and the LED lights up, signaling it has indeed read "rr" from the serial stream. However, when I use the above WriteLine function from C#, we get no such result.

I believe this boils down to the functionality of HyperTerminal sending character-by-character, and that the gets() function reads character-by-character until it encounters a newline.

Can anyone shed some light on how to get C#'s WriteLine() and C's gets() functions to work together?


The classic mistake is forgetting to turn the handshake signals on, either by enabling hardware handshake or by setting the SerialPort.DtrEnable and RtsEnable properties to true. Serial port devices should ignore anything until they detect the other end powered up and ready to receive.

Less likely is that the microcontroller isn't responding fast enough to received bytes. Either because it is slow to respond to the interrupt or is using polling. That can cause a receiver overrun error with loss of the received byte. Lowering the baudrate would fix that.


HyperTerminal may be (probably is) using a different newline character from C#. Try writing "rr\r" or "rr\n" instead of using WriteLine.


Have you considered using an oscilloscope or logic analyzer to help determine the problem? If you work with embedded systems, these tools are essential. Using these tools can tell you what the WriteLine method is doing or not doing compared to HT.


This is probably a buffering problem but it could also be related to what the microprocessor expects as an end of line terminator. Is HT sending newline only or carriage return followed by newline?

First of all, determine what the microprocessor expects for end of line. Then use Write instead of WriteLine and write the individual bytes, in ASCII. Remember that .NET likes to use UNICODE but a microprocessor probably expects ASCII.

Maybe you sent Unicode bytes or maybe you sent a carriage return character that was not expected and was concatenated to the RR.

Note that this class has an attribute called NewLine. You may be able to get WriteLine working by setting it correctly. There is another attribute called encoding. Do you have it set to ASCII?


C# might be sending 2 bytes for each r (eg UTF16), whereas Hyperterminal is probably just sending 1 byte for each r (eg ASCII).

Might want to look at strings in c# for information on how they are encoded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜