Jump to content

Recommended Posts

Posted (edited)

Boa tarde.

Estou a desenvolver uma Windows App em que comunica com um telefone enviando-lhe comandos AT e apresento o resulado do comando enviado numa textbox. Parece uma coisa simples mas não estou a conseguir ler o resultado do comando AT. Alguém me sabe dizer o que está mal no código? Tenho um botao que procura as portas disponiveis, outro para abrir a conexao, outro para fechar a conexao e mais um para enviar o comando AT. Tenho uma textbox vazia em que quando envio o comando AT vai apresentar o resultado nessa textbox. Desde já um muito obrigado.

public partial class Form1 : Form
{
 SerialPort sp;
 public Form1()
 {
	 InitializeComponent();
 }
 private void btSeacrh_Click(object sender, EventArgs e)
 {
	 string[] ports = SerialPort.GetPortNames();
	 foreach (string port in ports)
	 {
		 comboBoxPorts.Items.Add(port);
	 }
 }
 private void btSend_Click(object sender, System.EventArgs e)
 {
	 sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
	 string data = tbEnviarDados.Text.ToString();
	 sp.Write(data);
 }
 private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
 {
	 SerialPort sp = (SerialPort)sender;
	 string dados = sp.ReadExisting();
	 tbDadosRecebidos.Text = dados;
 }
 private void btAbrirConexao_Click(object sender, System.EventArgs e)
 {
	 string PortName = comboBoxPorts.Text.ToString();
	 sp = new SerialPort(PortName, 115200, Parity.None, 8, StopBits.One);
	 sp.Open();
 }
 private void btFecharConexao_Click(object sender, System.EventArgs e)
 {
	 sp.Close();
 }
}
Edited by Caça
GeSHi
  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.