serial port writing and reading
-
another problem I've with this code and writing to port: when I see the data which I'm sending in Comwizard ,sometimes I see an interrupt in the data received by comwizard. it is randomly and when I increase the frequency of sending(3 ms to 10ms ...) it appears in longer time and less than before. I checked all things but I cannot find any mistake in my code . is it possible to be relative to serial port class and functions of c#?can you suggest what is the mistake?
When you say you increase the frequency from 3ms to 10ms, I don't understand because that sounds like you are decreasing the frequency?
The difficult we do right away... ...the impossible takes slightly longer.
-
When you say you increase the frequency from 3ms to 10ms, I don't understand because that sounds like you are decreasing the frequency?
The difficult we do right away... ...the impossible takes slightly longer.
yes . excuse me. I'm decreasing it. and this is the code for reading bytes:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)//while (port != null && port.IsOpen)
{
try
{
var b1 = port.ReadByte();if (b1 == ReadSignal.STARTByte) { //read location b1 = port.ReadByte(); ... if (b1 == ReadSignal.STOPByte) { RSignal.isValid = true; } } if (RSignal.isValid) { RSignal.ExtractLocatoins(); if (DataReceived != null) DataReceived.Invoke(RSignal); } } catch (Exception ex) { //MessageBox.Show("1:"+ex.ToString()); } } }
and code for writing:
public void SendCommand(SerialPort port,byte[] command)//public void SendCommand(byte[] command)
{if (port != null && port.IsOpen) { port.Write(command, 0, command.Length); } else { MessageBox.Show("port is not open!"); } }
-
yes . excuse me. I'm decreasing it. and this is the code for reading bytes:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)//while (port != null && port.IsOpen)
{
try
{
var b1 = port.ReadByte();if (b1 == ReadSignal.STARTByte) { //read location b1 = port.ReadByte(); ... if (b1 == ReadSignal.STOPByte) { RSignal.isValid = true; } } if (RSignal.isValid) { RSignal.ExtractLocatoins(); if (DataReceived != null) DataReceived.Invoke(RSignal); } } catch (Exception ex) { //MessageBox.Show("1:"+ex.ToString()); } } }
and code for writing:
public void SendCommand(SerialPort port,byte[] command)//public void SendCommand(byte[] command)
{if (port != null && port.IsOpen) { port.Write(command, 0, command.Length); } else { MessageBox.Show("port is not open!"); } }
Are you using the ReadLine and Writeline functions? It may be that Comwizard is not waiting for the newline sequence to return the data it sees? Even though there is an interrupt in the data, does the full data come through eventually?
The difficult we do right away... ...the impossible takes slightly longer.
-
Are you using the ReadLine and Writeline functions? It may be that Comwizard is not waiting for the newline sequence to return the data it sees? Even though there is an interrupt in the data, does the full data come through eventually?
The difficult we do right away... ...the impossible takes slightly longer.
-
yes . excuse me. I'm decreasing it. and this is the code for reading bytes:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)//while (port != null && port.IsOpen)
{
try
{
var b1 = port.ReadByte();if (b1 == ReadSignal.STARTByte) { //read location b1 = port.ReadByte(); ... if (b1 == ReadSignal.STOPByte) { RSignal.isValid = true; } } if (RSignal.isValid) { RSignal.ExtractLocatoins(); if (DataReceived != null) DataReceived.Invoke(RSignal); } } catch (Exception ex) { //MessageBox.Show("1:"+ex.ToString()); } } }
and code for writing:
public void SendCommand(SerialPort port,byte[] command)//public void SendCommand(byte[] command)
{if (port != null && port.IsOpen) { port.Write(command, 0, command.Length); } else { MessageBox.Show("port is not open!"); } }
Check my comments in the code below:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)
{
try
{
var b1 = port.ReadByte(); // <<==What if the STOPByte is read here? Does that confuse the logic?if (b1 == ReadSignal.STARTByte) { //read location b1 = port.ReadByte(); ... if (b1 == ReadSignal.STOPByte) { RSignal.isValid = true; } } if (RSignal.isValid) { RSignal.ExtractLocatoins(); if (DataReceived != null) DataReceived.Invoke(RSignal); } } catch (Exception ex) { //MessageBox.Show("1:"+ex.ToString()); } } }
The difficult we do right away... ...the impossible takes slightly longer.
-
Check my comments in the code below:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)
{
try
{
var b1 = port.ReadByte(); // <<==What if the STOPByte is read here? Does that confuse the logic?if (b1 == ReadSignal.STARTByte) { //read location b1 = port.ReadByte(); ... if (b1 == ReadSignal.STOPByte) { RSignal.isValid = true; } } if (RSignal.isValid) { RSignal.ExtractLocatoins(); if (DataReceived != null) DataReceived.Invoke(RSignal); } } catch (Exception ex) { //MessageBox.Show("1:"+ex.ToString()); } } }
The difficult we do right away... ...the impossible takes slightly longer.
-
if the STOPByte is read there just poor out the data until new pack of correct data comes.(until STARTByte be found). also notice that I'm sending data in turn so if one pack of data is received then if no noise all data packs can be received in turn.
Yes you are right. If the full data comes eventually, then maybe there is no problem at all.
The difficult we do right away... ...the impossible takes slightly longer.
-
Yes you are right. If the full data comes eventually, then maybe there is no problem at all.
The difficult we do right away... ...the impossible takes slightly longer.
so what is the cause of the interrupt? is it relative to system(pc) or to serial port or my code? the interrupt (not sending data for some millisecond) can be seen by comwizard , also I must send data for a motor and also the motor shows such interrupt.
-
Yes you are right. If the full data comes eventually, then maybe there is no problem at all.
The difficult we do right away... ...the impossible takes slightly longer.
-
so what is the cause of the interrupt? is it relative to system(pc) or to serial port or my code? the interrupt (not sending data for some millisecond) can be seen by comwizard , also I must send data for a motor and also the motor shows such interrupt.
I'm sorry, I do not know. Are you sure your send data is full every time? I'm not there, so I can't see everything you see.
The difficult we do right away... ...the impossible takes slightly longer.