FUNCTION Balanza1( cPortName )
Local idPort , cDcb
Local nComPort := 4 //COM3
Local nTimeOut := 100 //ms
Local lIsOpened := FALSE
Local lIsInit := FALSE
Local nBytesSended := 0
Local nBytesReceived := 0
Local cReceiveBuffer := SPACE(512)
Local cStringToSend := "W<CR>" // este comando le indica a la balzan que devuelva el peso detectado
//To receive
Local cReceivedString := ""
//to COM open
IdPort := OpenComm("COM"+STR(nComPort,1),1024,256)
lIsOpened := (idPort>0)
//to Com Initialize
//lIsInit := HB_ComInit( nComPort, 9600 , "N", 8, 1 )
lIsInit := BuildCommDcb("COM"+STR(nComPort,1)+":9600,n,8,1" , @cDcb)
//Set Port
SetCommState( IdPort, cDcb )
//To send
//nBytesSended := HB_ComSend( nComPort, cStringToSend , , nTimeOut )
nBytesSended := WriteComm( IdPort,cStringToSend) )
//To receive
//nBytesReceived := HB_ComRecv( nComPort, @cReceiveBuffer, , nTimeOut )
nBytesReceived := ReadComm( IdPort,@cReceiveBuffer)
cReceivedString := Left( cReceiveBuffer, nBytesReceived )
? cReceivedString
return nil
carito wrote:Hola Karinha:
Es de marca METTLER TOLEDO , tiene conexion serial via adaptador con cable USB.
La documentacion es mala, nula que proporciona el proveedor. Asi que he estado
con prueba y error, y no hay caso, no lo logro comunicarme con ella.
Gracias por tu consulta.
#pragma BEGINDUMP
#include <windows.h>
#include <stdio.h>
#include <hbapi.h>
HB_FUNC( TEST )
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
const char data_to_send[] = "Hello Arduino!";
DWORD bytes_written;
char buffer[256];
DWORD bytes_read;
// Replace "COM3" with the correct port name for your Arduino
hSerial = CreateFile("COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hSerial == INVALID_HANDLE_VALUE) {
MessageBox( 0, "Error opening serial port", "Error", 0 );
hb_retnl( 1 );
return;
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
MessageBox( 0, " Error getting serial port state", "Error", 0 );
CloseHandle(hSerial);
hb_retnl( 1 );
return;
}
dcbSerialParams.BaudRate = CBR_9600; // Set your baud rate
dcbSerialParams.ByteSize = 8; // 8 data bits
dcbSerialParams.StopBits = ONESTOPBIT; // 1 stop bit
dcbSerialParams.Parity = NOPARITY; // No parity
if (!SetCommState(hSerial, &dcbSerialParams)) {
MessageBox( 0, " Error setting serial port state", "Error", 0 );
CloseHandle(hSerial);
hb_retnl( 1 );
return;
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts)) {
MessageBox( 0, " Error setting timeouts", "Error", 0 );
CloseHandle(hSerial);
hb_retnl( 1 );
return;
}
// Example: Write data to Arduino
if (!WriteFile(hSerial, data_to_send, sizeof(data_to_send) - 1, &bytes_written, NULL)) {
MessageBox( 0, " Error writing to serial port", "Error", 0 );
} else {
MessageBox( 0, "Bytes sent to Arduino", "Error", 0 );
}
// Example: Read data from Arduino
if (!ReadFile(hSerial, buffer, sizeof(buffer) - 1, &bytes_read, NULL)) {
MessageBox( 0, "Error reading from serial port", "Error", 0 );
} else {
buffer[bytes_read] = '\0';
// printf("Received %lu bytes from Arduino: %s\n", bytes_read, buffer);
}
CloseHandle(hSerial);
hb_retnl( 0 );
}
#pragma ENDDUMP
Return to FiveWin para Harbour/xHarbour
Users browsing this forum: Enrico Maria Giordano, Google [Bot] and 40 guests