0 out of 2 rated this helpful - Rate this topic

SerialPort.Write Method

Writes data to the serial port output buffer.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method Write(String) Writes the specified string to the serial port.
Public method Write(Byte[], Int32, Int32) Writes a specified number of bytes to the serial port using data from a buffer.
Public method Write(Char[], Int32, Int32) Writes a specified number of characters to the serial port using data from a buffer.
Top
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
unable to send integer through serial port
$0sir,$0 $0this is my sample program written in c++ (Visual Studio 2008 IDE). My problem is that there is no error in the code but there is problem with the command passed. It is not passed as integer but as string. so the irobo could not recognize the command.$0 $0can you suggest me some alternatives or correction in my code.$0 $0$0 $0 $0/* the code follows*/$0 $0$0 $0 $0//This example implements serial communication$0 $0//with an arduino microcontroller on COM4 and the$0 $0//Picasso robot on COM5, your ports will vary:$0 $0//#include<stdafx.h>$0 $0#include <stdio.h>$0 $0#include<stdlib.h>$0 $0#using <System.dll>$0 $0using namespace System;$0 $0using namespace System::IO::Ports;$0 $0$0 $0 $0SerialPort^ OpenSerialPort(System::String ^port, int baud);$0 $0void CloseSerialPorts(SerialPort ^Port);$0 $0void TurnOnRobotServos(SerialPort ^RBPort);$0 $0$0 $0 $0SerialPort^ OpenSerialPort(System::String ^port, int baud)$0 $0{$0 $0SerialPort^ newport;$0 $0try$0 $0{$0 $0newport = gcnew SerialPort(port, baud);$0 $0newport->Open();$0 $0}$0 $0    catch (IO::IOException^ e  )$0 $0{$0 $0Console::WriteLine(e->GetType()->Name+": Port is not ready");$0 $0}$0 $0catch (ArgumentException^ e)$0 $0{$0 $0Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");$0 $0}$0 $0return newport;$0 $0}$0 $0$0 $0 $0void TurnOnRobotServos(SerialPort ^RBPort)$0 $0{$0 $0char* msg="128 132 137 0 100 128 0";$0 $0$0 $0RBPort->Write(msg.toString());$0 $0}$0 $0$0 $0 $0void CloseSerialPorts(SerialPort ^Port)$0 $0{$0 $0Port->Close();$0 $0}$0 $0$0 $0 $0int main(array<System::String ^> ^args)$0 $0{$0 $0SerialPort^ Robot;$0 $0System::String^ RBPort = "COM1";$0 $0int RBbaud = 57600;$0 $0int ardbaud = 57600;$0 $0printf("done");$0 $0Robot = OpenSerialPort(RBPort,RBbaud);$0 $0TurnOnRobotServos(Robot);$0 $0$0 $0$0 $0$0 $0//CloseSerialPorts(ard);$0 $0CloseSerialPorts(Robot);$0 $0printf("exit");$0 $0while(1);$0 $0return 0;$0 $0}$0 $0$0 $0 $0Thanks in advance$0