Click here to Skip to main content
15,920,438 members
Home / Discussions / C#
   

C#

 
GeneralRe: I can not apply Drag Drop in this case Pin
VisualLive30-Dec-14 14:59
VisualLive30-Dec-14 14:59 
QuestionHow to organize this data Pin
turbosupramk329-Dec-14 8:14
turbosupramk329-Dec-14 8:14 
AnswerRe: How to organize this data Pin
Pete O'Hanlon29-Dec-14 8:53
mvePete O'Hanlon29-Dec-14 8:53 
GeneralRe: How to organize this data Pin
PIEBALDconsult29-Dec-14 9:07
mvePIEBALDconsult29-Dec-14 9:07 
AnswerRe: How to organize this data Pin
BillWoodruff29-Dec-14 9:20
professionalBillWoodruff29-Dec-14 9:20 
GeneralRe: How to organize this data Pin
PIEBALDconsult29-Dec-14 10:04
mvePIEBALDconsult29-Dec-14 10:04 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 12:12
professionalBillWoodruff29-Dec-14 12:12 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 10:50
SledgeHammer0129-Dec-14 10:50 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 12:11
professionalBillWoodruff29-Dec-14 12:11 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 12:28
SledgeHammer0129-Dec-14 12:28 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 14:18
professionalBillWoodruff29-Dec-14 14:18 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 14:58
SledgeHammer0129-Dec-14 14:58 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 23:44
professionalBillWoodruff29-Dec-14 23:44 
GeneralRe: How to organize this data Pin
SledgeHammer0130-Dec-14 4:13
SledgeHammer0130-Dec-14 4:13 
GeneralRe: How to organize this data Pin
PIEBALDconsult29-Dec-14 13:07
mvePIEBALDconsult29-Dec-14 13:07 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 23:15
professionalBillWoodruff29-Dec-14 23:15 
GeneralRe: How to organize this data Pin
SledgeHammer0130-Dec-14 4:17
SledgeHammer0130-Dec-14 4:17 
GeneralRe: How to organize this data Pin
BillWoodruff30-Dec-14 23:42
professionalBillWoodruff30-Dec-14 23:42 
GeneralRe: How to organize this data Pin
PIEBALDconsult30-Dec-14 15:29
mvePIEBALDconsult30-Dec-14 15:29 
GeneralRe: How to organize this data Pin
BillWoodruff30-Dec-14 23:53
professionalBillWoodruff30-Dec-14 23:53 
GeneralMessage Closed Pin
29-Dec-14 2:32
Marvic Grima29-Dec-14 2:32 
Questionserial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:16
Member 1114321929-Dec-14 2:16 
Hi all, I am pretty new to coding, and am attempting to port this code from C to c#, and cannot figure out the createfile section. Do i need it? can i replace it with something?

original C.

HANDLE openAndSetupComPort(const TCHAR* comport)
{
	HANDLE com_handle;
	DCB dcb_serial_params = { 0 };
	COMMTIMEOUTS timeouts = { 0 };
	DWORD com_error;
	COMSTAT comstat;

	com_handle = CreateFile(comport,
		GENERIC_READ | GENERIC_WRITE,
		0,
		0,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		0);
	if (com_handle == INVALID_HANDLE_VALUE){
		printf("Error opening port\n");
		return INVALID_HANDLE_VALUE;
	}



	dcb_serial_params.DCBlength = sizeof(dcb_serial_params);
	if (!GetCommState(com_handle, &dcb_serial_params)){
		printf("Failed to get the previous state of the serial port\n");
		CloseHandle(com_handle);
		return INVALID_HANDLE_VALUE;
	}
	dcb_serial_params.BaudRate = 115200; // default baud rate for 3-Space Sensors
	dcb_serial_params.ByteSize = 8;
	dcb_serial_params.StopBits = ONESTOPBIT;
	dcb_serial_params.Parity = NOPARITY;
	if (!SetCommState(com_handle, &dcb_serial_params)){
		printf("Failed to set the serial port's state\n");
	}

	timeouts.ReadIntervalTimeout = -1;
	timeouts.ReadTotalTimeoutConstant = 100;
	timeouts.ReadTotalTimeoutMultiplier = 10;
	timeouts.WriteTotalTimeoutConstant = 100;
	timeouts.WriteTotalTimeoutMultiplier = 10;
	if (!SetCommTimeouts(com_handle, &timeouts)){
		printf("Failed to set the timeouts\n");
		CloseHandle(com_handle);
		return INVALID_HANDLE_VALUE;
	}

	// Flush out any data that was on the line from a previous session
	Sleep(300);
	ClearCommError(com_handle, &com_error, &comstat);
	if (comstat.cbInQue != 0){
		PurgeComm(com_handle, PURGE_RXCLEAR | PURGE_TXCLEAR);
	}

	return com_handle;
}



which is being called here:

C#
if(!WriteFile(com_handle, write_bytes, sizeof(write_bytes), &bytes_written, 0)){
        printf("Error writing to port\n");
        return 2;
    }




So I am opening a serial port:

C#
public static void SerialSetupIMU()
        {
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName = "COM11";
            _serialPort.BaudRate = 115200;
            _serialPort.Parity = Parity.None;
            _serialPort.DataBits = 8;
            _serialPort.StopBits = StopBits.One;
            _serialPort.Handshake = Handshake.None;

            _serialPort.ReadTimeout = 100;
            _serialPort.WriteTimeout = 100;

           _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);


             try
            {
                _serialPort.Open();
                _serialPort.DiscardInBuffer();
                _serialPort.DiscardOutBuffer();
            }

            catch
            {
                Console.Write("port does not exist");
            }

            if (_serialPort.IsOpen)
                Console.Write("port 11 opened");


        }


and attempting to write:

_serialPort.Write(com_handle , write_bytes, byteSize, bytes_written, 0);



And it fails on the 'com_handle', obviously, because I haven't added it. But what is it expecting there?
Any help would be much appreciated!

Thank you much.
AnswerRe: serial port CreateFile in c# Pin
OriginalGriff29-Dec-14 2:28
mveOriginalGriff29-Dec-14 2:28 
GeneralRe: serial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:45
Member 1114321929-Dec-14 2:45 
GeneralRe: serial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:55
Member 1114321929-Dec-14 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.