IRQ and Serial Ports

J

joaquimfpinto

Dear All,

I made an app in c# that uses several serial ports.

For the serial ports I use a pnp Sunix board, some with 8 serial ports
other with 4 or even 2 serial ports.

Whenever I use the development computer I don't have ay problem with
my application.

The first pc I installed the app was exactly the same kind of pc as
the one I use for developing.

Later on I installed the app I some HP Compaq small form factor.

Since the first moment I had problems.

With no apparent reason when I try to open the serial ports the
computer freezes.

Than I disable the serial ports and parallel ports on the BIOS and I
was able to put the app running.

Now I installed on another HP Compaq dc 5100 sff and after deactivate
the serial and parallel ports on the BIOS I could open and run my app
but after restart the computer the all computer freezes again when I
try to open the ports.

What could be wrong with this?

Thanks,


Joaquim
 
N

Nicholas Paldino [.NET/C# MVP]

Well, without seeing any of the code, it's nearly impossible to say.
 
J

joaquimfpinto

Nicholas Paldino [.NET/C# MVP] escreveu:
Well, without seeing any of the code, it's nearly impossible to say.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dear All,

I made an app in c# that uses several serial ports.

For the serial ports I use a pnp Sunix board, some with 8 serial ports
other with 4 or even 2 serial ports.

Whenever I use the development computer I don't have ay problem with
my application.

The first pc I installed the app was exactly the same kind of pc as
the one I use for developing.

Later on I installed the app I some HP Compaq small form factor.

Since the first moment I had problems.

With no apparent reason when I try to open the serial ports the
computer freezes.

Than I disable the serial ports and parallel ports on the BIOS and I
was able to put the app running.

Now I installed on another HP Compaq dc 5100 sff and after deactivate
the serial and parallel ports on the BIOS I could open and run my app
but after restart the computer the all computer freezes again when I
try to open the ports.

What could be wrong with this?

Thanks,


Joaquim
This is the code from the class i use to open and read what is on the
serial port

using System;
using System.IO;
using System.Timers;
using DesktopSerialIO.SerialIO;
using System.Windows.Forms;
namespace Easy_Count
{
/// <summary>
/// Summary description for GFR220.
/// </summary>
public class MaqUm
{

TmpRelogio rel=new TmpRelogio();//nova instancia do TmpRelogio;
reads a file that has the intervals for the different timers
ErrosBD erros=new ErrosBD();//nova instancia do ErrosBD; Writes any
errors
SerialPort prta=new DesktopSerialIO.SerialIO.SerialPort();
SerialPort portabc=new DesktopSerialIO.SerialIO.SerialPort();

public System.Timers.Timer relsense=new System.Timers.Timer();//
timer used to send the sense command to the machine
System.Timers.Timer relbatch = new System.Timers.Timer();//
timer used to send the send_batch command to the machine
System.Timers.Timer relmyx = new System.Timers.Timer();//timer
used to send the set_myx command to the machine
System.Timers.Timer relreset = new System.Timers.Timer();//
timer used to send the reset_remote command to the machine

private int cont;//informs the bill counter number
private byte bc;
private byte[] sbuffer=new byte[30];//send the bytes from the
commands
private byte[] rxbuffer=new byte[33];//receive any bytes from the
serial port
private byte[] rxb=new byte[1];//whenever the serial port has more
bytes than expected pass trough this variable
public ulong[] valor=new ulong[7];//keeps the bill values
private byte bcc;
private byte BCCMode=0;

private bool rej=false;//indicates if there where any bill
rejections
private bool erro = false;//indicates if there is any error
private bool pgravar = false;//indicates if the app can record
the counting
private bool grava = false;//confirms that the app can record
private bool tratou = false;//indicates if the data was
already read and write
private bool aberta = false;//indicates if the serial porta is
open
private string nummaq;//keeps the machine number
private bool acnt=false;//indicates that the bill counter is
counting
private bool pode_ler=true;//indicates if the bar code reader can
read; it can reader only after the data has been recorded
//public bool pode_trab;//indica se já pode trabalhar

//variaveis para guardar a contagem; 1º como "string" e depois como
"decimal"
private string cinco;
private string dez;
private string vinte;
private string cinq;
private string cem;
private string duz;
private string quinh;

private ulong cinco_dec;
private ulong dez_dec;
private ulong vinte_dec;
private ulong cinq_dec;
private ulong cem_dec;
private ulong duz_dec;
private ulong quinh_dec;

public MaqUm()
{

rel.Tempo();//le os valores dos relógios relsense e relbatch
relsense.Elapsed+=new
System.Timers.ElapsedEventHandler(relsense_Elapsed);
relbatch.Elapsed+=new
System.Timers.ElapsedEventHandler(relbatch_Elapsed);
relreset.Elapsed+=new
System.Timers.ElapsedEventHandler(relreset_Elapsed);
relmyx.Elapsed+=new
System.Timers.ElapsedEventHandler(relmyx_Elapsed);

relsense.Interval=Variaveis.RSense;
relreset.Interval=Variaveis.RReset;
relbatch.Interval=Variaveis.RBat;
relmyx.Interval=250;//250

relsense.AutoReset=true;
relreset.AutoReset=true;
relbatch.AutoReset=true;
relmyx.AutoReset=true;

portabc.BitRate=9600;
portabc.DataBits=8;
portabc.StopBits=2;
portabc.Parity="n";
portabc.EnableOnComm=true;
portabc.Timeout=100;
portabc.OnComm+=new
DesktopSerialIO.SerialIO.SerialPort.OnCommEventHandler(portabc_OnComm);

prta.BitRate=2400;
prta.DataBits=7;
prta.StopBits=1;
prta.Parity="e";
//prta.Timeout=100;
prta.Timeout=Variaveis.RTime;
prta.EnableOnComm=true;
prta.OnComm+=new
DesktopSerialIO.SerialIO.SerialPort.OnCommEventHandler(prta_OnComm);

}
#region Abertura e fecho portas serie
public void AbrePorta(int num)
{
prta.CommPort=num;
prta.PortOpen=true;
if(prta.PortOpen==true)
{
reset_remote();
set_myx();
Aberta=true;
cont=num;
relsense.Enabled=true;
}
}
public void FechaPorta()
{
if(prta.PortOpen==true)
{
prta.PortOpen=false;
}
prta.Dispose();
Aberta=false;
}
public void AbrePortaBC(short num_Porta)//abre porta do leitor
{
portabc.CommPort=num_Porta;
portabc.PortOpen=true;
}
public void FechaPortaBC()//fecha porta do leitor
{
if(portabc.PortOpen==true)
portabc.PortOpen=false;
portabc.Dispose();
}
#endregion
#region Comandos para enviar à máquina
public void reset_remote()
{
limpa();
sbuffer[0]=0x02;
sbuffer[1]=0x00;
sbuffer[2]=0x01;
sbuffer[3]=0x30;
sbuffer[4]=0x03;
SendBlock(5);
relmyx.Start();
relsense.Start();
}
public void set_myx()
{
sbuffer[0]=0x02;
sbuffer[1]=0x00;
sbuffer[2]=0x01;
sbuffer[3]=0x32;
sbuffer[4]=0x03;
SendBlock(5);
}
public void set_dd()
{
sbuffer[0]=0x2;
sbuffer[1]=0x99;
sbuffer[2]=0x91;
sbuffer[3]=0x31;
sbuffer[4]=0x3;
SendBlock(5);
}
public void sense()
{
try
{
if(NumMaq!=null)
{
sbuffer[0]=0x02;
sbuffer[1]=0x00;
sbuffer[2]=0x01;
sbuffer[3]=0x35;
sbuffer[4]=0x03;
SendBlock(5);
}
}
catch(Exception erro)
{
erros.GravaErro(DateTime.Now.ToString("hh:mm:ss.fff")+" - "+
erro.Message);
}
}
public void send_batch()
{
sbuffer[0]=0x02;
sbuffer[1]=0x00;
sbuffer[2]=0x01;
sbuffer[3]=0x34;
sbuffer[4]=0x03;
SendBlock(5);
}
public void contagem()
{
sbuffer[0]=0x02;
sbuffer[1]=0x00;
sbuffer[2]=0x01;
sbuffer[3]=0x36;
sbuffer[4]=0x03;
SendBlock(5);
}
#endregion
#region Envia e recebe dados porta serie
private void SendBlock(int numero)
{
int index;
for(index=1,bcc=0;index<numero;index++)
{
if(BCCMode==0) bcc ^= (byte) sbuffer[index];
else
bcc += (byte) sbuffer[index];
sbuffer[numero]=bcc;
}
if(numero!=0)
for(int i=0;i<=numero;i++)
prta.Output(new byte[] {sbuffer});
}
private void prta_OnComm()
{
log_tudo();//writes everything that comes from the serial port
switch(prta.InBufferCount)
{
case 1:
case 9:
rxbuffer=prta.InputArray();
tratamento();//checks what is happening with the bill counter
break;
case 33:
rxbuffer=prta.InputArray();
relsense.Stop();
trata_valores();//reads and prepares any data
break;
}
if(prta.InBufferCount>33)
rxb=prta.InputArray();
}

private void portabc_OnComm()
{
if(PodeLer==true)//can the bar code read
{
limpa();//cleans the variables
NumMaq=portabc.InputString();
log_num();
}
}
#endregion
#region Codigo dos 4 relogios Sense; SendBatch; Myx e Reset
private void relsense_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
try
{
sense();
}
catch(Exception erro)
{
erros.GravaErro(DateTime.Now.ToString("hh:mm:ss.fff")+" - "+
erro.Message);
}
}
private void relbatch_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
relbatch.Stop();
send_batch();
}
private void relmyx_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
relmyx.Stop();
set_myx();
}
private void relreset_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
relreset.Stop();
reset_remote();
}
#endregion
#region Tratamento dos dados porta serie da maquina e leitor de
codigo de barras
private void tratamento()
{
try
{
switch(rxbuffer.Length)
{
case 9:
switch(rxbuffer[3])
{
case 112:
ACnt=false;
break;
case 113:
PGravar=false;
ACnt=true;
Erro=false;
Rej=false;
break;
}
switch(rxbuffer[5])
{
case 64: //sem notas
if(PGravar==true&&Rej==false)
Grava=true;
Rej=false;
break;
case 65: //só notas para contar
Rej=false;
PodeLer=false;
Grava=false;
PGravar=false;
Tratou=false;
relbatch.Start();
break;
case 66: //só notas contadas
ACnt=false;
PGravar=true;
Rej=false;
break;
case 67: // notas contadas mais notas para contar
Grava=false;
PGravar=false;
Tratou=false;
Rej=false;
relbatch.Start();
break;
case 68: //só notas rejeitadas
case 69: // notas rejeitadas mais notas para contar
case 70: // notas rejeitadas mais notas contadas
case 87:
ACnt=false;
Rej=true;
PGravar=false;
break;
case 73://erro
Erro=true;
PGravar=false;
break;
case 72: //erros
case 74:
case 75:
case 78:
case 79:
Erro=true;
PGravar=false;
relsense.Stop();
reset_remote();
break;
}
break;
}
}
catch(Exception erro)
{
erros.GravaErro(DateTime.Now.ToString("hh:mm:ss.fff")+" - "+
erro.Message);
}
}

private void trata_valores()
{
try
{
bc=0;
for(int i=1;i<=rxbuffer.Length-2;i++)
bc^=rxbuffer;
if(bc==rxbuffer[32])
{
log_cnt();
if(Tratou==false)
{
try
{
for(int i=25;i<=27;i++)
Cinco+=(char)rxbuffer;
for(int i=22;i<=24;i++)
Dez+=(char)rxbuffer;
for(int i=19;i<=21;i++)
Vinte+=(char)rxbuffer;
for(int i=16;i<=18;i++)
Cinq+=(char)rxbuffer;
for(int i=13;i<=15;i++)
Cem+=(char)rxbuffer;
for(int i=10;i<=12;i++)
Duz+=(char)rxbuffer;
for(int i=7;i<=9;i++)
Quinh+=(char)rxbuffer;

CincoDec+=Convert.ToUInt64(Cinco,10);
DezDec+=Convert.ToUInt64(Dez,10);
VinteDec+=Convert.ToUInt64(Vinte,10);
CinqDec+=Convert.ToUInt64(Cinq,10);
CemDec+=Convert.ToUInt64(Cem,10);
DuzDec+=Convert.ToUInt64(Duz,10);
QuinhDec+=Convert.ToUInt64(Quinh,10);

valor[0]=CincoDec;
valor[1]=DezDec;
valor[2]=VinteDec;
valor[3]=CinqDec;
valor[4]=CemDec;
valor[5]=DuzDec;
valor[6]=QuinhDec;

Cinco=null;
Dez=null;
Vinte=null;
Cinq=null;
Cem=null;
Duz=null;
Quinh=null;

PGravar=true;
Grava=false;
Tratou=true;
}
catch(Exception erro)
{
erros.GravaErro("Tratamento dos dados da contagem
"+erro.Message);
}
}
}
relsense.Start();
}
catch(Exception erro)
{
erros.GravaErro(DateTime.Now.ToString("hh:mm:ss.fff")+" - "+
erro.Message);
}
}
#endregion
#region Limpeza de dados
/// <summary>
/// Limpa os dados depois de contar e gravar ou quando existe um
erro na máquina
/// </summary>
public void limpa()
{
Cinco=null;
Dez=null;
Vinte=null;
Cinq=null;
Cem=null;
Duz=null;
Quinh=null;

CincoDec=0;
DezDec=0;
VinteDec=0;
CinqDec=0;
CemDec=0;
DuzDec=0;
QuinhDec=0;

valor[0]=0;
valor[1]=0;
valor[2]=0;
valor[3]=0;
valor[4]=0;
valor[5]=0;
valor[6]=0;
if(Erro==false)
NumMaq=null;
}

#endregion
#region Grava informação nos ficheiros log
private void log_cnt()
{
string ficheiro=@"Backup\LogContagem"+cont+"-"+Variaveis.data_part
+".txt";
if(!File.Exists(ficheiro))
{
using (StreamWriter sw = new StreamWriter(ficheiro))
{
sw.Flush();
sw.Write(NumMaq+",");
foreach(byte b in rxbuffer)
sw.Write(b+",");
sw.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part
+","+prta.InBufferCount+" , "+relsense.Enabled);
sw.WriteLine();
}
}
else if (File.Exists(ficheiro))
{
using (StreamWriter s_w = File.AppendText(ficheiro))
{
s_w.Flush();
s_w.Write(NumMaq+",");
foreach(byte b in rxbuffer)
s_w.Write(b+",");
s_w.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part
+","+prta.InBufferCount+" , "+relsense.Enabled);
s_w.WriteLine();

}
}
}
private void log_tudo()
{
string ficheiro=@"Backup\LogDados"+cont+"-"+Variaveis.data_part
+".txt";
if(nummaq!=null)
{
if(!File.Exists(ficheiro))
{
using (StreamWriter sw = new StreamWriter(ficheiro))
{
sw.Flush();
sw.Write(NumMaq+",");
foreach(byte b in rxbuffer)
sw.Write(b+",");
sw.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part+" ,
"+
System.DateTime.Now.ToString("hh:mm:ss.fff")+" ,
"+prta.InBufferCount+" , "+relsense.Enabled);
sw.WriteLine();

sw.Flush();
sw.Write(NumMaq+",");
foreach(byte b in rxbuffer)
sw.Write(b+",");
sw.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part+" ,
"+
System.DateTime.Now.ToString("hh:mm:ss.fff")+" ,
"+prta.InBufferCount+" , "+relsense.Enabled);
sw.WriteLine();
}
}
else if (File.Exists(ficheiro))
{
using (StreamWriter s_w = File.AppendText(ficheiro))
{
s_w.Flush();
s_w.Write(NumMaq+",");
foreach(byte b in rxbuffer)
s_w.Write(b+",");
s_w.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part+" ,
"+
System.DateTime.Now.ToString("hh:mm:ss.fff")+" ,
"+prta.InBufferCount+" , "+relsense.Enabled);
s_w.WriteLine();

s_w.Flush();
s_w.Write(NumMaq+",");
foreach(byte b in rxbuffer)
s_w.Write(b+",");
s_w.Write(" , "+rxbuffer.Length+" , " + Variaveis.data_part+" ,
"+
System.DateTime.Now.ToString("hh:mm:ss.fff")
+" ,"+prta.InBufferCount+" , "+relsense.Enabled);
s_w.WriteLine();

}
}
}
}
private void log_num()
{
string ficheiro=@"Backup\LogDados"+cont+"-"+Variaveis.data_part
+".txt";
if(!File.Exists(ficheiro))
{
using (StreamWriter sw = new StreamWriter(ficheiro))
{
sw.Flush();
sw.Write(NumMaq+",");
sw.WriteLine();
}
}
else if (File.Exists(ficheiro))
{
using (StreamWriter s_w = File.AppendText(ficheiro))
{
s_w.Flush();
s_w.Write(NumMaq+",");
s_w.WriteLine();

}
}
}
private void LogSense()
{
string ficheiro=@"Backup\LogSense"+cont+"-"+Variaveis.data_part
+".txt";
if(!File.Exists(ficheiro))
{
using (StreamWriter sw = new StreamWriter(ficheiro))
{
sw.Flush();
sw.Write(cont+" ,
"+System.DateTime.Now.ToString("hh:mm:ss.fff"));
sw.WriteLine();
}
}
else if (File.Exists(ficheiro))
{
using (StreamWriter s_w = File.AppendText(ficheiro))
{
s_w.Flush();
s_w.Write(cont+" ,
"+System.DateTime.Now.ToString("hh:mm:ss.fff"));
s_w.WriteLine();

}
}
}
private void LogEscreveSense(byte info)
{
string ficheiro=@"Backup\LogEscreveSense"+cont
+"-"+Variaveis.data_part+".txt";
if(!File.Exists(ficheiro))
{
using (StreamWriter sw = new StreamWriter(ficheiro))
{
sw.Flush();
sw.Write(cont+" ,
"+System.DateTime.Now.ToString("hh:mm:ss.fff"));
sw.WriteLine();
}
}
else if (File.Exists(ficheiro))
{
using (StreamWriter s_w = File.AppendText(ficheiro))
{
s_w.Flush();
s_w.Write(cont+" ,
"+System.DateTime.Now.ToString("hh:mm:ss.fff"));
s_w.WriteLine();

}
}
}
#endregion
#region Propriedades
/// <summary>
/// indica se existem rejeições
/// </summary>
public bool Rej
{
get
{
return rej;
}
set
{
rej=value;
}
}

/// <summary>
/// indica se houve erro
/// </summary>
public bool Erro
{
get
{
return erro;
}
set
{
erro=value;
}
}

/// <summary>
/// indica se já pode gravar
/// </summary>
public bool PGravar
{
get
{
return pgravar;
}
set
{
pgravar=value;
}
}

/// <summary>
/// indica que vai gravar depois de poder gravar
/// </summary>
public bool Grava
{
get
{
return grava;
}
set
{
grava=value;
}
}

/// <summary>
/// indica se já houve tratamento de dados
/// </summary>
public bool Tratou
{
get
{
return tratou;
}
set
{
tratou=value;
}
}
/// <summary>
/// indica se a porta serie esta aberta
/// </summary>
public bool Aberta
{
get
{
return aberta;
}
set
{
aberta=value;
}
}
/// <summary>
/// guarda o numero da maquina
/// </summary>
public string NumMaq
{
get
{
return nummaq;
}
set
{
nummaq=value;
}
}
/// <summary>
/// indica se está a contar
/// </summary>
public bool ACnt
{
get
{
return acnt;
}
set
{
acnt=value;
}
}
/// <summary>
/// indica se pode ler o numero da maquina
/// </summary>
public bool PodeLer
{
get
{
return pode_ler;
}
set
{
pode_ler=value;
}
}
/// <summary>
/// activa a valor contado de 5 euros
/// </summary>
private string Cinco
{
get
{
return cinco;
}
set
{
cinco=value;
}
}
/// <summary>
/// activa a valor contado de 10 euros
/// </summary>
private string Dez
{
get
{
return dez;
}
set
{
dez=value;
}
}

/// <summary>
/// activa a valor contado de 20 euros
/// </summary>
private string Vinte
{
get
{
return vinte;
}
set
{
vinte=value;
}
}

/// <summary>
/// activa a valor contado de 50 euros
/// </summary>
private string Cinq
{
get
{
return cinq;
}
set
{
cinq=value;
}
}

/// <summary>
/// activa a valor contado de 100 euros
/// </summary>
private string Cem
{
get
{
return cem;
}
set
{
cem=value;
}
}

/// <summary>
/// activa a valor contado de 200 euros
/// </summary>
private string Duz
{
get
{
return duz;
}
set
{
duz=value;
}
}

/// <summary>
/// activa a valor contado de 500 euros
/// </summary>
private string Quinh
{
get
{
return quinh;
}
set
{
quinh=value;
}
}

/// <summary>
/// passa o valor de cinco em string para inteiro
/// </summary>
private ulong CincoDec
{
get
{
return cinco_dec;
}
set
{
cinco_dec=value;
}
}
/// <summary>
/// passa o valor de dez em string para inteiro
/// </summary>
private ulong DezDec
{
get
{
return dez_dec;
}
set
{
dez_dec=value;
}
}
/// <summary>
/// passa o valor de vinte em string para inteiro
/// </summary>
private ulong VinteDec
{
get
{
return vinte_dec;
}
set
{
vinte_dec=value;
}
}
/// <summary>
/// passa o valor de cinquenta em string para inteiro
/// </summary>
private ulong CinqDec
{
get
{
return cinq_dec;
}
set
{
cinq_dec=value;
}
}
/// <summary>
/// passa o valor de cem em string para inteiro
/// </summary>
private ulong CemDec
{
get
{
return cem_dec;
}
set
{
cem_dec=value;
}
}
/// <summary>
/// passa o valor de duzentos em string para inteiro
/// </summary>
private ulong DuzDec
{
get
{
return duz_dec;
}
set
{
duz_dec=value;
}
}
/// <summary>
/// passa o valor de quinhentos em string para inteiro
/// </summary>
private ulong QuinhDec
{
get
{
return quinh_dec;
}
set
{
quinh_dec=value;
}
}

#endregion
}
}
Joaquim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top