新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Java軟件與單片機串口通信

Java軟件與單片機串口通信

作者: 時間:2016-11-23 來源:網(wǎng)絡(luò) 收藏
從菜鳥級的電子愛好者到骨灰級的電子工程師,相信大多數(shù)人都經(jīng)歷過制作萬年歷,畢竟它比較全面的考察對單片機基礎(chǔ)知識的掌握,并且體現(xiàn)了C語言編程模塊化的思想。

本文引用地址:http://butianyuan.cn/article/201611/320540.htm

而本文旨在介紹在完成制作萬年歷后實現(xiàn)軟件通過RS-232串口對萬年歷進行控制。軟件的編寫采用的Java語言,當(dāng)然,使用C++、VB等語言也可以編寫出與單片機串口通信的軟件,使用VB的MSCOMM控件可以更方便的實現(xiàn)串口通信。

Java軟件方面

需要用到Comm包,下載地址和使用方法請自行Google一下。

部分代碼:

1.找出電腦上的串口

static Enumeration portList=CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()){ //用循環(huán)結(jié)構(gòu)找出串口
portId=(CommPortIdentifier)portList.nextElement(); //強制轉(zhuǎn)換為通訊端口類型
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
System.out.println(portId.getName());

2.打開串口

SerialPort serialPort;//RS-232串口

try {
serialPort = (SerialPort) portId.open("ReadComm", 2000); //超時等待
}
catch (PortInUseException e) { }

3.設(shè)置串口通訊參數(shù)
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) { }

單片機方面

1.1602液晶時序圖

2.protues仿真圖(附Java軟件效果圖)

3.C51部分代碼

void write_com(uchar com)//寫指令
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

void write_date(uchar date)//寫數(shù)據(jù)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar num;
lcden=0;
write_com(0x38);//初始化
write_com(0x0c);//不顯示光標(biāo)
write_com(0x06);//指針自加一,整屏顯示不移動
write_com(0x01);//清屏
write_com(0x80);//設(shè)置光標(biāo)指針(第一行)
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);//設(shè)置光標(biāo)指針(第2行)
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x21; // 定時器0的方式1,定時器1的方式2
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //開總中斷
ET0=1;
TR0=1;
TH1=0xfd;//波特率9600
TL1=0xfd;
TR1=1;
}
void initser()
{
SM0=0;
SM1=1;//串口中斷方式1

ES=1;
REN=1;
}

4.標(biāo)準(zhǔn)字符庫

軟件控制單片機

點擊軟件相應(yīng)的三個按鈕(add、Shift、minus)發(fā)送對應(yīng)的byte數(shù)據(jù)(0、1、2),單片機接收到byte數(shù)據(jù)后執(zhí)行相應(yīng)的命令(加數(shù)、切換、減數(shù))。



關(guān)鍵詞: Java軟件單片機串口通

評論


技術(shù)專區(qū)

關(guān)閉