I am writing a program which writes channel urls and channel names to a device. I'm trying to write channel urls with a length of 254. Shorter urls (I haven't found the threshold yet) do work.
What makes it more strange is that the channel names are only 16 characters long, and these do also only write when the channel urls are shorter.
I have the following code which works:
for (int i = 0; i < numberOfChannels; i++)
{
string hexString = String.Format("{0:X1}", i);
Serialport.WriteLine("channel 2 " + hexString + " " + channelURLs[i]);
System.Threading.Thread.Sleep(1000);
Serialport.WriteLine("channel 1 " + hexString + " " + channelnames[i]);
System.Threading.Thread.Sleep(1000);
}
however, I do not want to have my code sleep 2seconds for writing 1 channel. QUESTION: How do I get those long strings written through the serialport without waiting 2 seconds?
This does not work:
for (int i = 0; i < numberOfChannels; i++)
{
string hexString = String.Format("{0:X1}", i);
if (Serialport.WriteLine("channel 2 " + hexString + " " + channelURLs[i]))
{
Debug.WriteLine("this should happen before the string is written");
}
if(Serialport.WriteLine("channel 1 " + hexString + " " + channelnames[i]))
{
Debug.WriteLine("this should happen before the string is written");
}
}
the debug lines are written, and the string is not written through the comport.
my Serialport.WriteLine function looks like this:
public static bool WriteLine(string data)
{
if (SerPort.IsOpen)
{
Debug.WriteLine("PORT OPEN, WRITING "+ data);
SerPort.WriteLine(data);
return true;
}
else return false;
}
UPDATE: I found that when I disconnect and reconnect, the first URL is written. Apparently it has to do with some buffer?
UPDATE2: This is how I set up my serialport:
SerPort.BaudRate = 115200;
SerPort.DataBits = 8;
SerPort.StopBits = StopBits.One;
SerPort.PortName = comnr;
SerPort.Handshake = Handshake.None;
Serialport.SerPort.ReadTimeout = 1000;
Serialport.SerPort.WriteTimeout = 1000;
SerPort.DataReceived += SerPort_Datareceived;
SerPort.Open();
SerPort.DiscardInBuffer();
SerPort.DiscardOutBuffer();
UPDATE 3: Fixed the issue
I've found where my issue was. Before sending the commands shown here, I sent a command which took some time to process. If the device recieved data during this process it would stop receiving data. I updated my code with a timer to check for an OK respond from the device before sending the next commands.
This depends on the device you are working on. If you send too many bytes to it, there's a chance that it cannot process them all.
You can try to decrease the waiting time from 1000 ms to, for example, 200 ms.
You can also try to send packages of data... for example, send the long string in packages of 50 characters, waiting an amount of time between them (example, 100 ms).
UPDATE 1:
Also try to decrease the BaudRate from 115200 to 9600.
Related
for (int i = 5; i <= 15; i++)
{
string temp = string.Concat(base_IP, i.ToString());
//await Task.Delay(1000);
PingReply reply = pingsender.Send(temp, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
textbox_progress.Text = textbox_progress.Text + "Connected IP -> " + temp + "\n\r";
}
progressBar.Value++;
}
textbox_progress.Text = textbox_progress.Text + "Scanning Complete " + "\n\r";
In the code segment , the progress-bar should increment with the increase in the value of variable 'i'. However , it doesn't happen. The progress-bar get's updated all at once after the loop ends. If I comment out the IP scanning function and use the delay function, the progress-bar works properly. Can anyone help me to fix it?
Besides this, the scanning time is too long. I simply transmitted "abcd" to get ping. This small loop takes approximately 6 seconds to complete. The value of "timeout" is one [Although I'm confused if the "pingsender.Send()" function takes it as seconds or milliseconds]. I
The ProgressBar cannot be updated while your for loop is being executed on the same thread. This is impossible.
Use the asynchronous SendPingAsync method:
for (int i = 5; i <= 15; i++)
{
string temp = string.Concat(base_IP, i.ToString());
PingReply reply = await pingsender.SendPingAsync(temp, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
textbox_progress.Text = textbox_progress.Text + "Connected IP -> " + temp + "\n\r";
}
progressBar.Value++;
}
textbox_progress.Text = textbox_progress.Text + "Scanning Complete " + "\n\r";
Although I'm confused if the "pingsender.Send()" function takes it as seconds or milliseconds
The timeout parameter specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.
i study C# Serial.
i write this code to receive Data.
when i run this code and another device sent data only once, but the program receives the data twice or more than three times.
how can i fix this problem?
There's still a lot I don't know. Please explain it easily.
I spent a week because I couldn't solve this problem.... :(
private void MainForm_Load(object sender, EventArgs e)//main form
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(EventDataReceived);
CheckForIllegalCrossThreadCalls = false;
........
........
........
}
........
........
........
void EventDataReceived(object sender, SerialDataReceivedEventArgs e)//this is receiving data method
{
int size = serialPort1.BytesToRead;// assign size of receive data to 'int size'
byte[] buff = new byte[size];// array who assign receiving data(size = int size)
serialPort1.Read(buff, 0, size);//assign receive data to 'buff'
string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
for (int i = 0; i < size; i++)
{
tb_rx.Text = tb_rx.Text + " \r\n " + hexData;
Thread.Sleep(1000);//When I first encountered the problem, I added it because I thought the interval was too short. But I don't think this is the solution.
}
}
This code is fishy:
string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
for (int i = 0; i < size; i++)
{
tb_rx.Text = tb_rx.Text + " \r\n " + hexData;
Thread.Sleep(1000);//When I first encountered the problem, I added it because I thought the interval was too short. But I don't think this is the solution.
}
BitConverter.ToString(byte[]) already converts the byte array to a sequence of hex strings. In the for loop, you then add this hexData string to tb_rx (probably a textbox) for each byte received. So depending on the number of bytes you receive at once, you do get duplicate output. Just change that to:
string hexData = BitConverter.ToString(buff).Replace("-", " ");//Convert the received data into hexadecimal numbers and store to 'string hexdata'
tb_rx.Text = tb_rx.Text + " \r\n " + hexData;
When I want to program a Teensy 3.5 micro-controller, I send it a .HEX file via a hardware serial port. I have two ways of doing this; one way is to use a serial comms app like Tera Term to send the file, and another way is via a small C# command-line app I wrote to do the same thing.
When I send a 3000+ line Hex file to the micro-controller, it takes approximately 14 seconds. When I do the same thing with my C# program, it takes twice as long (or longer!). The baud rate for both Tera Term and my C# program are the same, and of course the Teensy setup is common to both.
When I looked as the serial transfer using a digital scope, I see the following (same time scale in both photos):
The first photo shows the transfer when using Tera Term, and the second one shows the transfer when using my C# program using Serial.WriteLine() to transfer file contents line-by-line, as shown below:
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Timers;
using System.Diagnostics;
/*
Small console app to facilitate over-the-air (OTA) updates to a Teensy 3.x/4.x controller,
using VS2019 with the Visual Micro extension as the Arduino IDE. It is called by a post-build
'hook' statement in a file called 'board.txt' located in whatever Teensy program is
being updated. This app does the following:
- Extract the project path and selected COMPORT number from the arguments to the call to Main()
- Opens a UART serial port connection to the Teensy, typically one provided by a BT adaptor
operating in 'pass-through' mode. The serial port COMPORT number is passed into this app
as an argument.
- Sends whatever command is required to put the existing Teensy firmware into 'update' mode
- Using the path of the updating program (passed in as an argument), locates the .HEX file
associated with the project, and sends it's contents to the Teensy, one line at a time, counting
lines and confirming checksums line-by-line
- Compares the number of lines sent to the Teensy with the number of lines received by the Teensy,
and if there is a match, allows the Teensy update process to complete; otherwise aborts
*/
namespace TeensyFlash
{
class Program
{
const string startCmdStr = "U"; //used in sketch's 'GetUserInput()' to start upload sequence
static string rcvStr = string.Empty;
private static System.Timers.Timer aTimer;
private static bool bTimedOut;
static void Main(string[] args)
{
//Extract the build path and selected COMPORT number from the arguments to the call to Main()
Console.WriteLine("Teensy Flash Console");
Console.WriteLine("Number of arguments in args = {0}\n", args.Length);
int argindex = 0;
string comPortStr = string.Empty;
foreach (var item in args)
{
Console.WriteLine(item);
if (item.Contains("COM"))
{
comPortStr = args[argindex];
}
argindex++;
}
string build_path = args[0];
string projectName = args[args.Length - 1];
projectName = projectName.Substring(0, projectName.Length - 4); //remove extension
build_path = build_path.Replace("\"", string.Empty).Trim();
string hexfilename = build_path + "\\" + projectName + ".hex";
Console.WriteLine("path = {0}", build_path);
Console.WriteLine("comport = {0}", comPortStr);
Console.WriteLine("build name = {0}", projectName);
Console.WriteLine("path to HEX file = {0}", hexfilename);
try
{
Stopwatch stopwatch = Stopwatch.StartNew();
string[] lines = System.IO.File.ReadAllLines(hexfilename);
foreach (string item in lines)
{
Console.WriteLine(item);
}
Console.WriteLine("this took " + stopwatch.ElapsedMilliseconds + " Msec");
}
catch (Exception)
{
throw;
}
Console.WriteLine("Opening Serial Port...");
try
{
SerialPort _serport = new SerialPort(comPortStr, 115200);
_serport.WriteTimeout = 1000;
_serport.WriteBufferSize = 20480;
_serport.Open();
_serport.DiscardOutBuffer();
_serport.DiscardInBuffer();
Thread.Sleep(100);
Console.WriteLine("Sending Trigger Character " + startCmdStr);
Console.WriteLine(startCmdStr);
_serport.Write(startCmdStr);
Console.WriteLine("Waiting for 'waiting' from Teensy...");
rcvStr = string.Empty;
aTimer = new System.Timers.Timer();
//aTimer.Interval = 5000;
aTimer.Interval = 25000;
aTimer.Elapsed += OnTimedEvent;
aTimer.Start();
while (!rcvStr.Contains("waiting") && !bTimedOut)
{
if (_serport.BytesToRead > 0)
{
rcvStr = _serport.ReadLine();
}
}
aTimer.Stop();
if (bTimedOut)
{
Console.WriteLine("Timed out waiting for 'waiting' response from Teensy");
}
else
{
//if we get to here, the Teensy is ready to receive HEX file contents
Console.WriteLine("Received " + rcvStr + " from Teensy");
Stopwatch stopwatch2 = Stopwatch.StartNew();
int numlines = 0;
string[] lines = System.IO.File.ReadAllLines(hexfilename);
foreach (string item in lines)
{
numlines++;
_serport.WriteLine(item);
}
Console.WriteLine("total lines = {0}, time = {1} mSec", numlines, stopwatch2.ElapsedMilliseconds);
//now we wait for Teensy to emit "hex file: xx lines xx bytes..." and then "enter xx to flash..."
aTimer.Start();
while (!rcvStr.Contains("hex file:") && !bTimedOut)
{
if (_serport.BytesToRead > 0)
{
rcvStr = _serport.ReadLine();
}
}
aTimer.Stop();
aTimer.Dispose();
if (bTimedOut)
{
Console.WriteLine("Timed out waiting for 'hex file' response from Teensy");
}
else
{
//extract number of lines from Teensy string, and compare with numlines.
//If they match, then send the number back to Teensy to complete the update.
//Otherwise, send '0' to abort
int colonIdx = rcvStr.IndexOf(':');
int lineIdx = rcvStr.IndexOf("lines");
string compareStr = rcvStr.Substring(colonIdx + 1, lineIdx - colonIdx - 1);
compareStr = compareStr.Trim();
int numTeensyLines = Convert.ToInt16(compareStr);
Console.WriteLine("sent {0} teensy replied {1}", numlines, numTeensyLines);
if (numTeensyLines == numlines)
{
Console.WriteLine("numlines {0} matches numTeensyLines {1} - send confirmation",
numlines, numTeensyLines);
_serport.WriteLine(compareStr);
}
}
}
}
catch (Exception)
{
throw;
}
try
{
}
catch (Exception)
{
throw;
}
}
static string chksum(string input)
{
int TwosComplement(string s)
{
if (s.Length % 2 != 0)
throw new FormatException(nameof(input));
var checksum = 0;
for (var i = 0; i < s.Length; i += 2)
{
var value = int.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
checksum = (checksum + value) & 0xFF;
}
return 256 - checksum & 0xFF;
}
//return string.Concat(":", input, " ", TwosComplement(input).ToString("X2"));
return TwosComplement(input).ToString("X2");
}
private static void SetTimer()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(2000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
//aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
e.SignalTime);
bTimedOut = true;
}
}
}
The code that actually does the file transfer is:
Stopwatch stopwatch2 = Stopwatch.StartNew();
int numlines = 0;
string[] lines = System.IO.File.ReadAllLines(hexfilename);
foreach (string item in lines)
{
numlines++;
_serport.WriteLine(item);
}
Console.WriteLine("total lines = {0}, time = {1} mSec", numlines, stopwatch2.ElapsedMilliseconds);
When the '_serport.WriteLine(item);' line is commented out, the reported elapsed time is 0 mSec, as expected - so the 'ReadAllLines()' step isn't the problem.
Anyone have an idea why the 'WriteLine(item)' processing is so slow?
It depends on the structure of the method itself with the device, but I found on the same site an inquiry and means for it that help you
stackoverflow: console writeline slow
The problem
From the serial port I receive a stream of characters. The stream I receive will be terminated by \n. I receive the serial port data in parts like this:
Serial port Event 1: "123\n456\n789"
Serial port Event 2: "\n0123\n456\n789\n"
Serial port Event 3: "0123\n456\n789\n"
As you can see my stream fluctuates, and this is very normal since I read the serial port with "what is available currently".
My problem is that I want to log this to the UI with a RichTextBox. I cannot use ListBox because I need color and font formatting.
First approach
Before I tried this below and that works very well till the time the messages exceed around 30.000 lines of text. The UI gets unresponsive. This was the code:
uiTextActivity.SelectionFont = new Font(uiTextActivity.SelectionFont, FontStyle.Bold);
uiTextActivity.SelectionColor = LogMsgTypeColor[(int)msgtype];
uiTextActivity.AppendText(msg);
uiTextActivity.ScrollToCaret();
Later on I used this as a quick fix that clears the box after 2000 lines:
if ((int.Parse(uiLabelCounterActivity.Text) + 1) > 2000)
uiTextBoxResetActivity();
I want to keep a history of around 500lines.
But with this quick fix above I lose my log completely when the counter hits 2000.
I think what I need is a circular FIFO TextBox. So I can remove after 500 lines the oldest logs and append the new one.
Second approach
I also tried this (note that in this case below the newest logs entries are on top and the oldest below in the textbox)
msg = msgToPasteWhenComplete + msg;
int c = 0; // c=zero if no newline, 1 for single newline, 2 for 2times newline and so on
int latestTermination = 0;// store the point where \n is found
for (int e = 0; e < msg.Length; e++)
{
if (msg[e] == '\n')
{
c++;
latestTermination = e+1;
}
}
// contains strings all terminated with \n
string msgToPaste = msg.Substring(0, latestTermination);
// contains string that is not complete (not yet terminated with \n)
msgToPasteWhenComplete = msg.Substring(latestTermination, msg.Length - latestTermination);
string previous = msgToPaste + uiTextActivity.Text;
if (previous.Length > maxDisplayTextLength)
{
string debugInfo3 = previous.Substring(0, maxDisplayTextLength);
uiTextActivity.Text = debugInfo3;
}
else
uiTextActivity.Text = previous;
This works almost very well. The problem with this approach is that the line that comes from the serial port will not be pasted to the UI until the \n is received. This means that whenever communication is slow, I will have to wait for the serial stream to complete the whole line including \n before I can see it... What I want is to see every character directly.
info about serialport
The serial data am I getting from the SerialDataReceivedEvent, in that event I use comport.ReadExisting() to have a non-blocking event.
The serial data comes from my embedded programming board that has analog readings. The analog readings supply me with 20 lines per second, each line containing 20 chars.
I need the raw data to be read in a user interface where it has to be filtered to other textboxes depending on the prefix of the serial message (like err goes to error , warn goes to warning textbox, but they all go to the activity log. The activity log is what this question is about.
I wish I could give you a specific chunk of code to work off of, but I haven't had to do this kind of input processing in a while.
That being said, you might get better performance by buffering your inputs into a Queue (MSDN reference) object, and then polling the queue on a timer or by responding to some other event (maybe OnChanged?).
I found a solution that works, here is my code:
private const int maxDisplayTextLength = 5000;
private string splitString = "";
private int activityCount = 0;
private string errorMessageBox = "";
private bool errorMessageBoxNeedUpdate = true;
private int errorMessageBoxCount = 0;
private string filteredMessageBox = "";
private int filteredMessageCount = 0;
private bool filteredMessageBoxNeedUpdate = true;
private string activityLogFilterKeyword = "Warning";
string logHelperStringMaxSizeLimiter(string input)
{
// check if our buffer isn't getting bigger than our specified max. length
if (input.Length > maxDisplayTextLength)
{
// discard the oldest data in our buffer (FIFO) so we have room for our newest values
input = input.Substring(input.Length - maxDisplayTextLength);
}
return input;
}
private void logHelperIncoming(string msg)
{
msg = msg.Replace('\0', '\n'); // remove \0 NULL characters as they have special meanings in C# RichTextBox
// add the new received string to our buffer
splitString += msg;
// filter out special strings
int searchIndexStart = splitString.Length - msg.Length;
for (int e = searchIndexStart; e < splitString.Length; e++)
{
if (splitString[e] == '\n')
{
activityCount++;
string subString = splitString.Substring(searchIndexStart, e - searchIndexStart) + "\n";
searchIndexStart += e - searchIndexStart + 1; // update searchindex for next search
string filterError = "error";
// filter messages that start with error
if (subString.Length > filterError.Length) // for this filter, the length should be at least length of error
{
if (String.Compare(subString, 0, filterError, 0, 4, true) == 0)
{
errorMessageBox += subString;
errorMessageBoxNeedUpdate = true;
errorMessageBoxCount++;
}
}
// filter messages that start with XXX
if (subString.Length > activityLogFilterKeyword.Length && activityLogFilterKeyword.Length != 0) // for this filter, the length should be at least length of error
{
if (String.Compare(subString, 0, activityLogFilterKeyword, 0, activityLogFilterKeyword.Length, true) == 0)
{
filteredMessageBox += subString;
filteredMessageBoxNeedUpdate = true;
filteredMessageCount++;
}
}
}
}
}
// outputs to main activity textbox
private void Log(LogMsgType msgtype, string msg)
{
if (msgtype == LogMsgType.Incoming || msgtype == LogMsgType.Normal)
{
logHelperIncoming(msg);
}
else if (msgtype == LogMsgType.Outgoing)
{
}
splitString = logHelperStringMaxSizeLimiter(splitString);
filteredMessageBox = logHelperStringMaxSizeLimiter(filteredMessageBox);
errorMessageBox = logHelperStringMaxSizeLimiter(errorMessageBox);
uiTextActivity.Invoke(new EventHandler(delegate
{
// time to post our updated buffer to the UI
uiTextActivity.Text = splitString;
uiTextActivity.Update();
// scroll to the newest data only if user has no focus on the
uiTextActivity.SelectionStart = uiTextActivity.TextLength; // make sure view is to the latest
uiTextActivity.ScrollToCaret();
uiLabelCounterActivity.Text = activityCount.ToString();
if (errorMessageBoxNeedUpdate)
{
errorMessageBoxNeedUpdate = false;
uiTextActivity.SelectionColor = Color.Red;
// time to post our updated buffer to the UI
uiTextboxErrorLog.Text = errorMessageBox;
// scroll to the newest data only if user has no focus on the
uiTextboxErrorLog.SelectionStart = uiTextboxErrorLog.TextLength; // make sure view is to the latest
uiTextboxErrorLog.ScrollToCaret();
uiLabelCounterError.Text = errorMessageBoxCount.ToString();
}
if (filteredMessageBoxNeedUpdate)
{
filteredMessageBoxNeedUpdate = false;
// time to post our updated buffer to the UI
uiTextboxFilteredLog.Text = filteredMessageBox;
// scroll to the newest data only if user has no focus on the
uiTextboxFilteredLog.SelectionStart = uiTextboxFilteredLog.TextLength; // make sure view is to the latest
uiTextboxFilteredLog.ScrollToCaret();
uiLabelCounterFiltered.Text = filteredMessageCount.ToString();
}
// extract value from filter and store to filter
activityLogFilterKeyword = uiTextboxFilterKeyword.Text;
}));
}
I came up with the idea of making a label scroll a word to one side and then change the word and scroll back to the other like so
"ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping"
" pong"
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
"pong "
I want it to do ^^ only in a constant loop but I don't know how I would even get started doing that I would REALLY appreciate it if someone could help me with this. The max length of the text has to be 15 characters.
I don't care if it is smooth scrolling.
I want it to be a Winforms application and use .Net framework 4.0.
Here's what I would do, it seemed to work just fine when I tested it out, I created a windows form with a timer and label on it. Make sure to call timer.Start() when you open the form and it will start bouncing around the screen. If you change iUBound to a larger value it will move more spaces across the screen.
string _sPing = "ping";
string _sPong = "pong";
bool bGoingUp = true;
int iUBound = 15;
int iCnt = 1;
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (bGoingUp)
{
label1.Text = " " + label1.Text;
iCnt++;
}
else
{
label1.Text = label1.Text.Substring(1,label1.Text.Length - 1);
iCnt--;
}
if (iCnt == iUBound)
{
bGoingUp = false;
label1.Text = label1.Text.Replace(_sPing, _sPong);
}
else if (iCnt == 1)
{
bGoingUp = true;
label1.Text = label1.Text.Replace(_sPong, _sPing);
}
}
I'd keep the label contents the same and just move the label, feels like it should be less CPU load and the scrolling will be smoother.
Make a for-loop that runs from 0 to 11 (15 - length of "ping"). With new String(' ', i) you can create a string that is i spaces long. Then set the Text of your label to this space string concatenated with the word "ping".
Now you can make another loop, running from 11 down to 0 doing the same but with the word "pong".
If you enclose both loops in an endless loop (while (true) { ... }) this will run indefinitely.
You also might want to add a pause each time after you set the label text with Thread.Sleep(200). Where you specify the time is in milliseconds.
EDIT (since it is not homework):
Go to the events tab in the properties window and add a Shown event handler
private void frmMarquee_Shown(object sender, EventArgs e)
{
while (true) {
for (int i = 0; i <= 11; i++) {
label1.Text = new String(' ', i) + "ping";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
for (int i = 11; i >= 0; i--) {
label1.Text = new String(' ', i) + "pong";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
}
}
Note, this solution is not perfect, as the form will not close properly. You will have to abort the program. A solution using a timer will work smoother and the form will behave as expected when closing, however this is a straightforward and simple solution.
I found this example. Pretty close to what you want. The two key elements are (1)using StringBuilder functions to append characters and (2) an asynchronous delegate to put the animation in a different thread.
The idea of the StringBuilder is great because it should be more efficient that dealing with String. And I like Asynchronous delegate because it sounds way more impressive than Timer