在做用java寫的考勤程序﹐但現在不知怎樣發送十六進制的指令﹐我寫的代碼如下﹐但不會出數據﹐各位﹐有做過的幫我看看是什么問題﹐非常感謝!
try {
sport.setSerialPortParams(9600,
sport.DATABITS_8,
sport.STOPBITS_1,
sport.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try{
byte send_com[]=new byte[15];
send_com[0]=2;
send_com[1]=57;
send_com[2]=57;
send_com[3]=57;
send_com[4]=48;
send_com[5]=48;
send_com[6]=48;
send_com[7]=96;
send_com[8]=48;
send_com[9]=48;
send_com[10]=48;
send_com[11]=48;
send_com[12]=3;
send_com[13]=54;
send_com[14]=56; os.write(send_com);
}
catch (IOException e) {}
try {
sport.setSerialPortParams(9600,
sport.DATABITS_8,
sport.STOPBITS_1,
sport.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try{
byte send_com[]=new byte[15];
send_com[0]=2;
send_com[1]=57;
send_com[2]=57;
send_com[3]=57;
send_com[4]=48;
send_com[5]=48;
send_com[6]=48;
send_com[7]=96;
send_com[8]=48;
send_com[9]=48;
send_com[10]=48;
send_com[11]=48;
send_com[12]=3;
send_com[13]=54;
send_com[14]=56; os.write(send_com);
}
catch (IOException e) {}
还有发送十六进制的可以用DateOutputStream和DateInputStream
sport.setSerialPortParams(9600,
sport.DATABITS_8,
sport.STOPBITS_1,
sport.PARITY_NONE);
这个看清楚,硬件的产商应该有给你一个协议把
我的协议:
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);然后:
/**
* 给串口发送一个指令(数据是一个长整型)
*/
public void writeCom_long(long msg) throws IOException { OutputStream outputStream;
DataOutputStream dos;
outputStream = serialPort.getOutputStream();
dos = new DataOutputStream(outputStream);
dos.writeLong(msg); }
m_mscomm.InputLen = 0
m_mscomm.InputMode = 0
m_mscomm.InBufferCount = 0
m_mscomm.RThreshold = 1
longth = strHexToByteArray("02 39 39 39 30 30 30 60 30 30 30 30 03 36 38", bytSendByte())
If longth > 0 Then
m_mscomm.Output = bytSendByte
End If
'字元表示的十六進位數轉化為相應的整數
'錯誤則返回 -1
'**********************************Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出錯資訊
End If
ConvertHexChr = test
End Function'**********************************
'字串表示的十六進位資料轉化為相應的位元組串
'返回轉化後的位元組數
'**********************************Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六進位(二進位)資料位元組對應值
Dim hstr As String * 1 '高位字元
Dim lstr As String * 1 '低位元字元
Dim HighHexData As Integer '高位數值
Dim LowHexData As Integer '低位數值
Dim HexDataLen As Integer '位元組數
Dim StringLen As Integer '字串長度
Dim Account As Integer '計數
strTestn = "" '設初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字元中斷轉化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最後一次迴圈改變的數值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,則不會進入循環體
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
我的协议:
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);然后:
/**
* 给串口发送一个指令(数据是一个长整型)
*/
public void writeCom_long(long msg) throws IOException { OutputStream outputStream;
DataOutputStream dos;
outputStream = serialPort.getOutputStream();
dos = new DataOutputStream(outputStream);
dos.writeLong(msg); }我这个直接可以发送十六进制的阿
比如发送一个reset的指令就是writeCom_long(Ox30);
public static String byte2Hex(byte[] b) {
String hex = "", tmp="";
for (int n=0; n<b.length; n++) {
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) hex = hex + "0" + tmp;
else hex = hex + tmp;
if (n < b.length - 1) hex = hex + ":";
}
return hex.toUpperCase();
}