Serial port string recognision - c#

I'm having incoming data recieved over a serial port, with a serial port reader that sits there and reads all data. I want the following to happen:
When ENTER ("\r") is being received over the serial port, I want the line of data before the ENTER captured in a string, and its first letter compared with the first letter that comes after the ENTER. If they match, continue reading the serial, if they don't, then display the line of text.
Here's my solution in C#. It doesn't work and I suspect some logic errors:
public void SerialReceived_Uson()
{
// create a temporary storage string
string sTemp = "";
// add buffer to string (sCurrent)
sRecievedString += comPort.ReadExisting();
// when the last member in string is Carriage return copy the string to a temporary location (sTemp)
if (sRecievedString.EndsWith("\r"))
{
sTemp = sRecievedString;
// clear string
sRecievedString = "";
}
// add new buffer to the string
sRecievedString += comPort.ReadExisting();
// compare the starting characters of the two strings
if (sRecievedString[0] == sTemp[0])
{
// if they match, override sTemp with sCurrent and continue reading
sTemp = sRecievedString;
sRecievedString += comPort.ReadExisting();
}
// else display sTemp
else
{
MessageBox.Show(sTemp);
}
}

Related

StreamReader doesn't retrieve everything from NetworkStream (TCP and C#)

I'm working on a project, where you have to retrieve data from a server and display it in the UI. It's a newsgroups server, that contains about 250 groups in total.
The output from the server SHOULD as far as I understand, be stored in a NetworkStream object and is read from a StreamReader which saves each line into a string.
This works, but unfortunately it seems like it doesn't finish reading everything before finishing the method call.
Next time I call another command and read it from the StreamReader it then returns the rest of the output from the previous command.
I've been struggling with this for hours now, and can't see how to fix this.
This is my code:
public ObservableCollection<Newsgroup> GetNewsGroups()
{
ObservableCollection<Newsgroup> newsgroups = new ObservableCollection<Newsgroup>();
if(connectionStatus.Equals(ConnectionStatus.CONNECTED) && loginStatus.Equals(LoginStatus.LOGGED_IN))
{
byte[] sendMessage = Encoding.UTF8.GetBytes("LIST\n");
// Write to the server
ns.Write(sendMessage, 0, sendMessage.Length);
Console.WriteLine("Sent {0} bytes to server...", sendMessage.Length);
ns.Flush();
// b) Read from the server
reader = new StreamReader(ns, Encoding.UTF8);
// We want to ignore the first line, as it just contains information about the data
string test = reader.ReadLine();
Console.WriteLine(test);
string recieveMessage = "";
if (ns.CanRead)
{
while (reader.Peek() >= 0)
{
recieveMessage = reader.ReadLine();
Console.WriteLine("Got this message {0} back from the server", recieveMessage);
// This part will simply remove the annoying numbers after the newsgroup name
int firstSpaceIndex = recieveMessage.IndexOf(" ");
string refactoredGroupName = recieveMessage.Substring(0, firstSpaceIndex);
newsgroups.Add(new Newsgroup { GroupName = refactoredGroupName });
}
}
}
return newsgroups;
}
I'd be interested to see what information about the data you are throwing away on the first line (what's in "test" variable). If it tells you how many bytes are coming your way, you should use that information to retrieve the correct amount of data instead of Peek.
If the last line contains a single period, change your while loop to look like this instead:
recieveMessage = reader.ReadLine();
while (recieveMessage != ".")
{
Console.WriteLine("Got this message {0} back from the server", recieveMessage); // This part will simply remove the annoying numbers after the newsgroup name int
firstSpaceIndex = recieveMessage.IndexOf(" ");
string refactoredGroupName = recieveMessage.Substring(0, firstSpaceIndex);
newsgroups.Add(new Newsgroup { GroupName = refactoredGroupName });
recieveMessage = reader.ReadLine();
}

Sending serial command to trigger scanner Honeywell 1900

The problem is, that I can trigger scanner using Serial Port software "Hercules" sending command <SYN>T<CR><LF>, in datasheet is said to use command [SYN]T[CR] to trigger scanner, but I cant trigger it (both commands) using my serial port comunication bellow.
I get input when use scanner manually but can't trigger it... What is a problem?
(The port is virtual)
private static SerialPort port;
private static bool _continue = false;
public static void Main(string[] args)
{
port = new SerialPort();
port.PortName = "COM8";
port.BaudRate = 115200;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.RtsEnable = true;
port.DtrEnable = true;
port.ReadTimeout = 500;
port.WriteTimeout = 500;
port.Open();
_continue = true;
Thread thr = new Thread(SerialPortProgram);
thr.Start();
}
private static void SerialPortProgram()
{
Console.WriteLine("Writing to port: <SYN>T<CR><LF>");
string command = "<SYN>T<CR><LF>";
port.WriteLine(command);
while (_continue)
{
try
{
string input = port.ReadLine();
Console.WriteLine("Input is - " + input);
}
catch (TimeoutException) { }
}
}
Python barcode scanner serial trigger is an article that I answered similar Python question.
The contents are shown below.
This happens because you coded the abstract expression written in the document as raw output data.
The document represents 3 bytes of data transmission.
'SYN' and 'CR' are the following hexadecimal numbers.
'SYN' = \x16
'CR' = \x0d or escape sequence \r
'T' is an ordinary ASCII character.
Whitespace and < > [ ] { } are used to delimit the data in the document, not the data to send.
And, even you need to command prefix it.
Also use Write instead of WriteLine as written by #Turbofant.
You should write like this. Please try it.
string command = "\x16M\x0d\x16T\x0d";
port.Write(command);
I guess the problem is, that you are sending the wrong command string.
The <Syn>, <CR> and <LF> stands for the special, non printable ascii characters synchronous idle, Carriage return and line feed.
You need to encode them correctly in the string
Try sending:
string command = "\x16t\r\n";
port.Write(command);
\x16is <Syn> (Because Syn is ascii character 0x16, or 22 in decimal )
\r is <CR>
\n is <LN>
And use port.Write instead of port.WriteLine, because WriteLine automatically adds the \r\n at the end of the string.

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.

C# Serialport not receiving all the bytes in a single transmission

I'm using the following code to receive hex data over the serial port, it seems that some of these transmissions are broken up into 2 lines when they are parts of the same transmission. How do I make sure that each transmission is received properly?
public void Receiver(object sender, SerialDataReceivedEventArgs e)
{
string data;
do
{
data = COMPort.ReadExisting();
} while (COMPort.BytesToRead != 0);
RxARR = data.ToCharArray().ToList();
Dispatcher.Invoke(new MethodInvoker(Display)); // Start "Display" on the UI thread
}
You should never assume that data from a serial port is provided to you in one go (the same is true for network communication by the way). You need to make sure that your code accepts a message only if you have received everything.
Everything is hard to define. This can either be all characters until a defined termination sequence (for example \r\n or EOL byte) or until a fixed number of bytes is read. Generally one can say, as every message can be fragmented, reliable communcation is not possible without a defined end of message signal.
What we do is:
Create a StringBuilder
Read everything into that StringBuilder
Search for termination sequence
Remove everything from the StringBuilder up to and including the termination sequence
Process that chunk of data
Repeat from 3 until termination sequence is not found
Keep the remaining characters in StringBuilder, as this is the start of a new message
Pseudo code:
private StringBuilder serialBuffer = new StringBuilder();
private string terminationSequence = "\r\n"; // Anything that can't be part of a message
public void Receiver(object sender, SerialDataReceivedEventArgs e)
{
string data = COMPort.ReadExisting();
serialBuffer.Append(data);
string bufferString = serialBuffer.ToString();
int index = -1;
do
{
index = bufferString.IndexOf(terminationSequence);
if (index > -1)
{
string message = bufferString.Substring(0, index);
bufferString = bufferString.Remove(0, index + terminationSequence.Length);
HandleMessage(message);
}
}
while (index > -1)
serialBuffer = new StringBuilder(bufferString);
}
By the way: Looping within your DataReceived event is not desired, as this event is called by the SerialPort whenever something new is ready to be read. Reading in a loop may interfere with what SerialPort is doing by default. So: Don't read in a loop within that event! The "loop" is the event being fired in sequence by SerialPort.
I Had almost the same situation.
Like Thorsten said the data sometimes does not come at once..
Maybe because of Hardware or BaundRate..
What really works for me was:
var end = '\r';
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
RxString = Encoding.ASCII.GetString(data);
txtCod.AppendText(RxString);
if (RxString.IndexOf((char)end) > -1)
{
this.Invoke(new EventHandler(Final));
}

TCP Client - loop in communication

I am writing a program which goal is to communicate with the weight terminal using TCP client.
I'm sending specified messages (eg. checking status) and depending on the replies I'm making some another process.
First, some code.
Connection:
public static void PolaczZWaga(string IP, int port)
{
IP = IP.Replace(" ", "");
KlientTCP = new TcpClient();
KlientTCP.Connect(IPAddress.Parse(IP), port);
}
Sending message (eg. checking status)
public static string OdczytDanychZWagi(byte[] WysylaneZapytanie)
{
// Wysyłka komunikatu do podłączonego serwera TCP
byte[] GotoweZapytanie = KomunikatyWspolne.PoczatekKomunikacji.Concat(WysylaneZapytanie).Concat(KomunikatyWspolne.KoniecKumunikacji).ToArray();
NetworkStream stream = KlientTCP.GetStream();
stream.Write(GotoweZapytanie, 0, GotoweZapytanie.Length);
// Otrzymanie odpowiedzi
// Buffor na odpowiedz
byte[] odpowiedz = new Byte[256];
// String do przechowywania odpowiedzi w ASCII
String responseData = String.Empty;
// Odczyt danych z serwera
Int32 bytes = stream.Read(odpowiedz, 0, odpowiedz.Length);
responseData = System.Text.Encoding.ASCII.GetString(odpowiedz, 0, bytes);
return responseData;
}
After Form1 open I make an connection and checking status
string odp = KomunikacjaSieciowa.OdczytDanychZWagi(OdczytZWagi.Kom_RejestrStatusu);
char status = odp[0];
switch(status)
{
case 'B':
KomunikacjaSieciowa.WysylkaDoWyswietlaczaWagi_4linie(WysylkaDoWyswietlacza_Komunikaty.LogWitaj, WysylkaDoWyswietlacza_Komunikaty.LogZaloguj, WysylkaDoWyswietlacza_Komunikaty.PustaLinia, WysylkaDoWyswietlacza_Komunikaty.LogNrOperatora);
string NrOperatora = KomunikacjaSieciowa.OdczytDanychZWagi(OdczytZWagi.Kom_ZatwierdzoneF1);
//int NrOperatora_int = Convert.ToInt32(NrOperatora);
break;
// here goes next case etc
Here starts my problem - communication takes place only once and the operation requires data on the terminal. Before the operator enters data program ends.
How to change the code / loop / add a timer to repeated communication to achieve a certain status?
More specifically, as in this passage:
case 'B':
KomunikacjaSieciowa.WysylkaDoWyswietlaczaWagi_4linie(WysylkaDoWyswietlacza_Komunikaty.LogWitaj, WysylkaDoWyswietlacza_Komunikaty.LogZaloguj, WysylkaDoWyswietlacza_Komunikaty.PustaLinia, WysylkaDoWyswietlacza_Komunikaty.LogNrOperatora);
string NrOperatora = KomunikacjaSieciowa.OdczytDanychZWagi(OdczytZWagi.Kom_ZatwierdzoneF1);
repeat "string NrOperatora" depending on the returned data?
Where's the best place to make loop?? Maybe I should use thread??
I think using stream.BeginRead and checking the status when reads are complete is the best way so if the status is not OK you can call stream.BeginRead to the same method so it will be a loop calling here self until status is OK

Categories

Resources