LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

serial communication with C

I am having trouble communicating to the serial port using C and labview. I used the standard HANDLE for serial communication and I used the CALL LIBRARY NODE found in labview.  I created error messages to help me find out where the errors are, but the thing is that although my serial port is not connected to anything it still reads "Serial port successfully reconfigured." Help!
 

 BOOL fSuccess;
 char *pcCommPort = Commport;
 hCom = CreateFile(pcCommPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
 if (hCom == INVALID_HANDLE_VALUE){
  return "CreateFile failed!";
 }
 fSuccess = GetCommState(hCom,&dcb);
 if(!fSuccess)
 {
  return "GetCommState failed.";
 }
 dcb.BaudRate = 38400;
 dcb.ByteSize = 8;
 dcb.Parity = NOPARITY;
 dcb.StopBits = ONESTOPBIT;
 fSuccess = SetCommState(hCom,&dcb);
 if(!fSuccess)
 {
  return "SetCommState failed.";
 }
 return "Serial port successfully reconfigured.";
0 Kudos
Message 1 of 9
(3,607 Views)
I used Microsoft Visual C++ to compile it.
0 Kudos
Message 2 of 9
(3,600 Views)
If I understand correctly, you expected the call to fail. But you do not need an actual device connected to the port in order to configue the port itself. Thus you may be in fact configuring the port correctly. Try setting one of the parameters to an invalid value, like a ridulously high baud rate, and see if the configuration fails.
~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 Kudos
Message 3 of 9
(3,594 Views)
I sent the configuration to the serial port and I was expecting a false back since it is not hooked up to anything but instead i am receiving a true and when i do hook it up and try to send a command i get back what i just sent!
0 Kudos
Message 4 of 9
(3,591 Views)

As AnalogKid2DigitalMan said you do not need for anything to be connected to the serial port in order for the serial port to be configured.  Only when you try to write to the serial port or read from the serial port will you get an error.  If you get an error during configuration that means that the serial port is not present or that one of the settings is invalid, not that there is not a device connected to the serial port.

By the way, why are you using C to communicate over the serial port from LabVIEW?  Serial communication is very easy using the VISA drivers in LabVIEW.

Message Edited by John Rich on 07-29-2005 02:33 PM

0 Kudos
Message 5 of 9
(3,582 Views)

I am trying to put an extern "C" that i found but C++ is giving me errors because of it

extern "C" _declspec(dllexport) char *OpenSerial(char *Commport);
extern "C" _declspec(dllexport) void send_command(char cmd);
extern "C" _declspec(dllexport) char *CloseSerial(void);

0 Kudos
Message 6 of 9
(3,581 Views)
I am trying to access external software using the .dll file. When I write to a port or read from the port, I get back the same thing I put in. Let's say i hardcode 'Erase.' I should get back something like 'the Device is empty', but all i get back is 'Erase' again. Since its not connected to anything, I inputted an error like 'Error: could not erase'. It doesn't even to that.
0 Kudos
Message 7 of 9
(3,576 Views)
ECHO ON, possibly?
~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 Kudos
Message 8 of 9
(3,573 Views)

HERE is my erase code:

 DWORD iBytesWritten;
 DWORD iBytesRead;
 BOOL bWriteRC;
 BOOL bReadRC;
 char *sBuffer;

 bWriteRC = WriteFile(a,"E\r",2,&iBytesWritten,NULL);
 if (bWriteRC){
    ;    //do nothing
 }
 else
  return "Cannot Erase the Flash";
 bReadRC = ReadFile(a,&sBuffer,20,&iBytesRead,NULL);
 if (bReadRC){
  return sBuffer;    //do nothing
 }
 else
  return "Cannot Read the Flash";

0 Kudos
Message 9 of 9
(3,570 Views)