main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net"
  7. "os"
  8. "strings"
  9. "time"
  10. )
  11. func main() {
  12. const brotherPort int = 54921
  13. brotherIP := flag.String("a", "192.168.0.157", "IP address of the Brother scanner")
  14. resolution := flag.Int("r", 300, "Resolution of the scan")
  15. color := flag.String("c", "CGRAY", "Color mode of the scan")
  16. adf := flag.Bool("m", false, "Enable scan of all pages from feeder")
  17. flag.Parse()
  18. if net.ParseIP(*brotherIP) == nil {
  19. HandleError(fmt.Errorf("Invalid IP address: %s", *brotherIP))
  20. }
  21. log.Println("Valid IP address, opening socket...")
  22. socket, err := net.Dial("tcp", fmt.Sprintf("%s:%d", *brotherIP, brotherPort))
  23. HandleError(err)
  24. defer socket.Close()
  25. SendRequest(socket, *resolution, *color, *adf)
  26. }
  27. func SendRequest(socket net.Conn, resolution int, _mode string, adf bool) {
  28. mode, compression := GetCompressionMode(_mode)
  29. log.Println("Reading scanner status...")
  30. status := ReadPacket(socket)[:7]
  31. if "+OK 200" != status {
  32. HandleError(fmt.Errorf("Invalid reply from scanner: %s", status))
  33. }
  34. log.Println("Leasing options...")
  35. request := []byte(fmt.Sprintf("\x1bI\nR=%d,%d\nM=%s\n\x80", resolution, resolution, mode))
  36. SendPacket(socket, request)
  37. offer := ReadPacket(socket)
  38. if adf {
  39. log.Println("Enabling automatic document feeder (ADF)")
  40. request = []byte("\x1bD\nADF\n\x80")
  41. SendPacket(socket, request)
  42. ReadPacket(socket)
  43. }
  44. log.Println("Sending scan request...")
  45. offerOptions := strings.Split(offer, ",")
  46. requestFormat := "\x1bX\nR=%v,%v\nM=%s\nC=%s\nJ=MID\nB=50\nN=50\nA=0,0,%v,%v\n\x80"
  47. request = []byte(fmt.Sprintf(requestFormat, offerOptions[1], offerOptions[1], mode, compression, offerOptions[5], offerOptions[6]))
  48. SendPacket(socket, request)
  49. log.Println("Scanning started...")
  50. }
  51. func GetScan(socket net.Conn) {
  52. log.Println("Getting packets...")
  53. err := socket.SetReadDeadline(time.Now().Add(time.Second * 5))
  54. HandleError(err)
  55. scan := make([]byte, 0)
  56. for true {
  57. packet := make([]byte, 2048)
  58. _, err := socket.Read(packet)
  59. if err.(net.Error).Timeout() {
  60. break
  61. }
  62. HandleError(err)
  63. scan = append(scan, packet...)
  64. }
  65. println(string(scan))
  66. }
  67. func HandleError(err error) {
  68. if err != nil {
  69. log.Fatal(err)
  70. os.Exit(1)
  71. }
  72. }
  73. func GetCompressionMode(_mode string) (string, string) {
  74. if _mode == "GRAY64" {
  75. return _mode, "NONE"
  76. } else {
  77. return "CGRAY", "JPEG"
  78. }
  79. }
  80. func SendPacket(socket net.Conn, packet []byte) {
  81. _, err := socket.Write(packet)
  82. HandleError(err)
  83. }
  84. func ReadPacket(socket net.Conn) string {
  85. reply := make([]byte, 64)
  86. _, err := socket.Read(reply)
  87. HandleError(err)
  88. return string(reply)
  89. }