c# serial read doesn't do function on certain serial input - c#

I have a program that reads serial data from an arduino through a com port.
It works successfully when i program it to print the serial data onto a console, and it does so.
For example, the arduino says outputs "21", and the console writes "21".
But if I make an if statement saying If(data == "21"){/*dosomething*/}
it doesnt work even if the data is printed as "21".
Here are the parts of my code related to this issue.
Arduino
if(digitalRead(i) == HIGH && stat[i-2] == false){
Serial.print(i); Serial.println(1);
stat[i-2] = true;
}
The arduino code is kind of difficult to understand without the rest of the code, but all it does is scans all the ports to see if one of them is high. If it is, it prints the port number and a 1 next to it. For example if port 2 is high, it outputs "21". The same happens when port 2 is low, it outputs "20".
C# (an event handler)
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serialPort1 = sender as SerialPort;
string dataString = (serialPort1).ReadLine();
screen.clearline();
Console.Write(dataString);
if (dataString == "connectAlready")
{
connect.con();
}
if (dataString == "20")
{
Console.WriteLine("pdown");
}
if (dataString == "21")
{
Console.WriteLine("pup");
}
}
This is the event handler called when data is received.
It prints data to the screen such as "21","20", but it does not perform the functions in the If statements.
Any help would be appreciated, thank you very much.

My guess is that the strings you are comparing aren't completely equal. Arduino's documentation for its println() function states that the serial print ends in a carriage return character ('\r'), followed by the newline character ('\n'). The SerialPort.ReadLine() function reads and returns the string "up to the NewLine value in the input buffer" (found here), so the '\r' character is probably still in dataString.
If this is your problem, you can solve it by removing the last character with something like:
dataString = dataString.TrimEnd('\r');

Related

My textbox displaying data from serial port is updating too slow

I'm making a C# Win forms app that reads data from an arduino over serial port, parses it and displays it in a textbox.
My problem is that the value in the textbox is always a few seconds older than the value being sent by arduino even if I slow down the arduino to send the data once per second.
I know the problem must be in my C# code because when I read the serial port using a serial monitor everything's fine.
my code:
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
string msgType;
string serialMsg;
serialMsg = port.ReadLine();
if(serialMsg.Substring(0, 1) != "#")
{
return;
}
msgType = serialMsg.Substring(1, 4);
if(msgType == "VOLT") // recieve supply voltage reading
{
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = serialMsg.Substring(5, serialMsg.Length - 5); });
}
if (msgType == "AMPS") // recieve supply current reading
{
textBox2.Invoke((MethodInvoker)delegate { textBox2.Text = serialMsg.Substring(5, serialMsg.Length - 5); });
}
if (msgType == "LOAD") // recieve load current reading
{
textBox3.Invoke((MethodInvoker)delegate { textBox3.Text = serialMsg.Substring(5, serialMsg.Length - 5); });
}
}
The messages coming from the arduino are in this format: # + TYPE + DATA
Can you tell me what's slowing it down?
I was sending the messages from the arduino too quickly so I put a 50ms delay between them
I also used ReadExisting instead of ReadLine
This solved all the lagging

C# streaming large file through serialPort to an Arduino UNO

I´d like to stream a text file containing G-Code to an Arduino UNO via the serialPort.
The Arduino receives all bytes with the SerialEvent and adds it to a char array named buffer. If the buffer is full it is supposed to send an "!;" over the serial port to C#.
This works fine as I have tested it with the Serial Montior application of the Arduino IDE. But I can´t type text as fast as C# can send it :)
The C# program reads the G-Code file linewise and then sends each char in a line to the arduino. After each char I want to check if the Arduino tells me if the buffer is full. Otherwise keep streaming.
Somehow c# never notices the "!;" or even gets any Bytes to read from the Arduino while streaming. I have the feeling that the serialPort.Write() function blocks the port.
This is the Arduino Code:
void serialEvent()
{
// wenn im Puffer noch platz ist
if (buffercount < MAX_BUF)
{
char c = (char)Serial.read();
buffer[buffercount++] = c;
}
else
{
Serial.print("!;");
}
}
The serialEvent is fired every time the Arduino receives bytes on the port.
Max_BUF has a value of 64.
This is the C# Code:
private void startStreaming(string Text)
{
string[] stringSeparators;
string Text2Stream;
if (Text == "")
{
Text2Stream = File.ReadAllText(textBoxSelectFile.Text);
stringSeparators = new string[] { "\n" };
}
else
{
stringSeparators = new string[] { "\r\n" };
Text2Stream = Text;
}
string[] t2s = Text2Stream.Split(stringSeparators, StringSplitOptions.None);
foreach (string zeile in t2s)
{
if (zeile.Contains(")") || zeile.Contains("("))
{
continue;
}
// Code schicken
foreach (char c in zeile)
{
if (c == ' ') continue;
serialPort.Write(c.ToString());
if (serialPort.BytesToRead > 0)
{
if(serialPort.ReadExisting() == "!;")
{
**wait and do smth.**
}
}
}
serialPort.Write(";");
addTextToLog(zeile);
}
}
serialPort.ReadExisiting() never happens because there are never BytesToRead.
The ";" is for both the sign for the end of a line.
startStreaming is started in an asynchronous thread as BackGroundWorker.
Somehow c# never notices the "!;" or even gets any Bytes to read from the Arduino while streaming. I have the feeling that the serialPort.Write() function blocks the port.
The Write command is not blocking the port. The arduino is just much slower then your computer. So between these two lines:
serialPort.Write(c.ToString());
if (serialPort.BytesToRead > 0)
the arduino is occupied and no data is received yet. Therefore BytesToRead is 0. Two possibilities to solve that come right away to my mind.
1) Use the serialPort.DataReceived event for asynchronous processing. This will be triggered every time you receive data. Then you can react upon the received data in that event or
2) Just give the arduino some time with System.Threading.Thread.Sleep(1000);
serialPort.Write(c.ToString());
System.Threading.Thread.Sleep(1000);
if (serialPort.BytesToRead > 0)
You need to find out which is the minimum timespan to wait.

Cannot convert TCL serial com to C#

I'm trying to convert TCL code, used to communicate with a serial port to a "robot", to C#. But for some reason my commands are not getting responses.
This is the serial com init in TCL:
proc openUart {} {
set robot::fd_ [open COM${robot::port_num}: w+]
fconfigure $robot::fd_ -mode 9600,e,7,1
fconfigure $robot::fd_ -blocking 0
fconfigure $robot::fd_ -buffering none
fileevent $robot::fd_ readable ""
}
A "command" is sent like this:
proc SendCmd {command} {
set commandlen [string length $command]
for {set i 0} {$i < $commandlen} {incr i} {
set letter [string index $command $i]
after 10
puts -nonewline $robot::fd_ $letter
}
after [expr 2 * 10]
puts -nonewline $robot::fd_ "\n"
flush $robot::fd_
}
This is how I translated this to C#. Opening the port:
private void Initialize(string com)
{
_comPort = new SerialPort(com,9600,Parity.Even,7,StopBits.One)
{
Encoding = Encoding.ASCII,
NewLine = "\n"
};
_comPort.Open();
}
And sending a command:
private string SendCommand(Commands cmd)
{
string commandToWrite = Command(cmd);
for (int i = 0; i < CommandLen; i++)
{
Thread.Sleep(10);
_comPort.Write(commandToWrite.ToCharArray(), i, 1);
}
Thread.Sleep(10 * 2);
_comPort.Write("\n");
_comPort.BaseStream.Flush();
}
I connected my PC to the robot with a serial to USB cable and ran both TCL and C# programs -
The TCL script turns on a LED on the robot.
My C# code doesn't turn the LED on, meaning the robot did not recognize the command.
I'm using the same com port, so I believe the problem is one of these:
I did not initialize the com port correctly in C#. How do you set the blocking and buffering?
Could there be an encoding issue in C#? isn't ASCII the default encoding in TCL?
Could there be a timing difference in how I'm sending the command letter-by-letter between the two languages?
Issue resolved!
I finally looped back the cable into my PC using another serial cable and 2 blue wires, crossing the RX\TX (thanks don_q for the idea!).
Using a simple serial monitor, "UART Terminal", I sniffed the commands, and to my surprise the TCL script was adding a '\r' before the '\n'!
So in fact the robot was expecting this command format -
:010508010000F1\r\n
I changed the NewLine property in C# to be "\r\n", and now I finish a command by using -
_comPort.WriteLine("");
And now everything works.

C# passing message through serialport

I am trying to run a treadmill using the serial port, I was able to do it using matlab however I am having a few probelms when I ported the same code to C#. I am sure that the port is open there is probably something wrong with the message format. Would be great if someone can tell me what mistake I am making.The matlab code (which works) and the C# code (which doesn't work) are given below.
MATLAB CODE :
ctr = char(12); %control character
rel = char(169); %release
set_speed = char(163);
set_dir = char (184);
%initializing the ports
R = serial('COM4');
set(R, 'BaudRate', 4800, 'Parity', 'none', 'DataBits', 8, 'StopBits', 1, 'Terminator', 'CR');
set(R, 'InputBufferSize', 128, 'OutputBufferSize', 128);
fopen(R);
if R.status == 'open'
fprintf(R, [rel ctr]);
disp('port for R belt open and released');
else
disp('error with R port-- COM3');
end;
%initial direction to FORWARD
fprintf(R, [set_dir '0' char(12)]);
%set speed to
fprintf(R, [set_speed '0005' ctr]);
My C# version of the matlab code above
char ctr = (char)12;
char rel = (char)169; //release
char set_speed = (char)163;
char set_dir = (char)184;
void Start () {
try{
SerialPort R = new SerialPort();
R.BaudRate = 4800;
R.Parity = Parity.None;
R.DataBits = 8;
R.StopBits = StopBits.One;
R.ReadBufferSize = 128;
R.WriteBufferSize = 128;
R.Open();
if(R.IsOpen){
//Release
R.Write(rel+""+ctr);
print ("Serial port is open");
}
else print ("Serial port is close");
R.Write(set_dir+""+"0"+""+ctr);
R.Write(set_speed+""+"0005"+""+ctr);
}
catch(UnityException e){
print ("Exception");
print (e);
}
}
I'm not really familiar with C#, but I'll try to guess that you should also send terminator character in your C# code.
Check the fprintf (serial) documentation in MatLab:
fprintf(obj,'cmd') writes the string cmd to the device connected to the serial port object, obj. The default format is %s\n. The write operation is synchronous and blocks the command-line until execution completes.
fprintf(obj,'format','cmd') writes the string using the format specified by format.
In your calls you are using 1st syntax so your call
fprintf(R, [rel ctr]);
is actually
fprintf(R, '%s\n', [rel ctr]);
Usually, serial devices read input data until the terminator character is found. This means that transmission of the command string or data is completed and the device now can execute the command. This is much like hitting ENTER in MatLab command window: command is executed only after you do this.
Which terminator character to use should be specified in your device programming manual.
Seems that CR is OK since your MatLab code works.
In your MatLAb code you set the terminator to be CR character (ASCII code 13). I do not see this in your C# code so your device waits for CR which is not sent so there should be no reaction from your device.
I do not think that C# will send the terminator character for you, you should take care of it by yourself.
My guess is that
R.Write(rel+""+ctr + "\r");
should solve the problem (thanks dodald for reminding me that I missed the proper conclusion).
See also Terminator property of SERIAL object and Rules for Writing the Terminator.

Encoding.ASCII.GetString returning blank value (Only in windows Application)

I'm trying to create a windows application with Visual Studio 2012 but weird stuff seems to be happening... When I run the exact same code in a console app, it works fine but it seems that I can't output the following once I run it in a thread on a windows application project:
private void VisualUDPListener_Load(object sender, EventArgs e)
{
//System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
new Thread(delegate()
{
StartListener();
}).Start();
}
private void StartListener()
{
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (true)
{
//log text box
Log.AppendText("Listening \n");
byte[] bytes = listener.Receive(ref groupEP);
string hex_string = BitConverter.ToString(bytes);//this works and returns the correct hex data
string ascii_string = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//blank???????????
MessageBox.Show(ascii_string.Length.toString());//outputs 131 which is correct
MessageBox.Show(ascii_string);// shows a blank message box
Log.AppendText("--" + ascii_string + hex_string +" \n");//only outputs --
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
I target .NET Framework 4.5... If I send UDP data from a test java app it receives the data fine but if I send the exact same data through a mobile device which the code is intended for it comes out empty like in the comments above. (Then the device must be sending corrupt data? nope because if the code above is run in a console app it runs perfectly and outputs the correct strings)
Any help would be greatly appreciated.
As noted in comments, the string starts with 0 byte (00-04-02-00-20).
This correctly gets converted to C# string. MessageBox.Show invokes windows api function MessageBox. Windows API uses zero-terminated strings, so this specific string appears empty to WinAPI, because first byte is zero. You cannot log/display this string verbatim with APIs that use zero-terminated strings.
You need either to replace 0s with something else, like ascii_string.Replace((char)0, (char)1) or use apis which don't treat zeros as special characters.

Categories

Resources