You are on page 1of 4

private void buttonStart_Click(object sender, EventArgs e) { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.Open(); if (serialPort1.IsOpen) { buttonStart.Enabled = false; buttonStop.

Enabled = true; textBox1.ReadOnly = false; } }

Once the serialPort1 is open, any incoming serial characters will cause a DataReceived event to fire. Inside the event handler we read all existing characters from the internal serial receive buffer into string RxString. The next part is critical and not obvious. serialPort1 runs in it own separate thread behind the scenes. This thread cannot directly call any functions in the main thread of our application. However, a special function, Invoke( ), will allow it. So we use Invoke to call our DisplayText( ) function. RxString is the global string variable accessable by both threads.
private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); }

Our DisplayText( ) function is simple. We just append the text in RxString to whatever is already in textBox1.
private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(RxString); }

Clicking the Stop button calls buttonStop_Click( ). If serialPort1 is open, we close the port and set the button enables and textBox1 ReadOnly state back to their previous value.
private void buttonStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); buttonStart.Enabled = true; buttonStop.Enabled = false; textBox1.ReadOnly = true; } }

Last but not least, if the application is closed while serialPort1 is open, the port must be closed first or the program will hang.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); }

#include<8051.h> unsigned char str; void init_serial() { TMOD=0x20; TH1=0xfd; SCON=0x50; bit , Receiving on TR1=1; } // Initialize serial port // Mode=2 // 9600 baud // Serial mode=1 ,8-Bit data,1 Stop bit ,1 Start // Start timer

void transmit_data(unsigned char str) through serial port { SBUF=str; while(TI==0); TI=0; } void receive_data() interrupt 4 from RS232 into microcontroller { str=SBUF; RI=0; transmit_data(str); } void main() { init_serial(); IE=0x90; while(1); }

// Function to transmit data // Store data in sbuf // Wait till data transmit

// Function to recieve data serialy // Read sbuf // Transmit to HyperTerminal

// Initialize serial port

Phi c dng lnh sau bn trong Form.Designer.cs th mi nhn c d liu


this.sp2.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.sp2_DatatReceive);

d 3: Hy vit chng trnh 8051 truyn dng ch DienTuMayTinh.Com lin tc vi tc 9600 baud (8 bit d liu, 1 bit Stop) ln my tnh. Li gii: #include<at89x51.h> #include<string.h> void send(unsigned char a); void sendchuoi(char *a); main() { TMOD=0x20; //Khai bo th vin cho 89c51 //Khai bo th vin s dng hm strlen() //khai bo nguyn mu hm gi 1 k t //khai bo nguyn mu hm gi 1 chui //Chng trnh chnh //Chn Timer1, ch 2

TH1=0xFD; SCON=0x50; TR1=1;

//Ci t tc 9600 baud //0101 0000: Chn ch 1, Cho php nhn //Khi ng Timer1

while(1) //Vng lp v hn { sendchuoi("DienTuMayTinh.Com"); //Gi hm gi 1 chui send(10); //Gi du xung dng } } void send(unsigned char a) { SBUF=a; while(TI==0){} TI=0; } void sendchuoi(char *a) { int i,n; n=strlen(a); for(i=0;i<n;i++) { send(a[i]); } } //nh ngha hm gi 1 k t //Ghi 1 byte d liu vo thanh ghi SBUF //vng lp i c truyn TI ln 1 //Xa c truyn TI sau khi truyn xong //nh ngha hm gi 1 chui k t //Khai bo bin cc b s nguyn: i,n //Tnh di ca chui *a, lu vo bin n //Vng lp gi ln lt tng k t ln, //cho n khi ht chui *a (k t th n-1). //Gi hm gi 1 k t.

You might also like