fbpx

Expresate

Si además quieres enviarnos un Artículo para el Blog y redes sociales, pulsa el siguiente botón:

Programación puerto...
 
Avisos
Vaciar todo

Programación puerto serie

7 Respuestas
4 Usuarios
0 Reactions
3,188 Visitas
jorcoval
Respuestas: 418
Topic starter
(@jorcoval)
Reputable Member
Registrado: hace 18 años

A ver si alguien me puede ayudar a volcar un dato de tipo float por puerto Serie.
He encontrado que la comunicación en C es parecido a la escritura de ficheros, y el código más o menos

#include <stdio>
#include <winsock2>
#include "bluetooth/Include/ws2bth.h"
#include "bluetooth/Include/BluetoothAPIs.h"
#include "math.h"
#include <windows>
#include "bluetooth/Include/bthdef.h"
//#pragma comment(lib, "irprops.lib")

HANDLE m_hComm;

void main(){
m_hComm=CreateFile("COM5",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

if(m_hComm==(HANDLE)-1){printf("errorn");}

DCB dcb;
dcb.DCBlength=sizeof(DCB);
GetCommState(m_hComm,&dcb);
dcb.BaudRate=CBR_9600;
dcb.ByteSize=8;
dcb.Parity=EVENPARITY;
dcb.fParity=TRUE;
dcb.StopBits=ONESTOPBIT;
dcb.fBinary=TRUE;
SetCommState(m_hComm,&dcb);
unsigned char buffer[512];
int datoi=1;
unsigned long enviat=0,enviando=0,leidos=5;

while(1){
WriteFile(m_hComm,&datoi,leidos-enviat,&enviando,NULL);
}
}

Responder
6 respuestas
jorcoval
Respuestas: 418
Topic starter
(@jorcoval)
Reputable Member
Registrado: hace 18 años

¡¡He logrado algo!! Pero eso sí, en VB.
He logrado ya enviar cadenas, ahora estoy tratando de enviar floats porque en el Lego no puedo transformar de string a float (al menos, con el firmware original que ha sido el único que me ha dejado conectar.

Por si os interesa el código:

------CONEXION--------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
With SerialPort1
.PortName = "COM4"
.BaudRate = 96000
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReadTimeout = 300 '300ms
.WriteTimeout = 300 '300ms
End With
SerialPort1.Open()
Label1.Text = "Conectado"
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
--------------------------

--------Envio cadena------
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim byteOut(64) As Byte
Dim i As Integer
Try
byteOut(0) = Len(TextBox1.Text) + 5
byteOut(1) = &H0 '0 para NXT
byteOut(2) = &H80 'no espero respuesta
byteOut(3) = &H9 'Bluetooth
byteOut(4) = &H0 'Box Number - 1
byteOut(5) = Len(TextBox1.Text) + 1
For i = 1 To Len(TextBox1.Text)
byteOut(i + 5) = Asc(Mid(TextBox1.Text, i, 1))
Next
byteOut(Len(TextBox1.Text) + 6) = &H0
SerialPort1.Write(byteOut, 0, Len(TextBox1.Text) + 7)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
----------------------------

Por supuesto, recibo en el Lego sin problemas y muestro la cadena por pantalla.

Responder
drakerdg
Respuestas: 475
(@drakerdg)
Reputable Member
Registrado: hace 19 años

Solo como comentario. En VB es mejor trabajar la comunicación serial utilizando funciones en modo binario, en ves de utilizar Len(), mejor LenB(), en vez de Mid(), usar MidB(), y en vez de Asc(), utiliza AscB(), ya que estas funciones consideran cada bit enviado o recibido byte por byte sin discriminar caracteres no imprimibles del codico ASCII.

Responder
Página 2 / 2
Compartir: