在做用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) {}

解决方案 »

  1.   

    你硬件的通信协议对吗/
     
      还有发送十六进制的可以用DateOutputStream和DateInputStream
      

  2.   

    我之前开发识币器时也是通信协议错误
    sport.setSerialPortParams(9600,
                    sport.DATABITS_8,
                    sport.STOPBITS_1,
                    sport.PARITY_NONE);
    这个看清楚,硬件的产商应该有给你一个协议把
      

  3.   

    有的,上面的協議我在vb上通過了,就是java不行啊
      

  4.   

    ^_^偶进来接分了啊。希望你的测试OK。有事再MSN吧。等等高手,也许还有其他solution
      

  5.   

    我做过
    我的协议:
    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); }
      

  6.   

    其實我的那個指令是十六進制的﹐放在串口調試器上是可以取到數據的﹐它的指令是02 39 39 39 30 30 30 60 30 30 30 30 03 36 38,輸入這些指令﹐就可以從考勤機上取出數據﹐但就不知用java該如何實現.我的vb的代碼如下:
     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
      

  7.   

    '**********************************
    '字元表示的十六進位數轉化為相應的整數
    '錯誤則返回  -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
      

  8.   

    幫我看看﹐用java應該怎樣做的啊
      

  9.   

    我做过
    我的协议:
    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);
      

  10.   

    串口不是很难,如果协议没有问题应该可以做到。有的设备传输的是ASCII,就更简单鳓。
      

  11.   

    不会java呀,帮不了忙,还不如不说呀
      

  12.   

    如果串口正常连接.先将需要发送的数组转换成十六进制在发送
     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();
        }