7 years, 3 months ago.

Communicate with programming languages running on the host PC that can communicate with a serialport

I want to send a serial string with a c ++ program, after that, this string must be received by the loaded program on the mbed lpc1768 and print this string. This is the program to send the string to mbed lpc1768:

  1. include "windows.h"
  1. include "string.h"
  1. include <iostream>
  1. include <tchar.h>
  1. include <stdint.h>

using namespace std;

HANDLE SerialPort;

char stringa[200]; stringa di ingresso

void Insert()

{ printf("\nInserire stringa: ");

scanf("%s", stringa);

printf("\nStringa inserita con successo\n");

}

int Open () {

DCB dcb;

BOOL fSuccess;

TCHAR *pcCommPort=TEXT((TCHAR*)"COM4");

SerialPort=CreateFile(pcCommPort,GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (SerialPort == INVALID_HANDLE_VALUE){

printf("CreateFile failed with error %d.\n", GetLastError());

return (1);

}

SecureZeroMemory(&dcb, sizeof(DCB));

dcb.DCBlength = sizeof(DCB);

fSuccess = GetCommState(SerialPort, &dcb);

if (!fSuccess)

{

printf ("GetCommState failed with error %d.\n", GetLastError());

return(2);

}

115200 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_115200; baud rate

dcb.ByteSize = 8; data size, xmit and rcv

dcb.Parity = NOPARITY; parity bit

dcb.StopBits = ONESTOPBIT; stop bit

fSuccess = SetCommState(SerialPort, &dcb);

if (!fSuccess)

{

printf ("SetCommState failed with error %d.\n", GetLastError());

return(3);

}

fSuccess = GetCommState(SerialPort, &dcb);

if (!fSuccess)

{

printf ("GetCommState failed with error %d.\n", GetLastError());

return(2);

}

printf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);

return(0);

}

void Write() {

BOOL write;

DWORD written=0;

count=strlen(stringa);

write=WriteFile(SerialPort, stringa, count, &written, NULL);

if(write){

cout<<"\n\n"<<written;

}

}

int main() {

Open();

Insert();

Write();

}

Now how do I read the string through a program loaded on the mbed lpc1768?

1 Answer

7 years, 3 months ago.

Please use <<code>> and <</code>> tags on separate lines around your posted code to keep it readable.

Follow the serial port manual here. Basically you have to make sure that for the LPC1768 the mbed serial driver is installed on your PC, you will then see a new USB Com port appear on your PC whenever the mbed USB cable is connected to your PC, note the COM port number. Test the USB com port by opening it in Hyperterm. When that works, the same serial port must be selected in your own PC C++ code through the dcb struct, make sure the baudrate is the same as on the mbed side (default 9600).

Accepted Answer

Thank you for the reply. Ok is the C ++ program that opens the serialport and sends me the string to the mbed. But I can not do it though is the program on the mbed compiler to receive the string and print it. Which instructions should I use?

posted by Thomas Villacci 18 Aug 2017

Now how do I read the string through a program loaded on the mbed lpc1768?

This is the code in c++ :

#include "windows.h"

#include "string.h"
#include "iostream"
#include "tchar.h"
#include "stdint.h"


using namespace std;

HANDLE SerialPort;

char stringa[]="a";  //stringa di ingresso

int count=0;

void Insert()
{     
        printf("\nInserire stringa: ");
        scanf("%s", stringa);
        printf("\nStringa inserita con successo\n");

}

int Open () {
	
	DCB dcb;
    BOOL fSuccess;
    TCHAR *pcCommPort=TEXT((TCHAR*)"COM4"); 
     
    SerialPort=CreateFile(pcCommPort,GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    if (SerialPort == INVALID_HANDLE_VALUE){
	printf("CreateFile failed with error %d.\n", GetLastError());
	return (1);
}

   SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);
   
   fSuccess = GetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return(2);
   }
   
   //  115200 bps, 8 data bits, no parity, and 1 stop bit.
   dcb.BaudRate = CBR_115200;     //  baud rate
   dcb.ByteSize = 8;             //  data size, xmit and rcv
   dcb.Parity   = NOPARITY;      //  parity bit
   dcb.StopBits = ONESTOPBIT;    //  stop bit
   
   fSuccess = SetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return(3);
   }
   
   fSuccess = GetCommState(SerialPort, &dcb);

   if (!fSuccess) 
   {
      
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return(2);
   }
   
    printf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
    return(0);
}

void Write() {	
	BOOL write;
	DWORD written=0;
	count=strlen(stringa);
	write=WriteFile(SerialPort, stringa, count, &written, NULL);
	if(write){
		cout<<"\n\n"<<written;
	}	
}    
int main() {
	
	Open();
	Insert();
	Write();	
}

posted by Thomas Villacci 18 Aug 2017

First make sure the mbed side can read from its serial port by using a simple hyperterminal on the PC side to check communications. The mbed example code to read from serial is given in the API manual. You can use pc.getc() or pc.scanf(..) just like on any other C platform.

...
        if(pc.readable()) {
            ch = pc.getc());  // read one char from serial
        }
....
posted by Wim Huiskamp 18 Aug 2017

I used this program to read the string. But he only reads the first 16 characters, why?

#include "mbed.h"
Serial  pc(USBTX, USBRX);
char stringa[200];
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
 
void dev_recv()
{   
    int j=0;
    //char stringa[200];
    //led1 = !led1;
    while(pc.readable()) {
        
        stringa[j] = pc.getc();
        pc.putc(stringa[j]);
        
        
         }
         printf(stringa);

    
}

int main()
{
    pc.baud(115200);
    pc.attach(&dev_recv, Serial::RxIrq);
    while(1) {
        sleep();
    }
}
posted by Thomas Villacci 19 Aug 2017

You are not incrementing index ''j'' so all received chars end up in stringa[0] position. Also note that printf expects a 0 (zero) value at the end of the string that it should print. The zero indicates the last character. Make sure to insert that 0 in the array stringa following the final char that you store. There could also be some other issue with the code being too slow due to the putc() of each received char. Try to remove that and only print the complete string. The LPC1768 has a serial port FIFO that holds 16 chars. When more data is received and the code is too slow to empty the FIFO it will lose some characters.

posted by Wim Huiskamp 19 Aug 2017

I tried to increase the index j and only reads the first 16 characters. I also tried to remove the putc () function, so I did not print on the video either the first 16 characters. Why? Can you write the correct code? To view the entire string. thank you so much

posted by Thomas Villacci 20 Aug 2017