Click here to Skip to main content
15,896,557 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: serial port CreateFile in c# Pin
OriginalGriff29-Dec-14 3:42
mveOriginalGriff29-Dec-14 3:42 
Questionusing SqlConncetion Pin
Jassim Rahma29-Dec-14 1:16
Jassim Rahma29-Dec-14 1:16 
AnswerRe: using SqlConncetion Pin
OriginalGriff29-Dec-14 2:02
mveOriginalGriff29-Dec-14 2:02 
AnswerRe: using SqlConncetion Pin
Dominic Burford30-Dec-14 5:52
professionalDominic Burford30-Dec-14 5:52 
QuestionObj.Orientation structure newbie/intermediate question Pin
BlindNavigator29-Dec-14 0:04
BlindNavigator29-Dec-14 0:04 
AnswerRe: Obj.Orientation structure newbie/intermediate question Pin
Pete O'Hanlon29-Dec-14 4:45
mvePete O'Hanlon29-Dec-14 4:45 
GeneralRe: Obj.Orientation structure newbie/intermediate question Pin
BlindNavigator29-Dec-14 5:37
BlindNavigator29-Dec-14 5:37 
AnswerRe: Obj.Orientation structure newbie/intermediate question Pin
BillWoodruff29-Dec-14 5:17
professionalBillWoodruff29-Dec-14 5:17 
QuestionWorkFlow Pin
Zeyad Jalil28-Dec-14 20:58
professionalZeyad Jalil28-Dec-14 20:58 
AnswerRe: WorkFlow Pin
Pete O'Hanlon28-Dec-14 21:21
mvePete O'Hanlon28-Dec-14 21:21 
QuestionCreate diagonal in the DevExpress XtraReport Pin
nhanlaptrinh28-Dec-14 1:42
nhanlaptrinh28-Dec-14 1:42 
AnswerRe: Create diagonal in the DevExpress XtraReport Pin
Richard Andrew x6428-Dec-14 10:41
professionalRichard Andrew x6428-Dec-14 10:41 
GeneralRe: Create diagonal in the DevExpress XtraReport Pin
nhanlaptrinh29-Dec-14 3:59
nhanlaptrinh29-Dec-14 3:59 
GeneralRe: Create diagonal in the DevExpress XtraReport Pin
Pete O'Hanlon29-Dec-14 4:38
mvePete O'Hanlon29-Dec-14 4:38 

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.