MCU single-chip C language to send data to the serial port, PC Java language to obtain port data

Source: Internet
Author: User

Environment:

Windows7 64B,JDK64B,MYECLIPSE8.5,RXTX Development Package, Stc,keil, Geshe Beacon, 51 single-chip microcomputer, RS232USB to serial line.

Lower machine C code # include <reg51.h>
#include <string.h>
#define Inbuf_len 7//Data length
unsigned char Inbuf1[inbuf_len];
unsigned char checksum,count3,count=0;
Bit read_flag=0;
unsigned char com[]= "hello\n";
void Init_serialcomm (void)
{
tmod=0x20;//setting timer 1 for Mode 2
th1=0xfd;//set initial value setting baud rate
TL1=0XFD;
tr1=1;//Start Timer
sm0=0;//Serial Communication Mode setting
Sm1=1;
}
void Delay_1s ()//1s
{
unsigned int i;
for (i=0;i<45000;i++)
{
}
}


void Delay_1us ()//1s
{
unsigned int i;
for (i=0;i<45;i++)
{
}
}


Send a character to the serial port
void send_char_com (char ch)
{
Sbuf=ch;
while (ti==0);
ti=0;
Delay_1us ();
}


Sends a string to the serial port, strlen the string length
void send_string_com (char *str,int strlen)
{
int k=0;
Do
{
Send_char_com (* (str + k));
k++;
} while (K < strlen);
}


Serial port receive interrupt function
void serial () Interrupt 4 using 3
{
if (RI)
{
unsigned char ch1;
RI = 0;


Ch1=sbuf;
Inbuf1[count++]=ch1;
if (Count==inbuf_len)
{
read_flag=1; If the serial port receives the data to reach Inbuf_len, and the checksum is correct,
count=0; Set the number of digits flag
}


}
}


Main ()
{
Init_serialcomm (); Initialize the serial port
Delay_1s ();
Send_string_com ("10.7", 4);
return 1;


}



Upper computer Java code import gnu.io.CommPort;
Import Gnu.io.CommPortIdentifier;
Import Gnu.io.SerialPort;


Import Java.io.FileDescriptor;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;


Import java.sql.*;


public class Twowayserialcomm
{
Public Twowayserialcomm ()
{
Super ();
}

void Connect (String portname) throws Exception
{
Commportidentifier portidentifier = Commportidentifier.getportidentifier (portname);

if (portidentifier.iscurrentlyowned ())
{
System.out.println ("Error:port is currently on use");
}
Else
{
Commport Commport = Portidentifier.open (This.getclass (). GetName (), 2000);

if (Commport instanceof SerialPort)
{
SerialPort SerialPort = (SerialPort) commport;
Serialport.setserialportparams (9600,serialport.databits_8,serialport.stopbits_1,serialport.parity_none);

InputStream in = Serialport.getinputstream ();
OutputStream out = Serialport.getoutputstream ();

(New Thread (New Serialreader (in)). Start ();
(New Thread (new Serialwriter)). Start ();


}
Else
{
SYSTEM.OUT.PRINTLN ("Error:only serial ports is handled by this example.");
}
}
}

/** */
public static class Serialreader implements Runnable
{
InputStream in;

Public Serialreader (InputStream in)
{
This.in = in;
}

public void Run ()
{
byte[] buffer = new byte[1024];
int len =-1;
String com_get= "";
Dbinsert di=new Dbinsert ();
Try
{
while (len = this.in.read (buffer)) >-1)
{
10 seconds delay
try {
Thread.Sleep (10000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Com_get Saving serial data
Com_get=new String (buffer,0,2);

System.out.print (Com_get);

}
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}


/** */
public static class Serialwriter implements Runnable
{
OutputStream out;

Public Serialwriter (OutputStream out)
{
This.out = out;
}

public void Run ()
{
Try
{
int c = 0;
while ((c = System.in.read ()) >-1)
{
This.out.write (c);
}
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}

public static void Main (string[] args)
{
Try
{
(New Twowayserialcomm ()). Connect ("COM3");
}
catch (Exception e)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
}
The problem that may arise is garbled

It could be a switch button that doesn't turn on the microcontroller.

Prompt Open method Error

May not be closed Geshe beacon or myeclipse, both can not be opened at the same time.

Hint that the serial port is not found

Check your STC and code to see if the port is correct.

MCU single-chip C language to send data to the serial port, PC Java language to obtain port data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.